185 lines
6.2 KiB
Text
185 lines
6.2 KiB
Text
let userId = null;
|
|
let orderItems = [];
|
|
let totalPrice = 0;
|
|
|
|
const products = [
|
|
{ id: 1, nome: "Birra", prezzo: 5 },
|
|
{ id: 2, nome: "Acqua", prezzo: 1 },
|
|
{ id: 3, nome: "Pizza", prezzo: 10 },
|
|
{ id: 4, nome: "Patatine", prezzo: 3 },
|
|
{ id: 5, nome: "Panino", prezzo: 7 },
|
|
{ id: 6, nome: "Menu", prezzo: 12 }
|
|
];
|
|
|
|
// Funzione per leggere un cookie
|
|
function getCookie(name) {
|
|
const value = `; ${document.cookie}`;
|
|
const parts = value.split(`; ${name}=`);
|
|
if (parts.length === 2) return parts.pop().split(';').shift();
|
|
return null;
|
|
}
|
|
|
|
// Funzione per scrivere un cookie
|
|
function setCookie(name, value, days) {
|
|
const expires = new Date(Date.now() + days * 864e5).toUTCString();
|
|
document.cookie = `${name}=${value}; expires=${expires}; path=/`;
|
|
}
|
|
|
|
// Funzione per inizializzare la lista dei prodotti
|
|
function renderProductList() {
|
|
const productListDiv = document.getElementById("product-list");
|
|
productListDiv.innerHTML = "";
|
|
products.forEach((product) => {
|
|
// Cerca la quantità corrente nel carrello
|
|
const existingProduct = orderItems.find(item => item.id === product.id);
|
|
const quantity = existingProduct ? existingProduct.quantita : 0;
|
|
|
|
const productCard = document.createElement("div");
|
|
productCard.className = "product-card";
|
|
productCard.innerHTML = `
|
|
<div>${product.nome}</div>
|
|
<div>EUR ${product.prezzo}</div>
|
|
<div>
|
|
<button onclick="decreaseQuantity(${product.id})">-</button>
|
|
<span id="quantity-${product.id}">${quantity}</span>
|
|
<button onclick="increaseQuantity(${product.id})">+</button>
|
|
</div>
|
|
`;
|
|
productListDiv.appendChild(productCard);
|
|
});
|
|
}
|
|
|
|
// Funzione per aumentare la quantità di un prodotto
|
|
function increaseQuantity(productId) {
|
|
const product = products.find((p) => p.id === productId);
|
|
if (product) {
|
|
const existingProduct = orderItems.find(item => item.id === productId);
|
|
if (existingProduct) {
|
|
existingProduct.quantita += 1;
|
|
} else {
|
|
orderItems.push({ ...product, quantita: 1 });
|
|
}
|
|
totalPrice += product.prezzo;
|
|
updateTotal();
|
|
updateProductQuantity(productId);
|
|
}
|
|
}
|
|
|
|
// Funzione per diminuire la quantità di un prodotto
|
|
function decreaseQuantity(productId) {
|
|
const product = products.find((p) => p.id === productId);
|
|
if (product) {
|
|
const existingProduct = orderItems.find(item => item.id === productId);
|
|
if (existingProduct) {
|
|
if (existingProduct.quantita > 1) {
|
|
existingProduct.quantita -= 1;
|
|
totalPrice -= product.prezzo;
|
|
} else {
|
|
// Rimuove completamente il prodotto se la quantità è 0
|
|
orderItems = orderItems.filter(item => item.id !== productId);
|
|
totalPrice -= product.prezzo;
|
|
}
|
|
}
|
|
updateTotal();
|
|
updateProductQuantity(productId);
|
|
}
|
|
}
|
|
|
|
// Funzione per aggiornare la quantità nella UI
|
|
function updateProductQuantity(productId) {
|
|
const existingProduct = orderItems.find(item => item.id === productId);
|
|
const quantity = existingProduct ? existingProduct.quantita : 0;
|
|
document.getElementById(`quantity-${productId}`).textContent = quantity;
|
|
}
|
|
|
|
// Funzione per aggiornare il totale
|
|
function updateTotal() {
|
|
document.getElementById("total-price").textContent = `Totale: EUR ${totalPrice}`;
|
|
}
|
|
|
|
// Funzione per confermare l'ordine
|
|
document.getElementById("confirm-order-btn").addEventListener("click", function() {
|
|
const tipoPagamento = document.querySelector('input[name="payment-method"]:checked').value; // Recupera il metodo di pagamento selezionato
|
|
const data = new Date().toLocaleTimeString(); // Data in formato hh:mm:ss
|
|
|
|
const order = {
|
|
id_utente: userId,
|
|
tipo_pagamento: tipoPagamento,
|
|
prodotti_acquistati: orderItems.map(item => ({
|
|
// id: item.id,
|
|
nome: item.nome,
|
|
prezzo: item.prezzo,
|
|
quantita: item.quantita
|
|
})),
|
|
prezzo_totale: totalPrice,
|
|
data: data
|
|
};
|
|
|
|
// Salviamo l'ordine nel server
|
|
fetch("/ordini", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(order)
|
|
}).then(response => response.json()).then(data => {
|
|
if (data.message === 'Ordine salvato') {
|
|
showOrderSummary(order);
|
|
resetOrder(); // Resetta l'ordine dopo l'invio
|
|
}
|
|
});
|
|
});
|
|
|
|
// Mostra il riepilogo dell'ordine
|
|
function showOrderSummary(order) {
|
|
const orderSummaryDiv = document.getElementById("order-summary");
|
|
const orderDetailsDiv = document.getElementById("order-details");
|
|
let orderDetailsHTML = `<p>Ordine per l'ID Utente: ${order.id_utente}</p>`;
|
|
orderDetailsHTML += `<p>Tipo di pagamento: ${order.tipo_pagamento}</p>`;
|
|
order.prodotti_acquistati.forEach(item => {
|
|
orderDetailsHTML += `<p>${item.nome} - EUR ${item.prezzo} x ${item.quantita}</p>`;
|
|
});
|
|
orderDetailsHTML += `<p>Totale: EUR ${order.prezzo_totale}</p>`;
|
|
orderDetailsDiv.innerHTML = orderDetailsHTML;
|
|
|
|
orderSummaryDiv.style.display = "block";
|
|
document.getElementById("order-section").style.display = "none";
|
|
}
|
|
|
|
// Torna alla selezione dei prodotti
|
|
document.getElementById("back-to-order-btn").addEventListener("click", function() {
|
|
document.getElementById("order-summary").style.display = "none";
|
|
document.getElementById("order-section").style.display = "block";
|
|
});
|
|
|
|
// Reset dell'ordine
|
|
function resetOrder() {
|
|
orderItems = [];
|
|
totalPrice = 0;
|
|
updateTotal();
|
|
renderProductList();
|
|
document.getElementById("order-section").style.display = "block";
|
|
document.getElementById("order-summary").style.display = "none";
|
|
}
|
|
|
|
// Gestisci il login
|
|
document.getElementById("login-form").addEventListener("submit", function(event) {
|
|
event.preventDefault();
|
|
const inputId = document.getElementById("user-id").value;
|
|
if (inputId) {
|
|
setCookie('userId', inputId, 7); // Salva il cookie per 7 giorni
|
|
userId = inputId;
|
|
document.getElementById("login-section").style.display = "none";
|
|
document.getElementById("order-section").style.display = "block";
|
|
renderProductList();
|
|
}
|
|
});
|
|
|
|
// Verifica se l'ID utente è già nel cookie
|
|
window.onload = function() {
|
|
const savedUserId = getCookie('userId');
|
|
if (savedUserId) {
|
|
userId = savedUserId;
|
|
document.getElementById("login-section").style.display = "none";
|
|
document.getElementById("order-section").style.display = "block";
|
|
renderProductList();
|
|
}
|
|
};
|