135 lines
4.4 KiB
JavaScript
135 lines
4.4 KiB
JavaScript
// Variabili globali
|
|
let userId = null; // ID utente
|
|
let orderItems = []; // Array che contiene i prodotti nel carrello
|
|
let totalPrice = 0; // Prezzo totale dell'ordine
|
|
|
|
// Sezione Login
|
|
document.getElementById('login-form').addEventListener('submit', function(event) {
|
|
event.preventDefault();
|
|
userId = document.getElementById('user-id').value;
|
|
if (userId) {
|
|
document.getElementById('login-section').style.display = 'none';
|
|
document.getElementById('payment-method-section').style.display = 'block';
|
|
loadProducts(); // Carica i prodotti disponibili
|
|
}
|
|
});
|
|
|
|
// Funzione per caricare i prodotti
|
|
function loadProducts() {
|
|
const products = [
|
|
{ id: 1, nome: 'Panino', prezzo: 5 },
|
|
{ id: 2, nome: 'Acqua', prezzo: 1.5 },
|
|
{ id: 3, nome: 'Birra', prezzo: 3 }
|
|
];
|
|
|
|
const productListElem = document.getElementById('product-list');
|
|
products.forEach(product => {
|
|
const productCard = document.createElement('div');
|
|
productCard.className = 'product-card';
|
|
productCard.innerHTML = `
|
|
<h4>${product.nome}</h4>
|
|
<p>€ ${product.prezzo}</p>
|
|
<button onclick="addToOrder(${product.id}, '${product.nome}', ${product.prezzo})">Aggiungi al carrello</button>
|
|
`;
|
|
productListElem.appendChild(productCard);
|
|
});
|
|
}
|
|
|
|
// Funzione per aggiungere prodotti al carrello
|
|
function addToOrder(id, nome, prezzo) {
|
|
const productInOrder = orderItems.find(item => item.id === id);
|
|
|
|
if (productInOrder) {
|
|
productInOrder.quantita += 1;
|
|
} else {
|
|
orderItems.push({ id, nome, prezzo, quantita: 1 });
|
|
}
|
|
|
|
totalPrice += prezzo;
|
|
updateOrder();
|
|
}
|
|
|
|
// Funzione per aggiornare il carrello
|
|
function updateOrder() {
|
|
const orderItemsElem = document.getElementById('order-items');
|
|
orderItemsElem.innerHTML = '';
|
|
|
|
orderItems.forEach(item => {
|
|
const listItem = document.createElement('div');
|
|
listItem.textContent = `${item.nome} x ${item.quantita} - € ${item.prezzo * item.quantita}`;
|
|
orderItemsElem.appendChild(listItem);
|
|
});
|
|
|
|
document.getElementById('total-price').textContent = totalPrice;
|
|
}
|
|
|
|
// Mostra il riepilogo dell'ordine
|
|
document.getElementById('show-summary-btn').addEventListener('click', function() {
|
|
document.getElementById('order-section').style.display = 'none';
|
|
document.getElementById('order-summary').style.display = 'block';
|
|
updateOrderSummary();
|
|
});
|
|
|
|
// Funzione per aggiornare il riepilogo dell'ordine
|
|
function updateOrderSummary() {
|
|
const paymentMethod = document.querySelector('input[name="payment-method"]:checked').value;
|
|
const totalPriceElem = document.getElementById('summary-total-price');
|
|
const paymentMethodElem = document.getElementById('summary-payment-method');
|
|
const summaryProductsElem = document.getElementById('summary-products');
|
|
|
|
paymentMethodElem.textContent = paymentMethod;
|
|
totalPriceElem.textContent = totalPrice;
|
|
|
|
summaryProductsElem.innerHTML = '';
|
|
orderItems.forEach(item => {
|
|
const listItem = document.createElement('li');
|
|
listItem.textContent = `${item.nome} x ${item.quantita}`;
|
|
summaryProductsElem.appendChild(listItem);
|
|
});
|
|
}
|
|
|
|
// Annulla l'ordine e torna alla selezione dei prodotti
|
|
document.getElementById('cancel-order-btn').addEventListener('click', function() {
|
|
document.getElementById('order-summary').style.display = 'none';
|
|
document.getElementById('order-section').style.display = 'block';
|
|
});
|
|
|
|
// Conferma l'ordine
|
|
document.getElementById('confirm-order-btn').addEventListener('click', function() {
|
|
const tipoPagamento = document.querySelector('input[name="payment-method"]:checked').value;
|
|
const data = new Date().toLocaleTimeString();
|
|
|
|
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
|
|
};
|
|
|
|
// Invia l'ordine al 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') {
|
|
alert("Ordine confermato!");
|
|
resetOrder(); // Resetta l'ordine dopo l'invio
|
|
}
|
|
});
|
|
});
|
|
|
|
// Funzione per resettare l'ordine
|
|
function resetOrder() {
|
|
orderItems = [];
|
|
totalPrice = 0;
|
|
updateOrder();
|
|
document.getElementById('order-summary').style.display = 'none';
|
|
document.getElementById('order-section').style.display = 'block';
|
|
}
|