import{t as v,a as _,r as n,j as e,L as D,d as I}from"./index-D4ZMoJFe.js";import{f as M}from"./formatters-jmx2pV9o.js";import{A as w}from"./arrow-left-DvOngpmh.js";import{D as j}from"./download-CFmyYwm2.js";const N=new Date,C=new Date(N.getFullYear(),0,1).toISOString().slice(0,10),F=N.toISOString().slice(0,10);function R(){var h,f;const{id:d}=v(),{selectedCompanyId:g,user:l}=_(),r=g||(l==null?void 0:l.companyId)||((h=l==null?void 0:l.company)==null?void 0:h.id)||"",[o,i]=n.useState({from:C,to:F}),[a,y]=n.useState({item:null,rows:[],totals:null}),[S,m]=n.useState(!0),[x,u]=n.useState(""),p=n.useMemo(()=>({company_id:r,...o}),[r,o]);return n.useEffect(()=>{(async()=>{var s,b;if(!(!r||!d)){m(!0),u("");try{const c=await I.get(`/mgx/suppliers/${d}/statement`,{params:p});y(c.data)}catch(c){u(((b=(s=c==null?void 0:c.response)==null?void 0:s.data)==null?void 0:b.message)||"Impossible de charger le relevé fournisseur.")}finally{m(!1)}}})()},[r,d,p]),e.jsxs("div",{className:"space-y-6",children:[e.jsxs("section",{className:"card-panel p-6 sm:p-8",children:[e.jsxs(D,{to:"/mgx/suppliers",className:"inline-flex items-center gap-2 text-sm font-semibold text-accent hover:underline",children:[e.jsx(w,{size:16}),"Retour aux fournisseurs"]}),e.jsx("p",{className:"mt-5 text-xs uppercase tracking-[0.2em] text-gold",children:"MGX · Fournisseurs"}),e.jsx("h1",{className:"mt-3 text-3xl font-semibold text-text",children:"Relevé Fournisseur"}),e.jsx("p",{className:"mt-2 text-sm text-textMuted",children:((f=a.item)==null?void 0:f.name)||"—"})]}),e.jsx("section",{className:"card-panel p-5",children:e.jsxs("div",{className:"grid gap-3 xl:grid-cols-[150px_150px_1fr_140px_140px]",children:[e.jsx("input",{type:"date",className:"input-dark",value:o.from,onChange:t=>i(s=>({...s,from:t.target.value}))}),e.jsx("input",{type:"date",className:"input-dark",value:o.to,onChange:t=>i(s=>({...s,to:t.target.value}))}),e.jsx("div",{}),e.jsxs("button",{type:"button",className:"btn-secondary inline-flex items-center justify-center gap-2",children:[e.jsx(j,{size:16})," PDF"]}),e.jsxs("button",{type:"button",className:"btn-secondary inline-flex items-center justify-center gap-2",children:[e.jsx(j,{size:16})," Excel"]})]})}),x?e.jsx("div",{className:"rounded-2xl border border-danger/20 bg-danger/5 p-4 text-sm text-danger",children:x}):null,e.jsx("section",{className:"table-shell overflow-x-auto",children:e.jsxs("table",{className:"min-w-[1080px] w-full",children:[e.jsx("thead",{className:"table-head",children:e.jsx("tr",{children:["Date","N° pièce","Type opération","Référence","Débit","Crédit","Solde cumulé"].map(t=>e.jsx("th",{className:"table-cell",children:t},t))})}),e.jsx("tbody",{children:S?e.jsx("tr",{children:e.jsx("td",{colSpan:7,className:"table-cell text-textMuted",children:"Chargement..."})}):a.rows.length?a.rows.map((t,s)=>e.jsxs("tr",{className:"table-row",children:[e.jsx("td",{className:"table-cell",children:M(t.date)}),e.jsx("td",{className:"table-cell font-mono",children:t.piece_number}),e.jsx("td",{className:"table-cell",children:t.operation_type}),e.jsx("td",{className:"table-cell",children:t.reference}),e.jsx("td",{className:"table-cell amount-cell debit-amount",children:t.debit_formatted}),e.jsx("td",{className:"table-cell amount-cell credit-amount",children:t.credit_formatted}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:t.running_balance_formatted})]},`${t.date}-${s}`)):e.jsx("tr",{children:e.jsx("td",{colSpan:7,className:"table-cell text-textMuted",children:"Aucun mouvement trouvé."})})}),a.totals?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 debit-amount",children:a.totals.debit_formatted}),e.jsx("td",{className:"table-cell amount-cell font-semibold credit-amount",children:a.totals.credit_formatted}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:a.totals.balance_formatted})]})}):null]})})]})}export{R as default};
