// Basic logic for cart interactions document.addEventListener('DOMContentLoaded', () => { const saleForm = document.querySelector('[data-sale-form]'); if (!saleForm) return; let cart = []; const cartJsonInput = document.getElementById('cart_json'); const cartLinesContainer = document.getElementById('cart-lines'); const cartEmptyState = document.getElementById('cart-empty-state'); const cartCountLabel = document.getElementById('cart-count-label'); const cartSubtotalLabel = document.getElementById('cart-subtotal'); const cartTotalLabel = document.getElementById('cart-total'); function renderCart() { if (cart.length === 0) { cartEmptyState.style.display = 'block'; cartLinesContainer.innerHTML = ''; cartCountLabel.textContent = '0 ' + (window.saleLabels ? window.saleLabels.empty : 'items'); cartSubtotalLabel.textContent = '0.00'; cartTotalLabel.textContent = '0.00'; cartJsonInput.value = '[]'; return; } cartEmptyState.style.display = 'none'; let html = ''; let totalItems = 0; let subtotal = 0; cart.forEach((item, index) => { const lineTotal = item.qty * item.price; totalItems += item.qty; subtotal += lineTotal; html += `
${item.name}
${item.qty} x ${item.price.toFixed(3)}
${lineTotal.toFixed(3)}
`; }); cartLinesContainer.innerHTML = html; cartCountLabel.textContent = totalItems + ' items'; cartSubtotalLabel.textContent = subtotal.toFixed(3); cartTotalLabel.textContent = subtotal.toFixed(3); cartJsonInput.value = JSON.stringify(cart); } window.updateCartQty = function(index, delta) { if (cart[index]) { cart[index].qty += delta; if (cart[index].qty <= 0) { cart.splice(index, 1); } renderCart(); } }; window.removeCartItem = function(index) { cart.splice(index, 1); renderCart(); }; document.querySelectorAll('[data-add-product]').forEach(btn => { btn.addEventListener('click', () => { const sku = btn.dataset.sku; const name = btn.dataset.name; const price = parseFloat(btn.dataset.price); const existing = cart.find(i => i.sku === sku); if (existing) { existing.qty++; } else { cart.push({ sku, name, price, qty: 1 }); } // SweetAlert2 Toast for adding product if (typeof Swal !== 'undefined') { Swal.fire({ toast: true, position: 'top-end', icon: 'success', title: name + ' added', showConfirmButton: false, timer: 1500 }); } renderCart(); }); }); const clearBtn = document.querySelector('[data-clear-cart]'); if (clearBtn) { clearBtn.addEventListener('click', () => { cart = []; renderCart(); }); } // Handle form submission warning if empty saleForm.addEventListener('submit', (e) => { if (cart.length === 0) { e.preventDefault(); if (typeof Swal !== 'undefined') { Swal.fire('Empty Cart', 'Please add items before saving.', 'warning'); } else { Swal.fire({icon: 'warning', text: 'Cart is empty'}); } } }); renderCart(); });