import{r as l,j as e}from"./vendor-react-CGAml-me.js";import{u as R,B as o,p as E,q as H,r as I,s as K,L as h,I as D,S as L,k as O,l as z,m as U,n as q,t as V,d as j,e as p}from"./index-BoGb7ncW.js";import{B as A}from"./badge-DpPviVrw.js";import{T as J,a as M,b as g,c as f,d as $,e as d,C as G}from"./table-DFNexYWN.js";import{a as W,u as X,b as k}from"./vendor-query-BbgYCJYB.js";import{u as Y}from"./vendor-i18n-DzUOdQ2j.js";import{J as Z,O as _}from"./vendor-icons-JKgav82F.js";import"./vendor-radix-Hvp89ZoW.js";function oe(){const{t:a}=Y(),n=R(s=>s.user),v=W(),{data:r,isLoading:B}=X({queryKey:["users"],queryFn:()=>p.get("/users").then(s=>s.data)}),y=k({mutationFn:s=>p.post("/users",s).then(t=>t.data),onSuccess:()=>{v.invalidateQueries({queryKey:["users"]}),j.success(a("users.created")),i(!1)}}),F=k({mutationFn:s=>p.delete(`/users/${s}`).then(t=>t.data),onSuccess:()=>{v.invalidateQueries({queryKey:["users"]}),j.success(a("users.deleted"))},onError:s=>{var T,b;const t=(b=(T=s==null?void 0:s.response)==null?void 0:T.data)==null?void 0:b.detail;j.error(t??a("common.error"))}}),[P,i]=l.useState(!1),[c,N]=l.useState(""),[u,C]=l.useState(""),[w,S]=l.useState("user"),[m,x]=l.useState(null),Q=()=>{y.mutate({username:c,password:u,role:w})};return e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"flex justify-end",children:e.jsxs(o,{size:"sm",onClick:()=>{N(""),C(""),S("user"),i(!0)},children:[e.jsx(Z,{className:"mr-2 h-4 w-4"}),a("users.add")]})}),e.jsx("div",{className:"rounded-md border",children:e.jsxs(J,{children:[e.jsx(M,{children:e.jsxs(g,{children:[e.jsx(f,{children:a("users.username")}),e.jsx(f,{children:a("users.role")}),e.jsx(f,{className:"w-20 text-right",children:a("common.actions")})]})}),e.jsxs($,{children:[r==null?void 0:r.map(s=>e.jsxs(g,{children:[e.jsxs(d,{className:"font-mono font-medium",children:[s.username,s.id===(n==null?void 0:n.id)&&e.jsx("span",{className:"ml-2 text-xs text-muted-foreground",children:"(you)"})]}),e.jsx(d,{children:e.jsx(A,{variant:s.role==="admin"?"default":"secondary",children:s.role==="admin"?a("users.admin"):a("users.user")})}),e.jsx(d,{className:"text-right",children:s.id!==(n==null?void 0:n.id)&&e.jsx(o,{size:"icon",variant:"ghost",className:"h-8 w-8 text-destructive",onClick:()=>x(s.id),children:e.jsx(_,{className:"h-4 w-4"})})})]},s.id)),(!r||r.length===0)&&!B&&e.jsx(g,{children:e.jsx(d,{colSpan:3,className:"text-center text-muted-foreground",children:a("common.noData")})})]})]})}),e.jsx(E,{open:P,onOpenChange:i,children:e.jsxs(H,{className:"sm:max-w-sm",children:[e.jsx(I,{children:e.jsx(K,{children:a("users.add")})}),e.jsxs("div",{className:"space-y-3 py-2",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(h,{children:a("users.username")}),e.jsx(D,{value:c,onChange:s=>N(s.target.value)})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(h,{children:a("auth.password")}),e.jsx(D,{type:"password",value:u,onChange:s=>C(s.target.value)})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(h,{children:a("users.role")}),e.jsxs(L,{value:w,onValueChange:s=>S(s),children:[e.jsx(O,{children:e.jsx(z,{})}),e.jsxs(U,{children:[e.jsx(q,{value:"user",children:a("users.user")}),e.jsx(q,{value:"admin",children:a("users.admin")})]})]})]})]}),e.jsxs(V,{children:[e.jsx(o,{variant:"outline",onClick:()=>i(!1),children:a("common.cancel")}),e.jsx(o,{onClick:Q,disabled:!c||!u||y.isPending,children:a("common.add")})]})]})}),e.jsx(G,{open:!!m,title:a("users.delete"),description:a("users.deleteConfirm"),destructive:!0,onConfirm:()=>{m&&F.mutate(m),x(null)},onCancel:()=>x(null)})]})}export{oe as default};
