document.addEventListener('DOMContentLoaded', () => { // SweetAlert2 Toast Mixin const Toast = (typeof Swal !== 'undefined') ? Swal.mixin({ toast: true, position: 'top-end', showConfirmButton: false, timer: 3000, timerProgressBar: true, didOpen: (toast) => { toast.addEventListener('mouseenter', Swal.stopTimer); toast.addEventListener('mouseleave', Swal.resumeTimer); } }) : null; function showToast(msg, type = 'primary') { if (!Toast) { console.log(`Toast: ${msg} (${type})`); return; } let icon = 'info'; if (type === 'success') icon = 'success'; if (type === 'danger') icon = 'error'; if (type === 'warning') icon = 'warning'; Toast.fire({ icon: icon, title: msg }); } // Global settings with fallbacks const settings = (typeof COMPANY_SETTINGS !== 'undefined') ? COMPANY_SETTINGS : { currency_symbol: '$', currency_decimals: 2, vat_rate: 0 }; let cart = []; let currentOrderId = null; const cartItemsContainer = document.getElementById('cart-items'); const cartTotalPrice = document.getElementById('cart-total-price'); const cartSubtotal = document.getElementById('cart-subtotal'); const cartVatInput = document.getElementById('cart-vat-input'); const quickOrderBtn = document.getElementById('quick-order-btn'); const placeOrderBtn = document.getElementById('place-order-btn'); const recallBtn = document.getElementById('recall-bill-btn'); const reprintReceiptBtn = document.getElementById('reprint-receipt-btn'); const recallModalEl = document.getElementById('recallOrderModal'); const recallModal = recallModalEl ? new bootstrap.Modal(recallModalEl) : null; const recallList = document.getElementById('recall-orders-list'); let isLoyaltyRedemption = false; const loyaltySection = document.getElementById('loyalty-section'); const loyaltyPointsDisplay = document.getElementById('loyalty-points-display'); const loyaltyMessage = document.getElementById('loyalty-message'); const redeemLoyaltyBtn = document.getElementById('redeem-loyalty-btn'); const viewPointsHistoryBtn = document.getElementById('view-points-history-btn'); const pointsHistoryModalEl = document.getElementById('pointsHistoryModal'); const pointsHistoryModal = pointsHistoryModalEl ? new bootstrap.Modal(pointsHistoryModalEl) : null; const pointsHistoryBody = document.getElementById('points-history-body'); const pointsHistoryEmpty = document.getElementById('points-history-empty'); const addCustomerModalEl = document.getElementById('addCustomerModal'); const addCustomerModal = addCustomerModalEl ? new bootstrap.Modal(addCustomerModalEl) : null; const saveNewCustomerBtn = document.getElementById('save-new-customer'); let currentTableId = null; let currentTableName = null; const tableDisplay = document.getElementById('current-table-display'); const tableModalEl = document.getElementById('tableSelectionModal'); const tableSelectionModal = tableModalEl ? new bootstrap.Modal(tableModalEl) : null; const variantModalEl = document.getElementById('variantSelectionModal'); const variantSelectionModal = variantModalEl ? new bootstrap.Modal(variantModalEl) : null; let pendingProduct = null; const customerSearchInput = document.getElementById('customer-search'); const customerResults = document.getElementById('customer-results'); const selectedCustomerId = document.getElementById('selected-customer-id'); const clearCustomerBtn = document.getElementById('clear-customer'); const customerInfo = document.getElementById('customer-info'); const customerNameDisplay = document.getElementById('customer-name-display'); let currentCustomer = null; const paymentModalEl = document.getElementById('paymentSelectionModal'); const paymentSelectionModal = paymentModalEl ? new bootstrap.Modal(paymentModalEl) : null; const paymentMethodsContainer = document.getElementById('payment-methods-container'); const productSearchInput = document.getElementById('product-search'); let currentCategory = 'all'; let currentSearchQuery = ''; // Translation helper check const _t = (key) => (typeof t === 'function') ? t(key) : key; function formatCurrency(amount) { const symbol = settings.currency_symbol || '$'; const decimals = parseInt(settings.currency_decimals || 2); return symbol + parseFloat(amount).toFixed(decimals); } function filterProducts() { const items = document.querySelectorAll('.product-item'); items.forEach(item => { const matchesCategory = (currentCategory == 'all' || item.dataset.category == currentCategory); const name = (item.dataset.name || '').toLowerCase(); const name_ar = (item.dataset.nameAr || '').toLowerCase(); const sku = (item.dataset.sku || '').toLowerCase(); const matchesSearch = name.includes(currentSearchQuery) || name_ar.includes(currentSearchQuery) || sku.includes(currentSearchQuery); item.style.display = (matchesCategory && matchesSearch) ? 'block' : 'none'; }); } window.filterCategory = function(categoryId, btnElement) { currentCategory = categoryId; document.querySelectorAll('.category-btn').forEach(btn => btn.classList.remove('active')); if (btnElement) { btnElement.classList.add('active'); } else { const btn = document.querySelector(`.category-btn[data-category="${categoryId}"]`); if (btn) btn.classList.add('active'); } filterProducts(); }; document.querySelectorAll('.category-btn').forEach(btn => { btn.addEventListener('click', () => { filterCategory(btn.dataset.category, btn); }); }); if (productSearchInput) { productSearchInput.addEventListener('input', (e) => { currentSearchQuery = e.target.value.trim().toLowerCase(); filterProducts(); }); } window.openRecallOrderModal = function() { if (!recallModal) return; fetchRecallOrders(); recallModal.show(); }; function fetchRecallOrders() { if (!recallList) return; recallList.innerHTML = '
${_t('cart_empty')}