const itemsData = 1; const lang = '1'; const currencyLabel = '1'; const decimalPrecision = 1; const editOrderId = 1; const loyaltyEnabled = 1; const pointsPerCurrency = 1; const currencyPerPoint = 1; let cart = 1; let selectionModal; let paymentModal; let customerLoyaltyPoints = 0; let pointsToRedeem = 0; // If not editing, try to load from local storage if (!editOrderId) { try { const saved = localStorage.getItem('pos_cart'); if (saved) { const parsed = JSON.parse(saved); if (Array.isArray(parsed) && parsed.length > 0) cart = parsed; } } catch (e) { console.error('Cart parse error', e); } } document.addEventListener('DOMContentLoaded', () => { if (typeof bootstrap !== 'undefined') { const modalEl = document.getElementById('selectionModal'); if (modalEl) selectionModal = new bootstrap.Modal(modalEl); const payModalEl = document.getElementById('paymentModal'); if (payModalEl) paymentModal = new bootstrap.Modal(payModalEl); } // Category filtering document.querySelectorAll('.cat-filter').forEach(btn => { btn.onclick = () => { const cat = btn.getAttribute('data-cat'); document.querySelectorAll('.cat-filter').forEach(b => b.classList.remove('btn-primary')); document.querySelectorAll('.cat-filter').forEach(b => b.classList.add('btn-white', 'border')); btn.classList.add('btn-primary'); btn.classList.remove('btn-white', 'border'); document.querySelectorAll('.item-card-wrapper').forEach(card => { if (cat === 'all' || card.getAttribute('data-cat') === cat) card.style.display = 'block'; else card.style.display = 'none'; }); }; }); // Search const searchInput = document.getElementById('itemSearch'); if (searchInput) { searchInput.addEventListener('input', function(e) { const q = e.target.value.toLowerCase(); document.querySelectorAll('.item-card-wrapper').forEach(card => { const en = card.getAttribute('data-name-en') || ''; const ar = card.getAttribute('data-name-ar') || ''; if (en.includes(q) || ar.includes(q)) card.style.display = 'block'; else card.style.display = 'none'; }); }); } // Persistent Customer Search const custSearchInput = document.getElementById('customerSearchInput'); const custResults = document.getElementById('customerResults'); const clearCustBtn = document.getElementById('clearCustomerBtn'); const custIdInput = document.getElementById('customerId'); // Handle Edit Customer Pre-fill const initialCustId = "1"; const initialCustBtn = document.querySelector(`.customer-result-item[data-id="${initialCustId}"]`); if (initialCustBtn) { selectCustomer(initialCustId, initialCustBtn.getAttribute('data-name'), initialCustBtn.getAttribute('data-points')); } if (custSearchInput) { custSearchInput.addEventListener('focus', () => { custResults.classList.remove('d-none'); }); custSearchInput.addEventListener('input', function(e) { const q = e.target.value.toLowerCase(); custResults.classList.remove('d-none'); document.querySelectorAll('.customer-result-item').forEach(item => { const search = item.getAttribute('data-search') || ''; if (search.includes(q)) { item.parentElement.style.display = 'block'; } else { item.parentElement.style.display = 'none'; } }); }); document.addEventListener('click', function(e) { if (!document.getElementById('customerSearchWrapper').contains(e.target)) { custResults.classList.add('d-none'); } }); } if (clearCustBtn) { clearCustBtn.addEventListener('click', () => { resetCustomerSelection(); }); } custResults.addEventListener('click', function(e) { const btn = e.target.closest('.customer-result-item'); if (btn) { const id = btn.getAttribute('data-id'); const name = btn.getAttribute('data-name'); const points = btn.getAttribute('data-points') || 0; selectCustomer(id, name, points); custResults.classList.add('d-none'); } }); updateCart(); }); function selectCustomer(id, name, points) { const custIdInput = document.getElementById('customerId'); const custSearchInput = document.getElementById('customerSearchInput'); const clearCustBtn = document.getElementById('clearCustomerBtn'); const loyaltyDisplay = document.getElementById('loyaltyDisplay'); const loyaltyRedeemUI = document.getElementById('loyaltyRedeemUI'); const customerPointsEl = document.getElementById('customerPoints'); const redeemHint = document.getElementById('redeemHint'); custIdInput.value = id; customerLoyaltyPoints = parseFloat(points); if (id) { custSearchInput.value = name; clearCustBtn.classList.remove('d-none'); if (loyaltyEnabled) { loyaltyDisplay.classList.remove('d-none'); loyaltyRedeemUI.classList.remove('d-none'); customerPointsEl.innerText = customerLoyaltyPoints.toFixed(2); if (redeemHint) { redeemHint.innerText = lang === 'en' ? `1 Point = ${currencyPerPoint} ${currencyLabel}` : `1 نقطة = ${currencyPerPoint} ${currencyLabel}`; } } } else { resetCustomerSelection(); } } function resetCustomerSelection() { const custIdInput = document.getElementById('customerId'); const custSearchInput = document.getElementById('customerSearchInput'); const clearCustBtn = document.getElementById('clearCustomerBtn'); const loyaltyDisplay = document.getElementById('loyaltyDisplay'); const loyaltyRedeemUI = document.getElementById('loyaltyRedeemUI'); custIdInput.value = ''; custSearchInput.value = ''; custSearchInput.placeholder = lang === 'en' ? 'Walk-in Customer' : 'عميل عابر'; clearCustBtn.classList.add('d-none'); loyaltyDisplay.classList.add('d-none'); loyaltyRedeemUI.classList.add('d-none'); customerLoyaltyPoints = 0; pointsToRedeem = 0; const ptsInput = document.getElementById('pointsToRedeem'); if (ptsInput) ptsInput.value = ''; updateCart(); } function applyPoints() { const ptsInput = document.getElementById('pointsToRedeem'); let val = parseFloat(ptsInput.value) || 0; if (val > customerLoyaltyPoints) { alert(lang === 'en' ? 'Insufficient points' : 'نقاط غير كافية'); val = customerLoyaltyPoints; ptsInput.value = val; } pointsToRedeem = val; updateCart(); } function showOptions(itemId) { const item = itemsData[itemId]; if (!item) return; const itemNameEl = document.getElementById('selectionItemName'); if (itemNameEl) itemNameEl.innerText = lang === 'en' ? item.name_en : item.name_ar; const list = document.getElementById('optionsList'); if (list) { list.innerHTML = ''; item.services.forEach(s => { const col = document.createElement('div'); col.className = 'col-6'; col.innerHTML = ` `; list.appendChild(col); }); } if (selectionModal) selectionModal.show(); } function addToCart(itemId, serviceId) { const item = itemsData[itemId]; if (!item) return; const service = item.services.find(s => s.id === serviceId); if (!service) return; const existing = cart.find(i => i.item_id === itemId && i.service_id === serviceId); if (existing) { existing.qty++; } else { cart.push({ item_id: itemId, service_id: serviceId, name: lang === 'en' ? item.name_en : item.name_ar, service_name: lang === 'en' ? service.name_en : service.name_ar, price: service.price, qty: 1, vat_percent: item.vat_percent }); } if (selectionModal) selectionModal.hide(); updateCart(); } function changeQty(index, delta) { cart[index].qty += delta; if (cart[index].qty <= 0) cart.splice(index, 1); updateCart(); } function updateCart() { if (!editOrderId) { localStorage.setItem('pos_cart', JSON.stringify(cart)); } const cartList = document.getElementById('cartItems'); const emptyCart = document.getElementById('emptyCart'); if (!cartList || !emptyCart) return; if (cart.length === 0) { cartList.innerHTML = ''; emptyCart.style.display = 'block'; } else { emptyCart.style.display = 'none'; cartList.innerHTML = cart.map((item, index) => `