import{o as e}from"./rolldown-runtime-GyhWBpbT.js";import{F as t,O as n,l as r,o as i,s as ee}from"./react-vendor-BD_NkAgt.js";import{Cr as a,Dt as o,Er as s,Fr as c,Gt as l,H as u,Jr as d,Kr as f,Lr as p,Mn as m,Sr as h,Tr as g,U as _,Vr as v,Vt as te,Wr as ne,ii as y,kt as b,oi as x,pr as S,v as C,wr as w,yn as T}from"./mui-vendor-DsoH2_ot.js";import"./state-vendor-Cw2RdylD.js";import{D as E,_ as D,ft as O,k,rt as A,v as j,w as M,x as N,y as P}from"./index-Bo9BrBsx.js";import{t as F}from"./groups.api-s8SxzVp9.js";var I=e(x(),1),L=y(),R=()=>{let e=t(),y=n(e=>e.auth.user),x=r(),{setBreadcrumbs:R}=E(),{success:z,error:re}=O();M(`Groups`);let[B,V]=(0,I.useState)({page:0,pageSize:10}),[H,U]=(0,I.useState)(``),[W,G]=(0,I.useState)(``),[K,q]=(0,I.useState)(!1),[J,Y]=(0,I.useState)(null);(0,I.useEffect)(()=>(R([{label:`Home`,path:`/`,icon:(0,L.jsx)(o,{fontSize:`small`})},{label:`Settings`,path:`/settings`,icon:(0,L.jsx)(u,{fontSize:`small`})},{label:`Groups`,icon:(0,L.jsx)(b,{fontSize:`small`})}]),()=>R([])),[R]),(0,I.useEffect)(()=>{let e=setTimeout(()=>{G(H),H!==W&&V(e=>({...e,page:0}))},500);return()=>clearTimeout(e)},[H]);let X={page:B.page+1,page_size:B.pageSize,search:W||void 0},{data:Z,isLoading:ie,isFetching:ae,error:oe}=ee({queryKey:[`groups`,X],queryFn:()=>F.list(X)}),Q=i({mutationFn:e=>F.delete(e),onSuccess:e=>{x.invalidateQueries({queryKey:[`groups`]}),z(e?.message||`Group deleted successfully`),q(!1),Y(null)},onError:e=>{re(e.response?.data?.message||`Failed to delete group`)}}),$=[{field:`sno`,headerName:`S.No`,width:70,align:`center`,headerAlign:`center`,sortable:!1,filterable:!1,renderCell:e=>{let t=Z?.results.findIndex(t=>t.id===e.row.id);return t!==void 0&&t!==-1?B.page*B.pageSize+t+1:`-`}},{field:`name`,headerName:`Group Name`,flex:1,minWidth:200},{field:`users`,headerName:`Users Count`,width:120,align:`center`,headerAlign:`center`,valueGetter:(e,t)=>t.users?.length||0},{field:`permissions`,headerName:`Permissions`,flex:1,minWidth:150,renderCell:e=>(0,L.jsx)(v,{label:`${e.row.permissions?.length||0} permissions`,size:`small`,color:`primary`,variant:`outlined`})},{field:`actions`,headerName:`Actions`,width:120,sortable:!1,filterable:!1,renderCell:t=>(0,L.jsxs)(p,{children:[k(y,`change_group`)&&(0,L.jsx)(f,{size:`small`,onClick:()=>e(`/settings/groups/${t.row.id}`),title:`Edit`,children:(0,L.jsx)(te,{fontSize:`small`})}),k(y,`delete_group`)&&(0,L.jsx)(f,{size:`small`,onClick:()=>{Y(t.row),q(!0)},title:`Delete`,color:`error`,children:(0,L.jsx)(l,{fontSize:`small`})})]})}],se=()=>{e(`/settings`)},ce=()=>{e(`/settings/groups/new`)},le=e=>{U(e.target.value)},ue=()=>{J&&Q.mutate(J.id)};return oe?(0,L.jsx)(p,{p:3,children:(0,L.jsx)(ne,{color:`error`,children:`Failed to load groups`})}):(0,L.jsxs)(p,{sx:N(),children:[(0,L.jsx)(p,{sx:P(),children:(0,L.jsxs)(p,{sx:{display:`flex`,justifyContent:`space-between`,alignItems:`center`,flexWrap:`wrap`,gap:2},children:[(0,L.jsx)(A,{title:`Groups Master`,showBackButton:!0,onBack:se,disableBox:!0}),(0,L.jsxs)(p,{sx:{display:`flex`,alignItems:`center`,gap:2},children:[(0,L.jsx)(m,{placeholder:`Search groups...`,size:`small`,value:H,onChange:le,sx:{width:280,"& .MuiOutlinedInput-root":{backgroundColor:`#fff`,borderRadius:2,"&:hover fieldset":{borderColor:`#006766`},"&.Mui-focused fieldset":{borderColor:`#006766`}}},InputProps:{startAdornment:(0,L.jsx)(S,{position:`start`,children:(0,L.jsx)(_,{sx:{color:`#006766`}})})}}),k(y,`add_group`)&&(0,L.jsx)(c,{variant:`contained`,color:`primary`,startIcon:(0,L.jsx)(T,{}),onClick:ce,sx:{whiteSpace:`nowrap`},children:`Add New Group`})]})]})}),(0,L.jsx)(p,{sx:D(),children:(0,L.jsx)(d,{sx:{flexGrow:1,display:`flex`,flexDirection:`column`,overflow:`hidden`,minHeight:400,borderRadius:0},children:(0,L.jsx)(p,{sx:{flexGrow:1,minHeight:0},children:(0,L.jsx)(C,{rows:Z?.results||[],columns:$,rowCount:Z?.count||0,loading:ie||ae,pageSizeOptions:[10,20,50,100],paginationModel:B,paginationMode:`server`,onPaginationModelChange:V,disableRowSelectionOnClick:!0,disableColumnMenu:!0,sx:j()})})})}),(0,L.jsxs)(s,{open:K,onClose:()=>q(!1),children:[(0,L.jsx)(h,{children:`Delete Group`}),(0,L.jsx)(w,{children:(0,L.jsxs)(a,{children:[`Are you sure you want to delete the group "`,J?.name,`"? This action cannot be undone.`]})}),(0,L.jsxs)(g,{children:[(0,L.jsx)(c,{onClick:()=>{q(!1),Y(null)},children:`Cancel`}),(0,L.jsx)(c,{onClick:ue,color:`error`,disabled:Q.isPending,children:Q.isPending?`Deleting...`:`Delete`})]})]})]})};export{R as default};