import{j as t,c as g,S as E,W as T}from"./vendor-mui-CBBEb17G.js";import{B as _}from"./index-BkhbTDKf.js";import{S as N}from"./index-D7-0uPSw.js";import{a as x}from"./vendor-react-BBazpZul.js";import{at as A,J as h,cJ as G,aX as D,a_ as k,aO as S,v as b,by as C,aP as y,x as R,dD as w,I as f}from"./index-IP_FWtso.js";import"./vendor-utils-BveVE6Uf.js";import"./vendor-charts-tGnAgELZ.js";const I=x.createContext(null),L=({children:s})=>{const{getPatientGroup:i,deletePatientGroup:r}=G(),n=A(),[o,d]=x.useState([]),[e,p]=x.useState(null),[m,c]=x.useState(!1),a=async()=>{try{c(!0);const{data:u}=await i();if(!u)return;d(u)}catch(u){console.log("ERROR_IN_GET_PATIENT_GROUP_LIST",u)}finally{c(!1)}},j={states:{loading:m,patientGroupList:o,selectedPatientGroup:e},handlers:{setSelectedPatientGroup:p},actions:{getPatientGroupList:a,handleDelete:async()=>{try{if(!e)return;n({content:"آیا از حذف این گروه مطمئنید؟"}).then(async()=>{const{message:u}=await r(e.id);h.success(u),p(null),a()}).catch(()=>{})}catch(u){console.log("ERROR_IN_DELETING_PATIENT_GROUP",u)}}}};return t.jsx(I.Provider,{value:j,children:s})},P=()=>x.useContext(I),O=[{key:"id",dataIndex:"id",title:"کد",width:60},{key:"name",dataIndex:"name",title:"نام",width:120},{key:"createDate",dataIndex:"createDate",title:"تاریخ ایجاد",render:s=>k(s),width:100}],v=()=>{const{states:{loading:s,patientGroupList:i},handlers:{setSelectedPatientGroup:r},actions:{getPatientGroupList:n}}=P();return x.useEffect(()=>{n()},[]),t.jsx(D,{rowKey:"id",loading:s,columns:O,data:i,onClick:o=>r(o),boxProps:{height:"80vh"}})},U=({onClose:s})=>{const{actions:{getPatientGroupList:i}}=P(),{addPatientGroup:r}=G(),{control:n,formState:{errors:o,isSubmitting:d},handleSubmit:e,reset:p}=S({defaultValues:{name:""}}),m=()=>{p(),s()},c=async a=>{try{if(!a.name.length)return h.warn("نام الزامیست");const{message:l}=await r({name:a.name});h.success(l),i(),m()}catch(l){console.log("ERROR_IN_CREATING_PATIENT_GROUP",l)}};return t.jsx(b,{title:"افزودن گروه بیمار",onClose:m,maxWidth:"xs",children:t.jsx(g,{className:"!py-4",children:t.jsxs("form",{onSubmit:e(c),children:[t.jsx(C,{control:n,name:"name",render:({field:a})=>t.jsx(y,{...a,disabled:d,label:"نام",error:!!o.name})}),t.jsx(R,{loading:d,onClose:m,onSubmit:()=>c})]})})})},B=({onClose:s})=>{const{actions:{getPatientGroupList:i}}=P(),{updatePatientGroup:r}=G(),{states:{selectedPatientGroup:n}}=P(),{control:o,formState:{errors:d,isSubmitting:e},handleSubmit:p,reset:m}=S({defaultValues:{name:(n==null?void 0:n.name)||""}}),c=()=>{m(),s()},a=async l=>{try{if(!n)return h.error("ابتدا یک گروه را انتخاب کنید");if(!l.name.length)return h.warn("نام الزامیست");const{message:j}=await r({id:n.id,name:l.name});h.success(j),i(),c()}catch(j){console.log("ERROR_IN_CREATING_PATIENT_GROUP",j)}};return t.jsx(b,{title:"ویرایش گروه بیمار",onClose:c,maxWidth:"xs",children:t.jsx(g,{className:"!py-4",children:t.jsxs("form",{onSubmit:p(a),children:[t.jsx(C,{control:o,name:"name",render:({field:l})=>t.jsx(y,{...l,disabled:e,label:"نام",error:!!d.name})}),t.jsx(R,{loading:e,onClose:c,onSubmit:()=>a})]})})})},F=()=>{const{states:{selectedPatientGroup:s},actions:{getPatientGroupList:i,handleDelete:r}}=P(),[n,o]=x.useState({add:!1,edit:!1}),d=[{title:"افزودن",icon:t.jsx(f.AddIcon,{color:"success"}),onClick:()=>o(e=>({...e,add:!0})),disabled:!1},{title:"ویرایش",icon:t.jsx(f.EditIcon,{color:s?"primary":"disabled"}),onClick:()=>o(e=>({...e,edit:!0})),disabled:!s},{title:"حذف",icon:t.jsx(f.DeleteIcon,{color:s?"error":"disabled"}),onClick:r,disabled:!s},{title:"بازنشانی",icon:t.jsx(f.RefreshIcon,{color:"primary"}),onClick:i}];return t.jsxs(t.Fragment,{children:[t.jsx(E,{display:"flex",flexDirection:"row",gap:1,children:d.map(e=>t.jsx(w,{disabled:e.disabled,onClick:e.onClick,size:"small",children:t.jsx(T,{title:t.jsx("h1",{className:"text-sm",children:e.title}),children:e.icon})}))}),n.add&&t.jsx(U,{onClose:()=>o(e=>({...e,add:!1}))}),n.edit&&t.jsx(B,{onClose:()=>o(e=>({...e,edit:!1}))})]})},X=()=>t.jsxs(N,{className:"!py-4 !px-2 !space-y-2",children:[t.jsx(_,{prevRoutes:[{path:"/",title:"داشبورد"}],currentRoute:"مدیریت گروه‌ها"}),t.jsxs(L,{children:[t.jsx(F,{}),t.jsx(v,{})]})]});export{X as PatientGroup};
