import{a as v,i as M,r as c,d as _,j as e}from"./index-D4ZMoJFe.js";import{M as b}from"./mgxConfig-Tx_C8Bg-.js";import{S as Y}from"./save-DeIgwxFd.js";const x={default_currency:"DZD",date_format:"DD/MM/YYYY",fuel_anomaly_threshold_pct:30,default_payment_terms:30,default_alert_days_by_document_type:{},enabled_modules:Object.fromEntries(b.map(([m])=>[m,!0]))};function k(){const{selectedCompanyId:m,user:d}=v(),{pushToast:y}=M(),i=m||(d==null?void 0:d.companyId),[l,o]=c.useState(x),[p,g]=c.useState(!0),[h,f]=c.useState(!1),[j,u]=c.useState("");c.useEffect(()=>{let a=!1;return g(!0),_.get("/mgx/settings",{params:{company_id:i}}).then(t=>{a||o({...x,...t.data.item})}).catch(t=>{var s,n;a||u(((n=(s=t==null?void 0:t.response)==null?void 0:s.data)==null?void 0:n.message)||"Impossible de charger les paramètres.")}).finally(()=>{a||g(!1)}),()=>{a=!0}},[i]);const r=(a,t)=>{o(s=>({...s,[a]:t}))},N=async a=>{var t,s;a.preventDefault(),f(!0),u("");try{const n=await _.put("/mgx/settings",{...l,company_id:i});o({...x,...n.data.item}),y({title:"Paramètres MGX enregistrés",description:"La configuration générale a été mise à jour.",tone:"success"})}catch(n){u(((s=(t=n==null?void 0:n.response)==null?void 0:t.data)==null?void 0:s.message)||"Impossible d'enregistrer.")}finally{f(!1)}};return 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:"Administration MGX"}),e.jsx("h1",{className:"mt-3 text-3xl font-semibold text-text",children:"Paramétrage Général"}),e.jsx("p",{className:"mt-3 max-w-3xl text-sm leading-7 text-textMuted",children:"Définissez les paramètres globaux du module Parc Auto & Moyens Généraux."})]}),j?e.jsx("div",{className:"rounded-2xl border border-danger/20 bg-danger/5 px-5 py-4 text-sm text-danger",children:j}):null,e.jsxs("form",{onSubmit:N,className:"card-panel p-6",children:[p?e.jsx("p",{className:"text-sm text-textMuted",children:"Chargement..."}):e.jsxs("div",{className:"grid gap-5 xl:grid-cols-2",children:[e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Devise par défaut"}),e.jsx("input",{className:"input-dark",value:l.default_currency,onChange:a=>r("default_currency",a.target.value)})]}),e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Format de date"}),e.jsxs("select",{className:"input-dark",value:l.date_format,onChange:a=>r("date_format",a.target.value),children:[e.jsx("option",{value:"DD/MM/YYYY",children:"DD/MM/YYYY"}),e.jsx("option",{value:"YYYY-MM-DD",children:"YYYY-MM-DD"})]})]}),e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Seuil anomalie consommation (%)"}),e.jsx("input",{type:"number",min:"0",className:"input-dark",value:l.fuel_anomaly_threshold_pct,onChange:a=>r("fuel_anomaly_threshold_pct",Number(a.target.value))})]}),e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Délai de paiement par défaut"}),e.jsx("input",{type:"number",min:"0",className:"input-dark",value:l.default_payment_terms,onChange:a=>r("default_payment_terms",Number(a.target.value))})]}),e.jsxs("section",{className:"xl:col-span-2",children:[e.jsx("h2",{className:"text-lg font-semibold text-text",children:"Modules actifs"}),e.jsx("div",{className:"mt-4 grid gap-3 sm:grid-cols-2 xl:grid-cols-3",children:b.map(([a,t])=>{var s;return e.jsxs("label",{className:"card-panel-elevated flex items-center gap-3 p-4",children:[e.jsx("input",{type:"checkbox",checked:!!((s=l.enabled_modules)!=null&&s[a]),onChange:n=>r("enabled_modules",{...l.enabled_modules,[a]:n.target.checked}),className:"h-4 w-4 rounded border-border text-accent focus:ring-accent"}),e.jsx("span",{className:"text-sm font-medium text-text",children:t})]},a)})})]})]}),e.jsx("div",{className:"mt-6 flex justify-end",children:e.jsxs("button",{type:"submit",className:"btn-primary inline-flex items-center gap-2",disabled:h||p,children:[e.jsx(Y,{size:16}),h?"Enregistrement...":"Enregistrer"]})})]})]})}export{k as default};
