// static/js/cart.js import { getAuthHeaders, handleApiError } from './utils.js'; document.addEventListener('DOMContentLoaded', async () => { try { const token = localStorage.getItem('token'); if (!token) { window.location.href = '/login.html'; return; } const response = await fetch('/api/cart', { headers: getAuthHeaders() }); if (!response.ok) throw new Error('Błąd ładowania koszyka'); const cartItems = await response.json(); renderCart(cartItems); } catch (error) { handleApiError(error, 'Nie udało się załadować koszyka'); } }); function renderCart(cartItems) { const container = document.getElementById('cart-items'); container.innerHTML = ''; if (cartItems.length === 0) { container.innerHTML = '

Twój koszyk jest pusty

'; return; } let totalCartValue = 0; cartItems.forEach(item => { const price = parseFloat(item.cena); const itemTotal = price * item.quantity; totalCartValue += itemTotal; const itemHTML = `
${item.tytul}
${item.tytul}

${item.autor}

Cena jednostkowa ${price.toFixed(2)} PLN
Ilość
${item.quantity}
Suma ${itemTotal.toFixed(2)} PLN
`; container.insertAdjacentHTML('beforeend', itemHTML); }); // Suma całkowita const totalHTML = `

Suma całkowita

${totalCartValue.toFixed(2)} PLN

`; container.insertAdjacentHTML('beforeend', totalHTML); // Obsługa przycisków document.querySelectorAll('.decrease-quantity').forEach(button => { button.addEventListener('click', function() { const bookId = this.dataset.bookId; updateCartItemQuantity(bookId, 'decrease'); }); }); document.querySelectorAll('.remove-from-cart').forEach(button => { button.addEventListener('click', function() { const bookId = this.dataset.bookId; updateCartItemQuantity(bookId, 'remove'); }); }); // Finalizacja zamówienia document.getElementById('checkoutBtn')?.addEventListener('click', handleCheckout); } async function updateCartItemQuantity(bookId, action) { try { const token = localStorage.getItem('token'); if (!token) { window.location.href = '/login.html'; return; } let method, endpoint; if (action === 'decrease') { endpoint = `/api/decrease-cart-item/${bookId}`; method = 'POST'; } else { endpoint = `/api/remove-from-cart/${bookId}`; method = 'DELETE'; } const response = await fetch(endpoint, { method, headers: getAuthHeaders() }); if (!response.ok) throw new Error('Błąd aktualizacji koszyka'); location.reload(); } catch (error) { handleApiError(error); } } async function handleCheckout() { try { const token = localStorage.getItem('token'); if (!token) { window.location.href = '/login.html'; return; } // Pobierz zawartość koszyka const cartResponse = await fetch('/api/cart', { headers: getAuthHeaders() }); if (!cartResponse.ok) throw new Error('Błąd pobierania koszyka'); const cartItems = await cartResponse.json(); // Wyślij zamówienie const response = await fetch('/api/checkout', { method: 'POST', headers: getAuthHeaders(), body: JSON.stringify({ items: cartItems.map(item => ({ book_id: item.book_id, quantity: item.quantity })), total: cartItems.reduce((sum, item) => sum + (parseFloat(item.cena) * item.quantity), 0) }) }); if (!response.ok) throw new Error('Błąd podczas składania zamówienia'); window.location.href = '/thankyou.html'; } catch (error) { handleApiError(error, 'Nie udało się złożyć zamówienia'); } }