import{e as S,a as B,ab as U,q as X,i as V,r as c,j as e,P as Y,s as H,X as J,d}from"./index-D4ZMoJFe.js";import{a as K,b as Q}from"./mgxConfig-Tx_C8Bg-.js";import{S as W}from"./sparkles-BDTPTecE.js";import{T as Z}from"./trash-2-CGotPSCR.js";import{S as ee}from"./save-DeIgwxFd.js";/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const te=S("ArrowDown",[["path",{d:"M12 5v14",key:"s699le"}],["path",{d:"m19 12-7 7-7-7",key:"1idqje"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const se=S("ArrowUp",[["path",{d:"m5 12 7-7 7 7",key:"hav0vg"}],["path",{d:"M12 19V5",key:"x0mq9r"}]]),k={code:"",label:"",description:"",color:"#3B82F6",icon:"",sort_order:0,is_active:!0,metadata:{}},ae={fuel_type:[["unit","Unité"]],document_type:[["alert_days","Délai d'alerte par défaut (jours)","number"]],maintenance_type:[["frequency_km","Fréquence par défaut (km)","number"],["frequency_days","Fréquence par défaut (jours)","number"]],unit_of_measure:[["symbol","Symbole"]],alert_type:[["trigger_entity","Entité déclencheur"],["trigger_field","Champ"],["threshold_unit","Unité"]]};function oe(){const{selectedCompanyId:M,user:g}=B(),{refresh:p}=U(),A=X(),{pushToast:u}=V(),i=M||(g==null?void 0:g.companyId),[o,E]=c.useState("vehicle_category"),[y,I]=c.useState([]),[R,z]=c.useState({}),[F,v]=c.useState(!0),[r,n]=c.useState(k),[h,_]=c.useState(null),[C,b]=c.useState(""),L=K[o]||o,$=ae[o]||[],m=async()=>{var t,s;v(!0),b("");try{const[a,l]=await Promise.all([d.get("/mgx/referentiels",{params:{company_id:i,category:o,include_inactive:!0}}),d.get("/mgx/referentiels/all",{params:{company_id:i}})]);I(a.data.items||[]);const x={};Object.entries(l.data.items||{}).forEach(([f,P])=>{x[f]=P.filter(T=>T.is_active).length}),z(x)}catch(a){b(((s=(t=a==null?void 0:a.response)==null?void 0:t.data)==null?void 0:s.message)||"Impossible de charger les référentiels.")}finally{v(!1)}};c.useEffect(()=>{m()},[i,o]);const N=c.useMemo(()=>[...y].sort((t,s)=>t.sort_order-s.sort_order||t.label.localeCompare(s.label,"fr")),[y]),j=()=>{_(null),n({...k,sort_order:y.length+1})},D=t=>{_(t.id),n({code:t.code||"",label:t.label||"",description:t.description||"",color:t.color||"#3B82F6",icon:t.icon||"",sort_order:t.sort_order||0,is_active:t.is_active,metadata:t.metadata||{}})},q=async t=>{var s,a;t.preventDefault(),b("");try{const l={...r,category:o,company_id:i};h?await d.put(`/mgx/referentiels/${h}`,l):await d.post("/mgx/referentiels",l),u({title:"Référentiel enregistré",tone:"success"}),j(),await m(),await p()}catch(l){b(((a=(s=l==null?void 0:l.response)==null?void 0:s.data)==null?void 0:a.message)||"Impossible d'enregistrer.")}},G=async t=>{const s=await d.get(`/mgx/referentiels/${t.id}/usage-count`,{params:{company_id:i}}).then(l=>Number(l.data.usage_count||0)).catch(()=>0);if(s>0){u({title:"Suppression impossible",description:`${s} enregistrement(s) utilisent cet élément.`,tone:"danger"});return}await A({title:"Supprimer l'élément",description:`Confirmer la suppression de ${t.label} ?`,confirmLabel:"Supprimer"})&&(await d.delete(`/mgx/referentiels/${t.id}`,{data:{company_id:i}}),u({title:"Élément supprimé",tone:"success"}),await m(),await p())},O=async()=>{const t=await d.post(`/mgx/referentiels/${o}/seed`,{company_id:i});u({title:"Exemples chargés",description:`${t.data.inserted||0} élément(s) ajouté(s).`,tone:"success"}),await m(),await p()},w=async(t,s)=>{await d.put(`/mgx/referentiels/${t.id}`,{...t,company_id:i,sort_order:Math.max(0,Number(t.sort_order||0)+s)}),await m(),await p()};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:"Référentiels Dynamiques"}),e.jsx("p",{className:"mt-3 max-w-4xl text-sm leading-7 text-textMuted",children:"Tous les libellés métier MGX sont configurables ici. Les exemples sont modifiables et supprimables."})]}),C?e.jsx("div",{className:"rounded-2xl border border-danger/20 bg-danger/5 px-5 py-4 text-sm text-danger",children:C}):null,e.jsxs("section",{className:"grid gap-6 xl:grid-cols-[0.35fr_0.65fr]",children:[e.jsx("aside",{className:"card-panel p-4",children:e.jsx("div",{className:"space-y-4",children:Q.map(t=>e.jsxs("div",{children:[e.jsxs("p",{className:"mb-2 px-2 text-xs font-semibold uppercase tracking-[0.18em] text-textMuted",children:[t.icon," ",t.title]}),e.jsx("div",{className:"space-y-1",children:t.items.map(([s,a])=>e.jsxs("button",{type:"button",className:`flex w-full items-center justify-between rounded-xl px-3 py-2 text-left text-sm transition ${o===s?"bg-accent/10 font-semibold text-accent":"text-textMuted hover:bg-surface2 hover:text-text"}`,onClick:()=>{E(s),j()},children:[e.jsx("span",{children:a}),e.jsx("span",{className:"rounded-full border border-border bg-surface px-2 py-0.5 font-mono text-xs",children:R[s]||0})]},s))})]},t.title))})}),e.jsxs("main",{className:"space-y-6",children:[e.jsxs("section",{className:"card-panel p-5",children:[e.jsxs("div",{className:"flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-2xl font-semibold text-text",children:L}),e.jsxs("p",{className:"mt-1 text-sm text-textMuted",children:[N.length," élément(s)"]})]}),e.jsxs("div",{className:"flex flex-wrap gap-2",children:[e.jsxs("button",{type:"button",className:"btn-secondary inline-flex items-center gap-2",onClick:O,children:[e.jsx(W,{size:16}),"Charger des exemples"]}),e.jsxs("button",{type:"button",className:"btn-primary inline-flex items-center gap-2",onClick:j,children:[e.jsx(Y,{size:16}),"Ajouter"]})]})]}),e.jsx("div",{className:"mt-5 table-shell overflow-x-auto",children:e.jsxs("table",{className:"min-w-[920px] w-full",children:[e.jsx("thead",{className:"table-head",children:e.jsx("tr",{children:["Code","Libellé","Description","Couleur","Ordre","Actif","Actions"].map(t=>e.jsx("th",{className:"table-cell",children:t},t))})}),e.jsx("tbody",{children:F?e.jsx("tr",{children:e.jsx("td",{className:"table-cell text-textMuted",colSpan:7,children:"Chargement..."})}):N.length?N.map(t=>e.jsxs("tr",{className:"table-row",children:[e.jsx("td",{className:"table-cell font-mono font-semibold",children:t.code||"—"}),e.jsx("td",{className:"table-cell text-text",children:t.label}),e.jsx("td",{className:"table-cell text-textMuted",children:t.description||"—"}),e.jsx("td",{className:"table-cell",children:e.jsxs("span",{className:"inline-flex items-center gap-2",children:[e.jsx("span",{className:"h-4 w-4 rounded-full border border-border",style:{backgroundColor:t.color||"#CBD5E1"}}),t.color||"—"]})}),e.jsx("td",{className:"table-cell amount-cell font-mono",children:t.sort_order}),e.jsx("td",{className:"table-cell",children:t.is_active?"Oui":"Non"}),e.jsx("td",{className:"table-cell",children:e.jsxs("div",{className:"flex gap-2",children:[e.jsx("button",{type:"button",className:"btn-secondary px-3 py-2",onClick:()=>w(t,-1),children:e.jsx(se,{size:14})}),e.jsx("button",{type:"button",className:"btn-secondary px-3 py-2",onClick:()=>w(t,1),children:e.jsx(te,{size:14})}),e.jsx("button",{type:"button",className:"btn-secondary px-3 py-2",onClick:()=>D(t),children:e.jsx(H,{size:14})}),e.jsx("button",{type:"button",className:"btn-secondary px-3 py-2 text-danger",onClick:()=>G(t),children:e.jsx(Z,{size:14})})]})})]},t.id)):e.jsx("tr",{children:e.jsx("td",{className:"table-cell text-textMuted",colSpan:7,children:"Aucun élément. Chargez des exemples ou ajoutez le premier élément."})})})]})})]}),e.jsxs("form",{onSubmit:q,className:"card-panel p-5",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h3",{className:"text-xl font-semibold text-text",children:h?"Modifier l'élément":"Ajouter un élément"}),h?e.jsx("button",{type:"button",className:"btn-secondary px-3 py-2",onClick:j,children:e.jsx(J,{size:16})}):null]}),e.jsxs("div",{className:"mt-5 grid gap-4 lg:grid-cols-2",children:[e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Code"}),e.jsx("input",{className:"input-dark",value:r.code,onChange:t=>n(s=>({...s,code:t.target.value}))})]}),e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Libellé"}),e.jsx("input",{required:!0,className:"input-dark",value:r.label,onChange:t=>n(s=>({...s,label:t.target.value}))})]}),e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Couleur"}),e.jsx("input",{type:"color",className:"h-11 w-full rounded-xl border border-border bg-surface p-2",value:r.color,onChange:t=>n(s=>({...s,color:t.target.value}))})]}),e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Icône lucide"}),e.jsx("input",{className:"input-dark",value:r.icon,onChange:t=>n(s=>({...s,icon:t.target.value}))})]}),e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Ordre"}),e.jsx("input",{type:"number",className:"input-dark",value:r.sort_order,onChange:t=>n(s=>({...s,sort_order:Number(t.target.value)}))})]}),e.jsxs("label",{className:"flex items-center gap-3 pt-7",children:[e.jsx("input",{type:"checkbox",checked:r.is_active,onChange:t=>n(s=>({...s,is_active:t.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:"Actif"})]}),e.jsxs("label",{className:"space-y-2 lg:col-span-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:"Description"}),e.jsx("textarea",{className:"input-dark min-h-20",value:r.description,onChange:t=>n(s=>({...s,description:t.target.value}))})]}),$.map(([t,s,a="text"])=>{var l;return e.jsxs("label",{className:"space-y-2",children:[e.jsx("span",{className:"text-sm font-medium text-text",children:s}),e.jsx("input",{type:a,className:"input-dark",value:((l=r.metadata)==null?void 0:l[t])||"",onChange:x=>n(f=>({...f,metadata:{...f.metadata,[t]:a==="number"?Number(x.target.value):x.target.value}}))})]},t)})]}),e.jsxs("button",{type:"submit",className:"btn-primary mt-6 inline-flex w-full items-center justify-center gap-2",children:[e.jsx(ee,{size:16}),"Enregistrer"]})]})]})]})]})}export{oe as default};
