import{j as t,c as D,S as f,W as A,B as v}from"./vendor-mui-CBBEb17G.js";import{B as I}from"./index-DQbmBtLN.js";import{a as m}from"./vendor-react-BBazpZul.js";import{aO as B,v as N,by as U,aP as E,x as R,at as T,aQ as F,aX as O,I as u,z as S,J as j}from"./index-DdWwwZXu.js";import{S as P}from"./index-BevckEBv.js";import{S as W}from"./index-CDArk_1r.js";import"./vendor-utils-BveVE6Uf.js";import"./vendor-charts-tGnAgELZ.js";const z=({onClose:n,onSubmit:x,selectedItem:i,loading:a})=>{const{handleSubmit:h,control:o,formState:{errors:p}}=B({defaultValues:{name:(i==null?void 0:i.name)??""}}),y=s=>{x({name:s.name})};return t.jsxs(N,{title:i?"ویرایش واحد":"افزودن واحد جدید",maxWidth:"xs",onClose:n,children:[t.jsx(D,{className:"m-1 !p-2",children:t.jsx(f,{component:"form",children:t.jsx(U,{name:"name",control:o,defaultValue:"",rules:{required:!0},render:({field:{onChange:s,value:r}})=>t.jsx(E,{label:"عنوان",fullWidth:!0,error:!!p.name,className:"!mt-1",value:r,onChange:s})})})}),t.jsx(R,{onClose:n,onSubmit:h(y),loading:a})]})},K=()=>{const n=new P,x=T(),[i,a]=m.useState(!1),[h,o]=m.useState(!1),[p,y]=m.useState([]),[s,r]=m.useState(null),c=async()=>{try{a(!0),r(null);const{data:e}=await n.getAsync();e&&y(e)}finally{a(!1)}},g=async e=>{try{a(!0),await n.toggledAsync(e),j.success("تغییر وضعیت با موفقیت انجام شد"),c()}finally{a(!1)}},b=async e=>{try{a(!0);let l=null;if(s){const{message:d}=await n.updateAsync({id:s.id,name:e.name});l=d}else{const{message:d}=await n.addAsync({name:e.name});l=d}l&&j.success(l),o(!1),c()}finally{a(!1)}},k=async()=>{try{if(a(!0),!s)return;x({content:"آیا از حذف این واحد اطمینان دارید؟"}).then(async()=>{const{message:e}=await n.deleteAsync(s.id);j.success(e),c()}).catch(()=>{})}finally{a(!1)}};m.useEffect(()=>{c()},[]);const w=[{title:"ردیف",key:"num",width:70,render:(e,l,d)=>S(d+1)},{title:"شناسه",key:"id",dataIndex:"id",width:70,render:e=>S(e)},{title:"عنوان",key:"name",dataIndex:"name",width:200},{title:"وضعیت",key:"isDelete",dataIndex:"isDelete",width:70,render:(e,l)=>t.jsx(v,{variant:"contained",size:"small",color:e?"error":"success",onClick:()=>g(l.id),children:e?"غیر فعال":"فعال"})}],C=[{id:1,title:"جدید",icons:t.jsx(u.AddIcon,{color:"success"}),disabled:!1,onClick:()=>o(!0)},{id:2,title:"ویرایش",icons:t.jsx(u.EditIcon,{color:s?"primary":"inherit"}),disabled:!s,onClick:()=>o(!0)},{id:3,title:"حذف",icons:t.jsx(u.DeleteIcon,{color:s?"error":"inherit"}),disabled:!s,onClick:()=>k()},{id:4,title:"بازیابی",icons:t.jsx(u.RefreshIcon,{color:"info"}),disabled:!1,onClick:()=>c()}];return t.jsxs(f,{spacing:1,p:1,children:[t.jsx(f,{direction:"row",alignItems:"center",spacing:1,children:C.map(e=>t.jsx(A,{title:e.title,children:t.jsx(F,{onClick:e.onClick,disabled:e.disabled,children:e.icons})},e.id))}),t.jsx(f,{className:"!h-[80dvh]",children:t.jsx(O,{columns:w,data:p,rowKey:"id",loading:i,onClick:e=>r(e)})}),h&&t.jsx(z,{selectedItem:s,onSubmit:b,loading:i,onClose:()=>{o(!1),r(null)}})]})},H=()=>t.jsxs(W,{className:"!px-4 !py-2 !space-y-2",children:[t.jsx(I,{prevRoutes:[{path:"/",title:"داشبورد"},{path:"/",title:"اطلاعات پایه"}],currentRoute:"واحد خدمات"}),t.jsx(K,{})]});export{H as default};
