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-ChE_eana.js";import{An as a,Cr as o,Dt as s,Fr as c,Gt as l,H as u,Hr as d,Kr as f,Nr as p,Sr as m,U as h,Vt as g,Wr as _,br as v,dr as te,ii as y,kt as ne,ni as b,v as x,vn as S,wr as C,xr as w,zr as T}from"./mui-vendor-K60S-ugg.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-Cz-yne81.js";import{t as F}from"./groups.api-DGmeEYHA.js";var I=e(y(),1),L=b(),R=()=>{let e=t(),y=n(e=>e.auth.user),b=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)(s,{fontSize:`small`})},{label:`Settings`,path:`/settings`,icon:(0,L.jsx)(u,{fontSize:`small`})},{label:`Groups`,icon:(0,L.jsx)(ne,{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=>{b.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)(T,{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)(c,{children:[k(y,`change_group`)&&(0,L.jsx)(_,{size:`small`,onClick:()=>e(`/settings/groups/${t.row.id}`),title:`Edit`,children:(0,L.jsx)(g,{fontSize:`small`})}),k(y,`delete_group`)&&(0,L.jsx)(_,{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)(c,{p:3,children:(0,L.jsx)(d,{color:`error`,children:`Failed to load groups`})}):(0,L.jsxs)(c,{sx:N(),children:[(0,L.jsx)(c,{sx:P(),children:(0,L.jsxs)(c,{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)(c,{sx:{display:`flex`,alignItems:`center`,gap:2},children:[(0,L.jsx)(a,{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)(te,{position:`start`,children:(0,L.jsx)(h,{sx:{color:`#006766`}})})}}),k(y,`add_group`)&&(0,L.jsx)(p,{variant:`contained`,color:`primary`,startIcon:(0,L.jsx)(S,{}),onClick:ce,sx:{whiteSpace:`nowrap`},children:`Add New Group`})]})]})}),(0,L.jsx)(c,{sx:D(),children:(0,L.jsx)(f,{sx:{flexGrow:1,display:`flex`,flexDirection:`column`,overflow:`hidden`,minHeight:400,borderRadius:0},children:(0,L.jsx)(c,{sx:{flexGrow:1,minHeight:0},children:(0,L.jsx)(x,{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)(C,{open:K,onClose:()=>q(!1),children:[(0,L.jsx)(v,{children:`Delete Group`}),(0,L.jsx)(m,{children:(0,L.jsxs)(w,{children:[`Are you sure you want to delete the group "`,J?.name,`"? This action cannot be undone.`]})}),(0,L.jsxs)(o,{children:[(0,L.jsx)(p,{onClick:()=>{q(!1),Y(null)},children:`Cancel`}),(0,L.jsx)(p,{onClick:ue,color:`error`,disabled:Q.isPending,children:Q.isPending?`Deleting...`:`Delete`})]})]})]})};export{R as default};