import{i as M,r as l,j as e,p as C,d as y}from"./index-D4ZMoJFe.js";import{A as P}from"./AdminSubnav-B96tphNW.js";import{P as k}from"./PermissionMatrix-BNfbE3dY.js";import{b as w,e as E}from"./adminUtils-Qmlne9KJ.js";import{E as R}from"./eye-BauUd1OG.js";import{P as A}from"./pencil-UDXK7qaT.js";function $(){var g;const{pushToast:d}=M(),[f,o]=l.useState(!0),[x,m]=l.useState(!1),[p,u]=l.useState(""),[h,N]=l.useState([]),[a,i]=l.useState({open:!1,readOnly:!0,role:null,gridState:[]}),b=async()=>{var s,r;o(!0),u("");try{const t=await y.get("/roles");N(t.data.items||[])}catch(t){u(((r=(s=t==null?void 0:t.response)==null?void 0:s.data)==null?void 0:r.message)||"Impossible de charger les rôles.")}finally{o(!1)}};l.useEffect(()=>{b()},[]);const j=(s,r)=>{i({open:!0,readOnly:r,role:s,gridState:w(s.permissions)})},c=()=>{i({open:!1,readOnly:!0,role:null,gridState:[]})},S=(s,r)=>{i(t=>({...t,gridState:t.gridState.map(n=>n.key===s?{...n,cells:{...n.cells,[r]:!n.cells[r]}}:n)}))},v=async()=>{var s,r;if(a.role){m(!0);try{await y.put(`/roles/${a.role.id}`,{permissions:E(a.gridState)}),d({title:"Rôle mis à jour",description:a.role.label,tone:"success"}),c(),await b()}catch(t){d({title:"Enregistrement impossible",description:((r=(s=t==null?void 0:t.response)==null?void 0:s.data)==null?void 0:r.message)||"La mise à jour des permissions a échoué.",tone:"danger"})}finally{m(!1)}}};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("section",{className:"card-panel p-6 sm:p-8",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.2em] text-accent",children:"Administration"}),e.jsx("h1",{className:"mt-3 text-3xl font-semibold text-text",children:"Rôles"}),e.jsx("p",{className:"mt-3 max-w-3xl text-sm leading-7 text-textMuted",children:"Contrôlez les droits par module via une matrice de permissions centralisée."})]}),e.jsx("div",{className:"mt-6",children:e.jsx(P,{})})]}),p?e.jsx("section",{className:"rounded-2xl border border-danger/20 bg-danger/5 px-5 py-4 text-sm text-danger",children:p}):null,e.jsx("section",{className:"table-shell overflow-x-auto",children:e.jsxs("table",{className:"min-w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"bg-surface2 text-left text-xs uppercase tracking-[0.16em] text-textMuted",children:[e.jsx("th",{className:"px-4 py-3",children:"Rôle"}),e.jsx("th",{className:"px-4 py-3",children:"Utilisateurs"}),e.jsx("th",{className:"px-4 py-3",children:"Type"}),e.jsx("th",{className:"px-4 py-3 text-right",children:"Actions"})]})}),e.jsx("tbody",{children:f?e.jsx("tr",{className:"border-t border-border/70",children:e.jsx("td",{colSpan:4,className:"px-4 py-10 text-center text-textMuted",children:"Chargement des rôles..."})}):h.length?h.map(s=>e.jsxs("tr",{className:"border-t border-border/70 text-text",children:[e.jsxs("td",{className:"px-4 py-4",children:[e.jsx("div",{className:"font-medium",children:s.label}),e.jsx("div",{className:"mt-1 text-xs text-textMuted",children:s.name})]}),e.jsx("td",{className:"px-4 py-4",children:s.users_count}),e.jsx("td",{className:"px-4 py-4",children:e.jsx("span",{className:`status-pill ${s.visible?"border-accent/30 bg-accent/10 text-accent":"border-border bg-surface2 text-textMuted"}`,children:s.visible?"SCF":"Legacy"})}),e.jsx("td",{className:"px-4 py-4",children:e.jsxs("div",{className:"flex justify-end gap-2",children:[e.jsxs("button",{type:"button",className:"btn-secondary inline-flex items-center gap-2 px-3 py-2",onClick:()=>j(s,!0),children:[e.jsx(R,{size:15}),"Voir permissions"]}),e.jsxs("button",{type:"button",className:"btn-primary inline-flex items-center gap-2 px-3 py-2",onClick:()=>j(s,!1),children:[e.jsx(A,{size:15}),"Modifier"]})]})})]},s.id)):e.jsx("tr",{className:"border-t border-border/70",children:e.jsx("td",{colSpan:4,className:"px-4 py-10 text-center text-textMuted",children:"Aucun rôle disponible."})})})]})}),e.jsxs(C,{open:a.open,onClose:c,title:((g=a.role)==null?void 0:g.label)||"Permissions",subtitle:a.readOnly?"Consultation":"Modification",maxWidthClassName:"max-w-6xl",children:[e.jsx("div",{className:"p-6",children:e.jsx(k,{gridState:a.gridState,readOnly:a.readOnly,onToggle:S})}),e.jsxs("div",{className:"flex justify-end gap-3 border-t border-border px-6 py-4",children:[e.jsx("button",{type:"button",className:"btn-secondary",onClick:c,children:"Fermer"}),a.readOnly?null:e.jsx("button",{type:"button",className:"btn-primary",onClick:v,disabled:x,children:x?"Enregistrement...":"Enregistrer"})]})]})]})}export{$ as default};
