document.addEventListener('DOMContentLoaded', () => { let cart = []; const cartItemsContainer = document.getElementById('cart-items'); const cartTotalPrice = document.getElementById('cart-total-price'); const cartSubtotal = document.getElementById('cart-subtotal'); const checkoutBtn = document.getElementById('checkout-btn'); // Table Management let currentTableId = null; let currentTableName = null; const tableDisplay = document.getElementById('current-table-display'); const tableModalEl = document.getElementById('tableSelectionModal'); const tableSelectionModal = new bootstrap.Modal(tableModalEl); // Customer Search Elements 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'); // Product Search & Filter const productSearchInput = document.getElementById('product-search-input'); let currentCategory = 'all'; let currentSearchQuery = ''; // Helper for currency function formatCurrency(amount) { const settings = (typeof COMPANY_SETTINGS !== 'undefined') ? COMPANY_SETTINGS : { currency_symbol: '$', currency_decimals: 2 }; const symbol = settings.currency_symbol || '$'; const decimals = parseInt(settings.currency_decimals || 2); return symbol + parseFloat(amount).toFixed(decimals); } // --- Product Filtering (Category + Search) --- function filterProducts() { const items = document.querySelectorAll('.product-item'); items.forEach(item => { const matchesCategory = (currentCategory == 'all' || item.dataset.categoryId == currentCategory); const name = item.querySelector('.card-title').textContent.toLowerCase(); const matchesSearch = name.includes(currentSearchQuery); if (matchesCategory && matchesSearch) { item.style.display = 'block'; } else { item.style.display = 'none'; } }); } window.filterCategory = function(categoryId, btnElement) { currentCategory = categoryId; // Update Active Button State if (btnElement) { document.querySelectorAll('.category-btn').forEach(btn => btn.classList.remove('active')); btnElement.classList.add('active'); } else if (typeof btnElement === 'undefined' && categoryId !== 'all') { // Try to find the button corresponding to this category ID // This might happen if triggered programmatically } filterProducts(); }; if (productSearchInput) { productSearchInput.addEventListener('input', (e) => { currentSearchQuery = e.target.value.trim().toLowerCase(); filterProducts(); }); } // --- Customer Search --- let searchTimeout; if (customerSearchInput) { customerSearchInput.addEventListener('input', (e) => { const query = e.target.value.trim(); clearTimeout(searchTimeout); if (query.length < 2) { customerResults.style.display = 'none'; return; } searchTimeout = setTimeout(() => { fetch(`api/search_customers.php?q=${encodeURIComponent(query)}`) .then(res => res.json()) .then(data => { customerResults.innerHTML = ''; if (data.length > 0) { data.forEach(cust => { const a = document.createElement('a'); a.href = '#'; a.className = 'list-group-item list-group-item-action'; a.innerHTML = `
Cart is empty