From 5e0a3cfabee11f4ad5d8c6d9e7a8e7a6b5ca68af Mon Sep 17 00:00:00 2001
From: Lheorvine <lheor@tuta.io>
Date: Sun, 1 Jun 2025 17:41:00 +0200
Subject: [PATCH] Beta

---
 Cargo.toml                                  |  2 +-
 migrations/20250601094238_zmień_dostawe.sql |  3 +++
 static/cart.html                            | 13 +++++--------
 static/js/book.js                           | 14 +++++++++++++-
 static/js/books.js                          | 15 +++++++++++++--
 static/js/cart.js                           | 14 ++++++++++----
 static/js/profile.js                        | 17 ++++++++++++++++-
 7 files changed, 61 insertions(+), 17 deletions(-)
 create mode 100644 migrations/20250601094238_zmień_dostawe.sql

diff --git a/Cargo.toml b/Cargo.toml
index 1901332..14cdef3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "ksiegarnia"
-version = "0.1.0"
+version = "0.9.0"
 edition = "2021"
 
 [dependencies]
diff --git a/migrations/20250601094238_zmień_dostawe.sql b/migrations/20250601094238_zmień_dostawe.sql
new file mode 100644
index 0000000..5267c38
--- /dev/null
+++ b/migrations/20250601094238_zmień_dostawe.sql
@@ -0,0 +1,3 @@
+ALTER TABLE zamowienia ALTER COLUMN typ_dostawy SET DEFAULT 'shipping';
+
+UPDATE zamowienia SET typ_dostawy = 'shipping' WHERE typ_dostawy IS NULL;
diff --git a/static/cart.html b/static/cart.html
index d0f749b..196d8e6 100644
--- a/static/cart.html
+++ b/static/cart.html
@@ -58,19 +58,16 @@
 				<span class="text-primary" id="products-value">0.00 PLN</span>
 			    </div>
 			    
-			    <!-- Zmieniony układ dla opcji dostawy -->
-			    <div class="mb-3">
-				<div class="d-flex justify-content-between">
+				<div class="d-flex justify-content-between mb-3 align-items-center">
 				    <div class="form-check">
-					<input class="form-check-input" type="checkbox" id="localPickup">
+					<input class="form-check-input" type="checkbox" id="localPickup" checked>
 					<label class="form-check-label" for="localPickup">
-					    Dostawa
+					    Dostawa (12.99 PLN)
 					</label>
 				    </div>
 				    <span id="delivery-value">12.99 PLN</span>
-				</div>
-			    </div>
-			    
+				</div>			    
+
 			    <hr>
 			    <div class="d-flex justify-content-between fw-bold fs-5">
 				<span>Do zapłaty:</span>
