import{a as I,r as l,j as e,d as f}from"./index-D4ZMoJFe.js";import{f as C}from"./formatters-jmx2pV9o.js";const j=new Date,D=new Date(j.getFullYear(),0,1).toISOString().slice(0,10),M=j.toISOString().slice(0,10);function F(){var h;const{selectedCompanyId:b,user:d}=I(),r=b||(d==null?void 0:d.companyId)||((h=d==null?void 0:d.company)==null?void 0:h.id)||"",[g,N]=l.useState([]),[i,y]=l.useState([]),[c,o]=l.useState({from:D,to:M}),[S,v]=l.useState({items:[]}),[_,m]=l.useState(!0),[p,x]=l.useState(""),u=l.useMemo(()=>({company_id:r,...c,supplier_ids:i.join(",")}),[r,c,i]);return l.useEffect(()=>{(async()=>{if(!r)return;const s=await f.get("/mgx/suppliers",{params:{company_id:r}});N(s.data.items||[])})()},[r]),l.useEffect(()=>{(async()=>{var s,a;if(r){m(!0),x("");try{const n=await f.get("/mgx/suppliers/grand-livre",{params:u});v(n.data)}catch(n){x(((a=(s=n==null?void 0:n.response)==null?void 0:s.data)==null?void 0:a.message)||"Impossible de charger le grand livre fournisseurs.")}finally{m(!1)}}})()},[r,u]),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:"Grand Livre Fournisseurs"})]}),e.jsx("section",{className:"card-panel p-5",children:e.jsxs("div",{className:"grid gap-3 xl:grid-cols-[180px_180px_minmax(0,1fr)]",children:[e.jsx("input",{type:"date",className:"input-dark",value:c.from,onChange:t=>o(s=>({...s,from:t.target.value}))}),e.jsx("input",{type:"date",className:"input-dark",value:c.to,onChange:t=>o(s=>({...s,to:t.target.value}))}),e.jsx("select",{multiple:!0,className:"input-dark min-h-[120px]",value:i,onChange:t=>y(Array.from(t.target.selectedOptions,s=>s.value)),children:g.map(t=>e.jsxs("option",{value:t.id,children:[t.code," · ",t.name]},t.id))})]})}),p?e.jsx("div",{className:"rounded-2xl border border-danger/20 bg-danger/5 p-4 text-sm text-danger",children:p}):null,e.jsx("div",{className:"space-y-6",children:_?e.jsx("div",{className:"card-panel p-6 text-textMuted",children:"Chargement..."}):S.items.map(t=>{var s;return e.jsxs("section",{className:"card-panel overflow-hidden print:break-before-page",children:[e.jsxs("div",{className:"border-b border-border p-5",children:[e.jsxs("h2",{className:"text-xl font-semibold text-text",children:[t.supplier.code," · ",t.supplier.name]}),e.jsx("p",{className:"text-sm text-textMuted",children:((s=t.supplier.supplier_type)==null?void 0:s.label)||"Sans type"})]}),e.jsx("div",{className:"table-shell overflow-x-auto",children:e.jsxs("table",{className:"min-w-[980px] w-full",children:[e.jsx("thead",{className:"table-head",children:e.jsx("tr",{children:["Date","N° pièce","Type","Référence","Débit","Crédit","Solde"].map(a=>e.jsx("th",{className:"table-cell",children:a},a))})}),e.jsx("tbody",{children:t.rows.map((a,n)=>e.jsxs("tr",{className:"table-row",children:[e.jsx("td",{className:"table-cell",children:C(a.date)}),e.jsx("td",{className:"table-cell font-mono",children:a.piece_number}),e.jsx("td",{className:"table-cell",children:a.operation_type}),e.jsx("td",{className:"table-cell",children:a.reference}),e.jsx("td",{className:"table-cell amount-cell debit-amount",children:a.debit_formatted}),e.jsx("td",{className:"table-cell amount-cell credit-amount",children:a.credit_formatted}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:a.running_balance_formatted})]},`${t.supplier.id}-${n}`))}),e.jsx("tfoot",{children:e.jsxs("tr",{className:"bg-surface2",children:[e.jsx("td",{colSpan:4,className:"table-cell font-semibold text-text",children:"Totaux"}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:t.totals.debit_formatted}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:t.totals.credit_formatted}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:t.totals.balance_formatted})]})})]})})]},t.supplier.id)})})]})}export{F as default};
