2025-06-01 11:09:37 +02:00
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
|
|
const cartItemsContainer = document.getElementById('cart-items');
|
|
|
|
const checkoutBtn = document.getElementById('checkoutBtn');
|
|
|
|
const localPickupCheckbox = document.getElementById('localPickup');
|
|
|
|
|
|
|
|
// Załaduj zawartość koszyka
|
|
|
|
loadCart();
|
|
|
|
|
|
|
|
// 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 => {
|
|
|
|
const bookId = card.querySelector('.decrease-btn').dataset.bookId;
|
|
|
|
const quantity = parseInt(card.querySelector('.quantity-display').textContent);
|
|
|
|
const cena = parseFloat(card.querySelector('.card-text').textContent.replace(' PLN', ''));
|
|
|
|
const tytul = card.querySelector('.card-title').textContent;
|
|
|
|
const obraz_url = card.querySelector('img').src;
|
|
|
|
|
|
|
|
return {
|
|
|
|
book_id: parseInt(bookId),
|
|
|
|
quantity,
|
|
|
|
cena,
|
|
|
|
tytul,
|
|
|
|
obraz_url
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
updateSummary(cartItems);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checkoutBtn) {
|
|
|
|
checkoutBtn.addEventListener('click', handleCheckout);
|
|
|
|
}
|
|
|
|
});
|
2025-05-30 18:00:52 +02:00
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
async function loadCart() {
|
|
|
|
const cartItemsContainer = document.getElementById('cart-items');
|
|
|
|
|
|
|
|
if (!cartItemsContainer) return;
|
|
|
|
|
|
|
|
cartItemsContainer.innerHTML = `
|
|
|
|
<div class="text-center py-5">
|
|
|
|
<div class="spinner-border text-primary" role="status">
|
|
|
|
<span class="visually-hidden">Ładowanie...</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
2025-05-25 16:25:22 +02:00
|
|
|
try {
|
|
|
|
const response = await fetch('/api/cart', {
|
2025-06-01 11:09:37 +02:00
|
|
|
credentials: 'include'
|
2025-05-25 16:25:22 +02:00
|
|
|
});
|
2025-05-30 18:00:52 +02:00
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Błąd HTTP: ${response.status}`);
|
|
|
|
}
|
|
|
|
|
2025-05-25 16:25:22 +02:00
|
|
|
const cartItems = await response.json();
|
2025-06-01 11:09:37 +02:00
|
|
|
displayCartItems(cartItems);
|
|
|
|
updateSummary(cartItems);
|
2025-05-30 18:00:52 +02:00
|
|
|
} catch (error) {
|
2025-06-01 11:09:37 +02:00
|
|
|
console.error('Błąd ładowania koszyka:', error);
|
|
|
|
cartItemsContainer.innerHTML = `
|
|
|
|
<div class="alert alert-danger">Wystąpił błąd podczas ładowania koszyka: ${error.message}</div>
|
|
|
|
`;
|
2025-05-30 18:00:52 +02:00
|
|
|
}
|
2025-06-01 11:09:37 +02:00
|
|
|
}
|
2025-05-30 18:00:52 +02:00
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
function displayCartItems(cartItems) {
|
|
|
|
const cartItemsContainer = document.getElementById('cart-items');
|
|
|
|
|
|
|
|
if (!cartItemsContainer) return;
|
|
|
|
|
2025-05-30 18:00:52 +02:00
|
|
|
if (cartItems.length === 0) {
|
2025-06-01 11:09:37 +02:00
|
|
|
cartItemsContainer.innerHTML = `
|
|
|
|
<div class="alert alert-info">
|
|
|
|
Twój koszyk jest pusty
|
|
|
|
</div>
|
|
|
|
`;
|
2025-05-30 18:00:52 +02:00
|
|
|
return;
|
|
|
|
}
|
2025-06-01 11:09:37 +02:00
|
|
|
|
|
|
|
cartItemsContainer.innerHTML = '';
|
|
|
|
|
2025-05-30 18:00:52 +02:00
|
|
|
cartItems.forEach(item => {
|
2025-06-01 11:09:37 +02:00
|
|
|
const itemElement = `
|
|
|
|
<div class="card mb-3">
|
|
|
|
<div class="row g-0">
|
|
|
|
<div class="col-md-2">
|
|
|
|
<img src="${item.obraz_url}" class="img-fluid rounded-start" alt="${item.tytul}">
|
|
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
|
|
<div class="card-body">
|
|
|
|
<h5 class="card-title">${item.tytul}</h5>
|
|
|
|
<p class="card-text">${item.cena} PLN</p>
|
2025-05-30 18:00:52 +02:00
|
|
|
</div>
|
2025-06-01 11:09:37 +02:00
|
|
|
</div>
|
|
|
|
<div class="col-md-4 d-flex align-items-center justify-content-end pe-3">
|
|
|
|
<div class="d-flex align-items-center">
|
|
|
|
<button class="btn btn-sm btn-outline-secondary decrease-btn" data-book-id="${item.book_id}">
|
|
|
|
<i class="bi bi-dash"></i>
|
|
|
|
</button>
|
|
|
|
<span class="mx-2 quantity-display">${item.quantity}</span>
|
|
|
|
<button class="btn btn-sm btn-outline-secondary increase-btn" data-book-id="${item.book_id}">
|
|
|
|
<i class="bi bi-plus"></i>
|
|
|
|
</button>
|
|
|
|
<button class="btn btn-sm btn-danger ms-2 remove-btn" data-book-id="${item.book_id}">
|
|
|
|
<i class="bi bi-trash"></i>
|
|
|
|
</button>
|
2025-05-25 16:25:22 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2025-05-30 15:40:23 +02:00
|
|
|
</div>
|
|
|
|
`;
|
2025-06-01 11:09:37 +02:00
|
|
|
cartItemsContainer.innerHTML += itemElement;
|
2025-05-30 18:00:52 +02:00
|
|
|
});
|
2025-06-01 11:09:37 +02:00
|
|
|
|
|
|
|
// Dodaj event listeners dla przycisków
|
|
|
|
document.querySelectorAll('.decrease-btn').forEach(btn => {
|
|
|
|
btn.addEventListener('click', () => updateCartItemQuantity(btn.dataset.bookId, -1));
|
|
|
|
});
|
|
|
|
|
|
|
|
document.querySelectorAll('.increase-btn').forEach(btn => {
|
|
|
|
btn.addEventListener('click', () => updateCartItemQuantity(btn.dataset.bookId, 1));
|
2025-05-30 18:00:52 +02:00
|
|
|
});
|
2025-06-01 11:09:37 +02:00
|
|
|
|
|
|
|
document.querySelectorAll('.remove-btn').forEach(btn => {
|
|
|
|
btn.addEventListener('click', () => removeCartItem(btn.dataset.bookId));
|
|
|
|
});
|
|
|
|
}
|
2025-05-30 15:40:23 +02:00
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
async function updateCartItemQuantity(bookId, change) {
|
|
|
|
try {
|
|
|
|
const response = await fetch('/api/update-cart-quantity', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
book_id: parseInt(bookId),
|
|
|
|
change: change
|
|
|
|
}),
|
|
|
|
credentials: 'include'
|
2025-05-30 15:40:23 +02:00
|
|
|
});
|
2025-06-01 11:09:37 +02:00
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
const quantityElement = document.querySelector(`.decrease-btn[data-book-id="${bookId}"]`).nextElementSibling;
|
|
|
|
const currentQuantity = parseInt(quantityElement.textContent);
|
|
|
|
const newQuantity = currentQuantity + change;
|
|
|
|
|
|
|
|
// Jeśli nowa ilość jest mniejsza lub równa 0, usuń produkt
|
|
|
|
if (newQuantity <= 0) {
|
|
|
|
const cartItem = quantityElement.closest('.card');
|
|
|
|
cartItem.style.opacity = '0';
|
|
|
|
setTimeout(() => {
|
|
|
|
cartItem.remove();
|
|
|
|
updateSummaryFromDOM();
|
|
|
|
}, 300);
|
|
|
|
|
|
|
|
// Dodatkowo wyślij żądanie usunięcia z serwera
|
|
|
|
await fetch(`/api/remove-from-cart/${bookId}`, {
|
|
|
|
method: 'DELETE',
|
|
|
|
credentials: 'include'
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Animacja zmiany ilości
|
|
|
|
quantityElement.textContent = newQuantity;
|
|
|
|
updateSummaryFromDOM();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
alert('Błąd aktualizacji ilości');
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Błąd:', error);
|
|
|
|
alert('Wystąpił błąd podczas aktualizacji ilości');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
const quantity = parseInt(card.querySelector('.quantity-display').textContent);
|
|
|
|
const cena = parseFloat(card.querySelector('.card-text').textContent.replace(' PLN', ''));
|
|
|
|
const tytul = card.querySelector('.card-title').textContent;
|
|
|
|
const obraz_url = card.querySelector('img').src;
|
|
|
|
|
|
|
|
return {
|
|
|
|
book_id: parseInt(bookId),
|
|
|
|
quantity,
|
|
|
|
cena,
|
|
|
|
tytul,
|
|
|
|
obraz_url
|
|
|
|
};
|
2025-05-30 18:00:52 +02:00
|
|
|
});
|
2025-06-01 11:09:37 +02:00
|
|
|
|
|
|
|
updateSummary(cartItems);
|
|
|
|
}
|
2025-05-30 15:40:23 +02:00
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
function updateSummary(cartItems) {
|
|
|
|
const localPickupCheckbox = document.getElementById('localPickup');
|
|
|
|
const productsValueEl = document.getElementById('products-value');
|
|
|
|
const deliveryValueEl = document.getElementById('delivery-value');
|
|
|
|
const totalValueEl = document.getElementById('total-value');
|
|
|
|
|
|
|
|
if (!productsValueEl || !deliveryValueEl || !totalValueEl) return;
|
|
|
|
|
|
|
|
// Oblicz wartość produktów
|
|
|
|
let productsValue = 0;
|
|
|
|
if (cartItems && cartItems.length > 0) {
|
|
|
|
productsValue = cartItems.reduce((total, item) => {
|
|
|
|
return total + (item.cena * item.quantity);
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
productsValueEl.textContent = productsValue.toFixed(2) + ' PLN';
|
|
|
|
|
|
|
|
// Oblicz koszt dostawy
|
|
|
|
const deliveryCost = localPickupCheckbox && localPickupCheckbox.checked ? 0 : 12.99;
|
|
|
|
deliveryValueEl.textContent = deliveryCost.toFixed(2) + ' PLN';
|
|
|
|
|
|
|
|
const totalValue = productsValue + deliveryCost;
|
|
|
|
totalValueEl.textContent = totalValue.toFixed(2) + ' PLN';
|
2025-05-30 18:00:52 +02:00
|
|
|
}
|
2025-05-25 16:54:16 +02:00
|
|
|
|
2025-06-01 11:09:37 +02:00
|
|
|
async function removeCartItem(bookId) {
|
2025-05-30 15:40:23 +02:00
|
|
|
try {
|
2025-06-01 11:09:37 +02:00
|
|
|
const response = await fetch(`/api/remove-from-cart/${bookId}`, {
|
|
|
|
method: 'DELETE',
|
|
|
|
credentials: 'include'
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
loadCart();
|
2025-05-30 18:00:52 +02:00
|
|
|
} else {
|
2025-06-01 11:09:37 +02:00
|
|
|
alert('Błąd usuwania z koszyka');
|
2025-05-30 15:40:23 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2025-06-01 11:09:37 +02:00
|
|
|
console.error('Błąd:', error);
|
|
|
|
alert('Wystąpił błąd podczas usuwania z koszyka');
|
2025-05-30 15:40:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-30 18:00:52 +02:00
|
|
|
async function handleCheckout() {
|
2025-06-01 11:09:37 +02:00
|
|
|
const cartItemsContainer = document.getElementById('cart-items');
|
|
|
|
const localPickupCheckbox = document.getElementById('localPickup');
|
|
|
|
|
|
|
|
if (!cartItemsContainer || cartItemsContainer.innerHTML.includes('Twój koszyk jest pusty')) {
|
|
|
|
alert('Twój koszyk jest pusty!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-05-25 16:54:16 +02:00
|
|
|
try {
|
2025-06-01 11:09:37 +02:00
|
|
|
// Pobierz aktualną zawartość koszyka
|
|
|
|
const response = await fetch('/api/cart', {
|
|
|
|
credentials: 'include'
|
2025-05-25 16:54:16 +02:00
|
|
|
});
|
2025-06-01 11:09:37 +02:00
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Błąd ładowania koszyka');
|
|
|
|
}
|
|
|
|
|
|
|
|
const cartItems = await response.json();
|
|
|
|
|
|
|
|
// Przygotuj dane do zamówienia
|
|
|
|
const checkoutData = {
|
|
|
|
items: cartItems.map(item => ({
|
|
|
|
book_id: item.book_id,
|
|
|
|
quantity: item.quantity
|
|
|
|
})),
|
|
|
|
total: parseFloat(document.getElementById('total-value').textContent),
|
|
|
|
delivery_type: localPickupCheckbox.checked ? "local" : "shipping"
|
|
|
|
};
|
|
|
|
|
2025-05-25 16:54:16 +02:00
|
|
|
// Wyślij zamówienie
|
2025-06-01 11:09:37 +02:00
|
|
|
const checkoutResponse = await fetch('/api/checkout', {
|
2025-05-25 16:54:16 +02:00
|
|
|
method: 'POST',
|
2025-06-01 11:09:37 +02:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify(checkoutData),
|
|
|
|
credentials: 'include'
|
2025-05-25 16:54:16 +02:00
|
|
|
});
|
2025-06-01 11:09:37 +02:00
|
|
|
|
|
|
|
if (checkoutResponse.ok) {
|
|
|
|
window.location.href = '/thankyou.html';
|
|
|
|
} else {
|
|
|
|
const errorData = await checkoutResponse.json();
|
|
|
|
alert(`Błąd składania zamówienia: ${errorData.message || 'Nieznany błąd'}`);
|
|
|
|
}
|
2025-05-25 16:54:16 +02:00
|
|
|
} catch (error) {
|
2025-06-01 11:09:37 +02:00
|
|
|
console.error('Błąd:', error);
|
|
|
|
alert('Wystąpił błąd podczas składania zamówienia');
|
2025-05-25 16:54:16 +02:00
|
|
|
}
|
2025-05-30 18:00:52 +02:00
|
|
|
}
|