diff --git a/static/js/book.js b/static/js/book.js
index 513c6a5..cb8d6d1 100644
--- a/static/js/book.js
+++ b/static/js/book.js
@@ -1,3 +1,5 @@
+import { formatCurrency } from "./utils.js"
+
 document.addEventListener('DOMContentLoaded', function() {
     const urlParams = new URLSearchParams(window.location.search);
     const bookId = urlParams.get('id');
@@ -38,9 +40,11 @@ async function loadBookDetails(bookId) {
 }
 
 function displayBookDetails(book) {
+    const price = formatCurrency(book.cena);
+
     document.getElementById('book-title').textContent = book.tytul;
     document.getElementById('book-author').textContent = book.autor;
-    document.getElementById('book-price').textContent = `${book.cena} PLN`;
+    document.getElementById('book-price').textContent = price;
     document.getElementById('book-description').textContent = book.opis;
     
     const bookCover = document.getElementById('book-cover');
@@ -51,6 +55,14 @@ function displayBookDetails(book) {
 }
 
 async function addToCart(bookId) {
+    const response = await fetch('/api/check-auth');
+    const data = await response.json();
+
+    if (!data.authenticated) {
+        window.location.href = '/login.html';
+        return;
+    }
+
     try {
         const response = await fetch('/api/add-to-cart', {
             method: 'POST',
diff --git a/static/js/books.js b/static/js/books.js
index 6186b84..faf7292 100644
--- a/static/js/books.js
+++ b/static/js/books.js
@@ -1,3 +1,5 @@
+import { formatCurrency } from './utils.js';
+
 document.addEventListener('DOMContentLoaded', function() {
     const booksContainer = document.getElementById('books-container');
     const searchInput = document.getElementById('searchInput');
@@ -72,6 +74,8 @@ function displayBooks(books) {
     booksContainer.innerHTML = '';
     
     books.forEach(book => {
+        const price = formatCurrency(book.cena);
+
         const bookCard = `
             <div class="book-card">
                 <a href="/book.html?id=${book.id}" class="book-cover-link">
@@ -80,7 +84,7 @@ function displayBooks(books) {
                         <div class="book-overlay">
                             <h5>${book.tytul}</h5>
                             <p>${book.autor}</p>
-                            <p class="price">${book.cena} PLN</p>
+                            <p class="price">${price}</p>
                         </div>
                     </div>
                 </a>
@@ -101,8 +105,15 @@ function displayBooks(books) {
     });
 }
 
-// Funkcja dodająca do koszyka
 async function addToCart(bookId) {
+    const response = await fetch('/api/check-auth');
+    const data = await response.json();
+
+    if (!data.authenticated) {
+        window.location.href = '/login.html';
+        return;
+    }
+
     try {
         const response = await fetch('/api/add-to-cart', {
             method: 'POST',
diff --git a/static/js/cart.js b/static/js/cart.js
index f3ed04f..072868a 100644
--- a/static/js/cart.js
+++ b/static/js/cart.js
@@ -8,7 +8,6 @@ document.addEventListener('DOMContentLoaded', function() {
     
     // Dodaj event listeners
     if (localPickupCheckbox) {
-        // JEDEN listener dla checkboxa
         localPickupCheckbox.addEventListener('change', function() {
             // Pobierz aktualny stan koszyka i zaktualizuj podsumowanie
             const cartItems = Array.from(document.querySelectorAll('.card.mb-3')).map(card => {
@@ -60,6 +59,13 @@ async function loadCart() {
         
         const cartItems = await response.json();
         displayCartItems(cartItems);
+        
+        // Ustaw checkbox jako odznaczony domyślnie (dostawa)
+        const localPickupCheckbox = document.getElementById('localPickup');
+        if (localPickupCheckbox) {
+            localPickupCheckbox.checked = false;
+        }
+        
         updateSummary(cartItems);
     } catch (error) {
         console.error('Błąd ładowania koszyka:', error);
@@ -179,7 +185,6 @@ async function updateCartItemQuantity(bookId, change) {
     }
 }
 
-// Funkcja do aktualizacji podsumowania na podstawie DOM
 function updateSummaryFromDOM() {
     const cartItems = Array.from(document.querySelectorAll('.card.mb-3')).map(card => {
         const bookId = card.querySelector('.decrease-btn').dataset.bookId;
@@ -218,8 +223,8 @@ function updateSummary(cartItems) {
     
     productsValueEl.textContent = productsValue.toFixed(2) + ' PLN';
     
-    // Oblicz koszt dostawy
-    const deliveryCost = localPickupCheckbox && localPickupCheckbox.checked ? 0 : 12.99;
+    // Oblicz koszt dostawy: jeśli checkbox ZAZNACZONY to 0 (odbiór lokalny), w przeciwnym razie 12.99 (dostawa)
+    const deliveryCost = localPickupCheckbox && localPickupCheckbox.checked ? 12.99 : 0;
     deliveryValueEl.textContent = deliveryCost.toFixed(2) + ' PLN';
     
     const totalValue = productsValue + deliveryCost;
@@ -272,6 +277,7 @@ async function handleCheckout() {
                 quantity: item.quantity
             })),
             total: parseFloat(document.getElementById('total-value').textContent),
+            // Odwrócona logika: checked = odbiór lokalny, unchecked = dostawa
             delivery_type: localPickupCheckbox.checked ? "local" : "shipping"
         };
         
diff --git a/static/js/profile.js b/static/js/profile.js
index b162211..733f563 100644
--- a/static/js/profile.js
+++ b/static/js/profile.js
@@ -50,13 +50,27 @@ function displayOrderHistory(orders) {
     
     orders.forEach(order => {
         const orderDate = new Date(order.data_zamowienia).toLocaleDateString();
+        
+        // Poprawione mapowanie typów dostawy
+        let deliveryType = order.typ_dostawy || 'shipping';
+        let deliveryText = '';
+        
+        if (deliveryType === 'local') {
+            deliveryText = 'Odbiór lokalny';
+        } else if (deliveryType === 'shipping') {
+            deliveryText = 'Dostawa';
+        } else {
+            // Dla starych zamówień bez typu
+            deliveryText = 'Dostawa';
+        }
+        
         const orderElement = `
             <div class="card mb-3 order-card">
                 <div class="card-header">
                     <h5>Zamówienie #${order.id} - ${orderDate}</h5>
                     <p class="mb-0">Status: ${order.status || 'W realizacji'}</p>
                     <p class="mb-0">Suma: ${order.suma_totalna} PLN</p>
-                    <p class="mb-0">Typ dostawy: ${order.typ_dostawy === 'local' ? 'Odbiór lokalny' : 'Dostawa'}</p>
+                    <p class="mb-0">Typ: ${deliveryText}</p>
                 </div>
                 <div class="card-body">
                     <h6>Produkty:</h6>
@@ -78,3 +92,4 @@ function displayOrderHistory(orders) {
         orderHistoryContainer.innerHTML += orderElement;
     });
 }
+