/** * tienda.js — Store / Tablet dashboard logic for Nexus Autoparts */ (function () { 'use strict'; var API = ''; // ================================================================ // Utility // ================================================================ function fmt(n) { return '$' + (parseFloat(n) || 0).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ','); } function esc(s) { if (!s) return ''; var d = document.createElement('div'); d.textContent = s; return d.innerHTML; } // ================================================================ // Clock // ================================================================ function updateClock() { var now = new Date(); var h = now.getHours(); var m = String(now.getMinutes()).padStart(2, '0'); var ampm = h >= 12 ? 'PM' : 'AM'; h = h % 12 || 12; document.getElementById('clock').textContent = h + ':' + m + ' ' + ampm; } updateClock(); setInterval(updateClock, 30000); // ================================================================ // Load Dashboard Stats // ================================================================ function loadStats() { fetch(API + '/api/tienda/stats') .then(function (r) { return r.json(); }) .then(function (d) { var st = d.sales_today || {}; var sm = d.sales_month || {}; var pt = d.payments_today || {}; // KPIs document.getElementById('kpi-sales-today').textContent = fmt(st.total); document.getElementById('kpi-sales-count').textContent = (st.count || 0) + ' facturas'; document.getElementById('kpi-month').textContent = fmt(sm.total); document.getElementById('kpi-month-count').textContent = (sm.count || 0) + ' facturas'; document.getElementById('kpi-customers').textContent = d.total_customers || 0; document.getElementById('kpi-parts-count').textContent = (d.total_parts || 0) + ' partes'; document.getElementById('kpi-pending').textContent = fmt(d.pending_balance || 0); document.getElementById('kpi-pending-count').textContent = (d.pending_invoices || 0) + ' facturas'; // Today's payments document.getElementById('payments-today-amount').textContent = fmt(pt.total); document.getElementById('payments-today-count').textContent = (pt.count || 0) + ' pagos registrados'; // Top debtors renderDebtors(d.top_debtors || []); // Recent invoices renderInvoices(d.recent_invoices || []); }) .catch(function (err) { console.error('Error loading stats:', err); }); } // ================================================================ // Render Debtors // ================================================================ function renderDebtors(debtors) { var el = document.getElementById('debtors-list'); if (debtors.length === 0) { el.innerHTML = '