!function(){"use strict";var e=null,t=1;function n(e,t){var n=document.createElement("div");n.className="toast "+(t||"success"),n.textContent=e,document.body.appendChild(n),setTimeout((function(){n.remove()}),3e3)}function a(e,t){return t=t||{},fetch(""+e,t).then((function(e){return e.ok?e.json():e.json().then((function(e){throw new Error(e.error||"Error")}))}))}function o(e){if(!e)return"";var t=document.createElement("div");return t.textContent=e,t.innerHTML}function i(e){return"$"+(parseFloat(e)||0).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g,",")}function c(e){return e?new Date(e).toLocaleDateString("es-MX",{day:"2-digit",month:"short",year:"numeric"}):""}var d=null;function r(){var e=document.getElementById("customer-search").value,n=document.getElementById("customer-grid");n.innerHTML='
Cargando...
';var c="?page="+t+"&per_page=30";e&&(c+="&search="+encodeURIComponent(e)),a("/api/pos/customers"+c).then((function(e){var a=e.data||[];if(0===a.length)return n.innerHTML='
No se encontraron clientes
',void(document.getElementById("customer-pagination").innerHTML="");n.innerHTML=a.map((function(e){return'
'+o(e.name)+'
'+o(e.rfc||"Sin RFC")+'
'+i(e.balance)+'Limite: '+i(e.credit_limit)+"
"})).join(""),n.querySelectorAll(".customer-card-item").forEach((function(e){e.addEventListener("click",(function(){s(parseInt(e.getAttribute("data-id")))}))}));var c=e.pagination,d=document.getElementById("customer-pagination");c.total_pages<=1?d.innerHTML="":(d.innerHTML="Pag '+c.page+"/"+c.total_pages+"',d.querySelectorAll("button").forEach((function(e){e.addEventListener("click",(function(){t=parseInt(e.getAttribute("data-p")),r()}))})))})).catch((function(e){console.error("Error loading customers:",e),n.innerHTML='
Error al cargar clientes
'}))}function s(t){e=t,document.getElementById("list-view").style.display="none",document.getElementById("detail-view").style.display="block",a("/api/pos/customers/"+t+"/statement").then((function(e){var t=e.customer;document.getElementById("dh-name").textContent=t.name,document.getElementById("dh-rfc").textContent=t.rfc||"Sin RFC";var n=document.getElementById("dh-balance");n.textContent=i(t.balance),n.className="dh-value "+(t.balance>0?"danger":"success"),document.getElementById("dh-limit").textContent=i(t.credit_limit),document.getElementById("dh-terms").textContent=t.payment_terms+" dias";var a=document.getElementById("invoice-list");0===e.invoices.length?a.innerHTML='Sin facturas':a.innerHTML=e.invoices.map((function(e){return''+o(e.folio)+""+c(e.date_issued)+""+i(e.total)+""+i(e.amount_paid)+''+e.status+""})).join("");var d=document.getElementById("payment-list");0===e.payments.length?d.innerHTML='Sin pagos':d.innerHTML=e.payments.map((function(e){return""+c(e.date_payment)+''+i(e.amount)+""+o(e.payment_method)+""+o(e.reference||"")+""+o(e.invoice_folio||"General")+""})).join("");var r=document.getElementById("pay-invoice"),s=e.invoices.filter((function(e){return"paid"!==e.status&&"cancelled"!==e.status})).map((function(e){return'"})).join("");r.innerHTML=''+s}))}document.getElementById("customer-search").addEventListener("input",(function(){clearTimeout(d),d=setTimeout((function(){t=1,r()}),400)})),document.getElementById("btn-back-list").addEventListener("click",(function(){document.getElementById("detail-view").style.display="none",document.getElementById("list-view").style.display="block",e=null,r()})),document.getElementById("btn-pay").addEventListener("click",(function(){var t=parseFloat(document.getElementById("pay-amount").value);if(!t||t<=0)n("Ingresa un monto valido","error");else{var o=document.getElementById("pay-invoice").value;a("/api/pos/payments",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({customer_id:e,amount:t,payment_method:document.getElementById("pay-method").value,reference:document.getElementById("pay-reference").value.trim()||null,invoice_id:o?parseInt(o):null,notes:document.getElementById("pay-notes").value.trim()||null})}).then((function(){n("Pago de "+i(t)+" registrado"),document.getElementById("pay-amount").value="",document.getElementById("pay-reference").value="",document.getElementById("pay-notes").value="",s(e)})).catch((function(e){n(e.message,"error")}))}})),r()}();