import{a as T,a4 as A,r as a,j as e,L as _,P as E,s as L,d as p}from"./index-D4ZMoJFe.js";import{f as z,a as F}from"./formatters-jmx2pV9o.js";import{g as O,p as V}from"./mgxDepotFilter-Vtxq-a-5.js";const m=new Date,G=new Date(m.getFullYear(),m.getMonth(),1).toISOString().slice(0,10),X=m.toISOString().slice(0,10);function H(){var v;const{selectedCompanyId:f,user:r}=T(),i=f||(r==null?void 0:r.companyId)||((v=r==null?void 0:r.company)==null?void 0:v.id)||"",N=A("service_type"),[t,b]=a.useState({from:G,to:X,supplier_id:"",service_type_ref_id:"",vehicle_id:"",depot_id:O()}),[x,y]=a.useState([]),[S,w]=a.useState([]),[I,D]=a.useState([]),[k,C]=a.useState([]),[M,u]=a.useState(!0),[h,j]=a.useState(""),g=a.useMemo(()=>({company_id:i,...t}),[i,t]);a.useEffect(()=>{(async()=>{var n,c;if(i){u(!0),j("");try{const[l,o,P,R]=await Promise.all([p.get("/mgx/services",{params:g}),p.get("/mgx/suppliers",{params:{company_id:i}}),p.get("/mgx/vehicles",{params:{company_id:i}}),p.get("/mgx/depots",{params:{company_id:i}})]);y(l.data.items||[]),w(o.data.items||[]),D(P.data.items||[]),C(R.data.items||[])}catch(l){j(((c=(n=l==null?void 0:l.response)==null?void 0:n.data)==null?void 0:c.message)||"Impossible de charger les prestations.")}finally{u(!1)}}})()},[i,g]),a.useEffect(()=>{V(t.depot_id)},[t.depot_id]);const d=(s,n)=>b(c=>({...c,[s]:n}));return e.jsxs("div",{className:"space-y-6",children:[e.jsx("section",{className:"card-panel p-6 sm:p-8",children:e.jsxs("div",{className:"flex flex-col gap-5 xl:flex-row xl:items-end xl:justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.2em] text-gold",children:"MGX · Prestations"}),e.jsx("h1",{className:"mt-3 text-3xl font-semibold text-text",children:"Prestations de Service"})]}),e.jsxs(_,{to:"/mgx/services/new",className:"btn-primary inline-flex items-center gap-2",children:[e.jsx(E,{size:16}),"Nouvelle prestation"]})]})}),e.jsx("section",{className:"card-panel p-5",children:e.jsxs("div",{className:"grid gap-3 xl:grid-cols-[150px_150px_minmax(0,1fr)_180px_180px_180px]",children:[e.jsx("input",{type:"date",className:"input-dark",value:t.from,onChange:s=>d("from",s.target.value)}),e.jsx("input",{type:"date",className:"input-dark",value:t.to,onChange:s=>d("to",s.target.value)}),e.jsxs("select",{className:"input-dark",value:t.supplier_id,onChange:s=>d("supplier_id",s.target.value),children:[e.jsx("option",{value:"",children:"Tous prestataires"}),S.map(s=>e.jsx("option",{value:s.id,children:s.name},s.id))]}),e.jsxs("select",{className:"input-dark",value:t.service_type_ref_id,onChange:s=>d("service_type_ref_id",s.target.value),children:[e.jsx("option",{value:"",children:"Tous types"}),N.map(s=>e.jsx("option",{value:s.id,children:s.label},s.id))]}),e.jsxs("select",{className:"input-dark",value:t.vehicle_id,onChange:s=>d("vehicle_id",s.target.value),children:[e.jsx("option",{value:"",children:"Tous véhicules"}),I.map(s=>e.jsx("option",{value:s.id,children:s.plate_number},s.id))]}),e.jsxs("select",{className:"input-dark",value:t.depot_id,onChange:s=>d("depot_id",s.target.value),children:[e.jsx("option",{value:"",children:"Tous dépôts"}),k.map(s=>e.jsx("option",{value:s.id,children:s.label},s.id))]})]})}),h?e.jsx("div",{className:"rounded-2xl border border-danger/20 bg-danger/5 p-4 text-sm text-danger",children:h}):null,e.jsx("section",{className:"table-shell overflow-x-auto",children:e.jsxs("table",{className:"min-w-[1120px] w-full",children:[e.jsx("thead",{className:"table-head",children:e.jsx("tr",{children:["Date","Prestataire","Type","Véhicule","Dépôt","Montant","N° facture","Statut paiement","Actions"].map(s=>e.jsx("th",{className:"table-cell",children:s},s))})}),e.jsx("tbody",{children:M?e.jsx("tr",{children:e.jsx("td",{colSpan:9,className:"table-cell text-textMuted",children:"Chargement..."})}):x.length?x.map(s=>{var n,c,l,o;return e.jsxs("tr",{className:"table-row",children:[e.jsx("td",{className:"table-cell",children:z(s.service_date)}),e.jsx("td",{className:"table-cell",children:((n=s.supplier)==null?void 0:n.name)||"—"}),e.jsx("td",{className:"table-cell",children:((c=s.service_type)==null?void 0:c.label)||"—"}),e.jsx("td",{className:"table-cell",children:((l=s.vehicle)==null?void 0:l.plate_number)||"—"}),e.jsx("td",{className:"table-cell",children:((o=s.depot)==null?void 0:o.label)||"—"}),e.jsx("td",{className:"table-cell amount-cell font-semibold",children:F(s.amount)}),e.jsx("td",{className:"table-cell",children:s.invoice_number||"—"}),e.jsx("td",{className:"table-cell",children:s.payment_status||"—"}),e.jsx("td",{className:"table-cell",children:e.jsx(_,{to:`/mgx/services/${s.id}/edit`,className:"btn-secondary px-3 py-2",children:e.jsx(L,{size:15})})})]},s.id)}):e.jsx("tr",{children:e.jsx("td",{colSpan:9,className:"table-cell text-textMuted",children:"Aucune prestation trouvée."})})})]})})]})}export{H as default};
