async function loadCart() { try { const response = await fetch('/api/cart', { headers: getAuthHeaders() }); if (response.status === 401) { window.location.href = '/login.html'; return; } const items = await response.json(); if (items.length === 0) { container.innerHTML = `

Twój koszyk jest pusty

Przeglądaj książki
`; return; } const container = document.getElementById('cart-items'); container.innerHTML = items.map(item => `
${item.tytul}

Ilość: ${item.quantity}

${item.cena.toString()} PLN

`).join(''); } catch (error) { console.error('Błąd:', error); showError('Wystąpił błąd podczas ładowania koszyka'); } } 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: { 'Authorization': `Bearer ${token}` } }); if (!response.ok) throw new Error('Błąd ładowania koszyka'); const cartItems = await response.json(); const container = document.getElementById('cart-items'); container.innerHTML = ''; cartItems.forEach(item => { const itemHTML = `
${item.tytul}
${item.tytul}

Ilość: ${item.quantity}

Cena: ${item.cena} PLN

`; container.insertAdjacentHTML('beforeend', itemHTML); }); } catch (error) { console.error('Error:', error); alert('Nie udało się załadować koszyka'); } }); document.getElementById('checkoutBtn').addEventListener('click', async () => { try { const token = localStorage.getItem('token'); if (!token) { window.location.href = '/login.html'; return; } // Pobierz aktualną zawartość koszyka const cartResponse = await fetch('/api/cart', { headers: { 'Authorization': `Bearer ${token}` } }); const cartItems = await cartResponse.json(); // Przygotuj dane do zamówienia const checkoutData = { items: cartItems.map(item => ({ book_id: item.book_id, quantity: item.quantity })), total: cartItems.reduce((sum, item) => sum + (parseFloat(item.cena) * item.quantity), 0) }; // Wyślij zamówienie const response = await fetch('/api/checkout', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify(checkoutData) }); if (!response.ok) throw new Error('Błąd podczas składania zamówienia'); window.location.href = '/thankyou.html'; } catch (error) { console.error('Error:', error); alert('Nie udało się złożyć zamówienia: ' + error.message); } });