import{r,j as e}from"./vendor-react-9M-xu2bR.js";import{c as w}from"./infrastructure-n1j_TN6w.js";import{B as x,C as b,v as y,G as v}from"./ui-components-BxvihT61.js";import"./framework-BM176iet.js";import{b as i}from"./vendor-ui-utils-3tkeUVaX.js";import{a as C}from"./index-CTBPkbPG.js";import{s as k}from"./systemObjects-DSpePIIy.js";import{b9 as h,m as R,L as S,l as _}from"./vendor-icons-ndICq8DC.js";import"./vendor-i18n-E3-WitRV.js";import"./vendor-radix-C6qchjxv.js";import"./vendor-charts-BLNOCZWl.js";import"./vendor-objectstack-YKtcdalX.js";import"./vendor-zod-DxQVy2_o.js";import"./plugins-core-qwgXbAqa.js";import"./vendor-dndkit-CX1oDZqy.js";import"./plugins-views-CveFN6PV.js";import"./data-adapter-rvE3xy2B.js";import"./ui-layout-C6KWQtRO.js";const p=k.find(c=>c.name==="sys_role"),o=p.views[0].columns;function Q(){const{user:c}=w(),l=c?.role==="admin",t=C(),[m,u]=r.useState([]),[f,d]=r.useState(!0),a=r.useCallback(async()=>{if(t){d(!0);try{const s=await t.find("sys_role");u(s.data||[])}catch{i.error("Failed to load roles")}finally{d(!1)}}},[t]);r.useEffect(()=>{a()},[a]);const j=r.useCallback(async()=>{if(t)try{await t.create("sys_role",{name:"New Role",description:"",isSystem:!1}),i.success("Role created"),a()}catch{i.error("Failed to create role")}},[t,a]),g=r.useCallback(async s=>{if(t)try{await t.delete("sys_role",s),i.success("Role deleted"),a()}catch{i.error("Failed to delete role")}},[t,a]);return e.jsxs("div",{className:"flex flex-col gap-4 sm:gap-6 p-4 sm:p-6",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3",children:[e.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[e.jsx("div",{className:"bg-primary/10 p-2 rounded-md shrink-0",children:e.jsx(h,{className:"h-5 w-5 text-primary"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("h1",{className:"text-xl sm:text-2xl font-bold tracking-tight",children:"Role Management"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-0.5",children:"Define roles and assign permissions"})]})]}),l&&e.jsxs(x,{size:"sm",className:"shrink-0 gap-2",onClick:j,children:[e.jsx(R,{className:"h-4 w-4"}),"Add Role"]})]}),e.jsx(b,{children:e.jsx(y,{className:"p-0",children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-muted/50",children:[o.map(s=>{const n=p.fields.find(N=>N.name===s);return e.jsx("th",{className:"h-10 px-3 sm:px-4 text-left font-medium text-muted-foreground whitespace-nowrap",children:n?.label??s},s)}),l&&e.jsx("th",{className:"h-10 px-3 sm:px-4 text-right font-medium text-muted-foreground",children:"Actions"})]})}),e.jsx("tbody",{children:f?e.jsx("tr",{children:e.jsx("td",{className:"p-6 text-center",colSpan:o.length+(l?1:0),children:e.jsx(S,{className:"h-6 w-6 animate-spin mx-auto text-muted-foreground"})})}):m.length===0?e.jsx("tr",{children:e.jsx("td",{className:"p-4 sm:p-6 text-center text-sm text-muted-foreground",colSpan:o.length+(l?1:0),children:e.jsxs("div",{className:"flex flex-col items-center gap-2 py-4",children:[e.jsx(h,{className:"h-8 w-8 text-muted-foreground/50"}),e.jsx("p",{children:"No roles found."}),e.jsx(v,{variant:"secondary",className:"text-xs",children:"plugin-grid powered"})]})})}):m.map(s=>e.jsxs("tr",{className:"border-b hover:bg-muted/50 transition-colors",children:[o.map(n=>e.jsx("td",{className:"h-10 px-3 sm:px-4 whitespace-nowrap",children:String(s[n]??"")},n)),l&&e.jsx("td",{className:"h-10 px-3 sm:px-4 text-right",children:e.jsx(x,{variant:"ghost",size:"icon",className:"h-7 w-7",onClick:()=>g(s.id||s._id),children:e.jsx(_,{className:"h-3.5 w-3.5 text-destructive"})})})]},s.id||s._id))})]})})})})]})}export{Q as RoleManagementPage};
