import{a as g,a4 as _,r as c,j as e,d as y}from"./index-D4ZMoJFe.js";import{f as v}from"./formatters-jmx2pV9o.js";const S=new Date().toISOString().slice(0,10);function I(){var u;const{selectedCompanyId:b,user:s}=g(),d=b||(s==null?void 0:s.companyId)||((u=s==null?void 0:s.company)==null?void 0:u.id)||"",j=_("supplier_type"),[n,r]=c.useState({as_of:S,from:"",type:""}),[t,f]=c.useState({items:[],totals:null}),[N,i]=c.useState(!0),[m,p]=c.useState(""),x=c.useMemo(()=>({company_id:d,...n}),[d,n]);return c.useEffect(()=>{(async()=>{var l,h;if(d){i(!0),p("");try{const o=await y.get("/mgx/suppliers/balance",{params:x});f(o.data)}catch(o){p(((h=(l=o==null?void 0:o.response)==null?void 0:l.data)==null?void 0:h.message)||"Impossible de charger la balance fournisseurs.")}finally{i(!1)}}})()},[d,x]),e.jsxs("div",{className:"space-y-6",children:[e.jsxs("section",{className:"card-panel p-6 sm:p-8",children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.2em] text-gold",children:"MGX · Fournisseurs"}),e.jsx("h1",{className:"mt-3 text-3xl font-semibold text-text",children:"Balance Fournisseurs"})]}),e.jsx("section",{className:"card-panel p-5",children:e.jsxs("div",{className:"grid gap-3 xl:grid-cols-[180px_180px_220px]",children:[e.jsx("input",{type:"date",className:"input-dark",value:n.as_of,onChange:a=>r(l=>({...l,as_of:a.target.value}))}),e.jsx("input",{type:"date",className:"input-dark",value:n.from,onChange:a=>r(l=>({...l,from:a.target.value}))}),e.jsxs("select",{className:"input-dark",value:n.type,onChange:a=>r(l=>({...l,type:a.target.value})),children:[e.jsx("option",{value:"",children:"Tous types"}),j.map(a=>e.jsx("option",{value:a.id,children:a.label},a.id))]})]})}),m?e.jsx("div",{className:"rounded-2xl border border-danger/20 bg-danger/5 p-4 text-sm text-danger",children:m}):null,e.jsx("section",{className:"table-shell overflow-x-auto",children:e.jsxs("table",{className:"min-w-[1100px] w-full",children:[e.jsx("thead",{className:"table-head",children:e.jsx("tr",{children:["Code","Nom","Type","Total achats période","Total règlements","Solde dû","Échéance prochaine"].map(a=>e.jsx("th",{className:"table-cell",children:a},a))})}),e.jsx("tbody",{children:N?e.jsx("tr",{children:e.jsx("td",{colSpan:7,className:"table-cell text-textMuted",children:"Chargement..."})}):t.items.length?t.items.map(a=>{var l;return e.jsxs("tr",{className:"table-row",children:[e.jsx("td",{className:"table-cell font-mono font-semibold",children:a.code}),e.jsx("td",{className:"table-cell",children:a.name}),e.jsx("td",{className:"table-cell",children:((l=a.supplier_type)==null?void 0:l.label)||"—"}),e.jsx("td",{className:"table-cell amount-cell",children:a.total_purchases_formatted}),e.jsx("td",{className:"table-cell amount-cell",children:a.total_payments_formatted}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:a.balance_due_formatted}),e.jsx("td",{className:"table-cell",children:v(a.next_due_date)})]},a.id)}):e.jsx("tr",{children:e.jsx("td",{colSpan:7,className:"table-cell text-textMuted",children:"Aucune donnée disponible."})})}),t.totals?e.jsx("tfoot",{children:e.jsxs("tr",{className:"bg-surface2",children:[e.jsx("td",{colSpan:3,className:"table-cell font-semibold text-text",children:"Grand total"}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:t.totals.total_purchases_formatted}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:t.totals.total_payments_formatted}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:t.totals.balance_due_formatted}),e.jsx("td",{className:"table-cell"})]})}):null]})})]})}export{I as default};
