document.addEventListener('DOMContentLoaded', () => { const body = document.body; const savedTheme = localStorage.getItem('store-theme'); if (savedTheme === 'dark' || savedTheme === 'light') { body.dataset.theme = savedTheme; } const themeToggle = document.querySelector('[data-theme-toggle]'); const helpToggle = document.querySelector('[data-help-toggle]'); const helpClose = document.querySelector('[data-help-close]'); const helpDrawer = document.querySelector('[data-help-drawer]'); themeToggle?.addEventListener('click', () => { const nextTheme = body.dataset.theme === 'dark' ? 'light' : 'dark'; body.dataset.theme = nextTheme; localStorage.setItem('store-theme', nextTheme); }); const toggleHelp = (open) => { if (!helpDrawer) return; helpDrawer.classList.toggle('is-open', open); helpDrawer.setAttribute('aria-hidden', open ? 'false' : 'true'); }; helpToggle?.addEventListener('click', () => toggleHelp(true)); helpClose?.addEventListener('click', () => toggleHelp(false)); helpDrawer?.addEventListener('click', (event) => { if (event.target === helpDrawer) { toggleHelp(false); } }); document.querySelectorAll('[data-auto-hide]').forEach((alert) => { setTimeout(() => { alert.style.transition = 'opacity .3s ease'; alert.style.opacity = '0'; setTimeout(() => alert.remove(), 320); }, 3200); }); const formatSyp = (value) => `${new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format(value || 0)} ل.س`; const rateInput = document.querySelector('[data-rate-input]'); const ratePreview = document.querySelector('[data-rate-preview]'); const equipmentUsd = document.querySelector('[data-equipment-usd]'); const equipmentPreview = document.querySelector('[data-equipment-preview]'); const invoiceEquipment = document.querySelector('[data-invoice-equipment]'); const invoiceQty = document.querySelector('[data-invoice-qty]'); const invoiceStock = document.querySelector('[data-invoice-stock]'); const invoiceTotal = document.querySelector('[data-invoice-total]'); const currentRate = () => { const parsed = parseFloat(rateInput?.value || body.dataset.currentRate || '0'); return Number.isFinite(parsed) ? parsed : 0; }; const updateRatePreview = () => { if (ratePreview) ratePreview.textContent = formatSyp(currentRate()); }; const updateEquipmentPreview = () => { if (!equipmentPreview) return; const usd = parseFloat(equipmentUsd?.value || '0'); equipmentPreview.textContent = usd > 0 && currentRate() > 0 ? formatSyp(usd * currentRate()) : '—'; }; const updateInvoicePreview = () => { if (!invoiceTotal) return; const option = invoiceEquipment?.selectedOptions?.[0]; const price = parseFloat(option?.dataset.price || '0'); const stock = parseInt(option?.dataset.stock || '0', 10); const qty = parseInt(invoiceQty?.value || '0', 10); if (invoiceStock) { invoiceStock.textContent = option && option.value ? `${stock} قطعة` : '—'; } if (!option || !option.value || price <= 0 || qty <= 0 || currentRate() <= 0) { invoiceTotal.textContent = '—'; return; } invoiceTotal.textContent = formatSyp(price * qty * currentRate()); }; rateInput?.addEventListener('input', () => { body.dataset.currentRate = rateInput.value; updateRatePreview(); updateEquipmentPreview(); updateInvoicePreview(); }); equipmentUsd?.addEventListener('input', updateEquipmentPreview); invoiceEquipment?.addEventListener('change', updateInvoicePreview); invoiceQty?.addEventListener('input', updateInvoicePreview); updateRatePreview(); updateEquipmentPreview(); updateInvoicePreview(); const printableInvoice = document.querySelector('[data-printable-invoice]'); const printButton = document.querySelector('[data-print-invoice]'); const exportSelectedButton = document.querySelector('[data-export-selected-pdf]'); const exportInvoicesButton = document.querySelector('[data-export-invoices]'); const invoicesTable = document.querySelector('[data-invoices-table]'); const openPrintWindow = (title, html) => { const win = window.open('', '_blank', 'width=900,height=700'); if (!win) return; win.document.write(`