import{M as R,S as A,j as e,c as F,o as O,W as ne}from"./vendor-mui-CBBEb17G.js";import{al as U,e as L,fH as V,u as re,at as ce,aO as ie,Z as oe,n as _,dD as B,I as g,v as $,by as z,aP as q,r as le,x as de,g as H,J as b,aX as P,fJ as ue,a$ as pe,gE as me,c$ as xe,ao as K}from"./index-IP_FWtso.js";import{a as i}from"./vendor-react-BBazpZul.js";import{a as M}from"./vendor-utils-BveVE6Uf.js";import{B as fe}from"./index-BkhbTDKf.js";import{T as he}from"./index-Dz6vPtCj.js";import"./vendor-charts-tGnAgELZ.js";const ye=U(R)`
  box-shadow: 0 3px 20px rgb(0 0 0 / 4%);
  background-color: rgb(255, 255, 255);
  padding: 10px 20px 10px;
  min-height: 100%;

  .MuiDataGrid-cell {
    cursor: pointer !important;
  }
`;U(A)`
  margin-bottom: 0;

  .btc-icons {
    background: rgba(51, 83, 178, 0.04);
    min-width: 49px;
    margin: 0 5px;
  }
`;const X=i.createContext(void 0),De=({children:m})=>{const{GetDepartmentsReq:a}=V(),{clinicId:t,officeId:x}=L(f=>f.doctorData),[c,r]=i.useState(!1),[o,j]=i.useState([]),[d,y]=i.useState(null),p=async f=>{r(!0);const{data:I}=await a({ClinicId:t,OfficeId:x,Name:f});j(I),r(!1)};i.useEffect(()=>{p()},[]);const S={loading:c,DepartmentsList:o,getData:p,search:f=>{p(f)},rowSelected:d,setRowSelected:y};return e.jsx(X.Provider,{value:S,children:m})},k=()=>i.useContext(X),ge=()=>{const{clinicId:m,officeId:a}=L(n=>n.doctorData),t=re(),x=ce(),{rowSelected:c,setRowSelected:r,getData:o}=k(),{DeleteDepartment:j,AddDepartment:d,EditDepartment:y,GetDepartmentById:p,getDepartmentTypes:w}=V(),{control:S,handleSubmit:f,formState:{errors:I},reset:C,setValue:G}=ie({defaultValues:{title:"",type:1}}),[E,s]=i.useState(!1),[l,u]=i.useState(!1),[v,h]=i.useState([]),Y=()=>{u(!1),s(!0),C()},J=async()=>{const{data:n}=await w();n&&h(n.map(D=>({label:D.title,value:D.id.toString()})))},Q=async()=>{try{if(!c)return;const{data:n}=await p(c.id);if(!n)return;G("title",n.title),G("type",n.type),u(!0),s(!0)}catch(n){console.log("ERROR_IN_GET_DEPARTMENT_BY_ID",n)}},Z=()=>{c&&x({content:"آیا از حذف این بخش مطمئن هستید؟"}).then(async()=>{const{message:n}=await j(c.id);o(),t(H().getDepartmentsAction()),r(null),b.success(n)}).catch(()=>{})},ee=async n=>{const{title:D,type:ae}=n,W={title:String(D),type:+ae,clinicId:m,officeId:a};let T="";try{if(l){if(!c)return;const{message:N}=await y({...W,id:c.id});T=N??""}else{const{message:N}=await d(W);T=N??""}b.success(T),o(),r(null),C(),t(H().getDepartmentsAction()),s(!1)}catch{console.log("ERROR_IN_DEPARTMENT_DATA_SUBMITTION")}},{isInRole:te}=oe();i.useEffect(()=>{J()},[]);const se=[{item:e.jsx(B,{onClick:Y,children:e.jsx(g.AddIcon,{fontSize:"large",color:"success"})}),expectedRoles:_.AddDepartment},{item:e.jsx(B,{onClick:Q,children:e.jsx(g.EditIcon,{fontSize:"large",color:c?"primary":"disabled"})}),expectedRoles:_.EditDepartment},{item:e.jsx(B,{onClick:Z,children:e.jsx(g.DeleteIcon,{fontSize:"large",color:c?"error":"disabled"})}),expectedRoles:_.DeleteDepartment}];return e.jsxs(e.Fragment,{children:[e.jsx(A,{direction:"row",paddingY:1,spacing:1,children:se.filter(n=>te(void 0,[n.expectedRoles])===!0).map(n=>n.item)}),E&&e.jsxs($,{title:l?"ویرایش بخش":"افزودن گروه بخش",maxWidth:"xs",onClose:()=>s(!1),children:[e.jsx(F,{className:"!pb-1",children:e.jsxs(A,{component:"form",paddingX:0,marginBottom:0,paddingTop:1,alignItems:"center",gap:2,children:[e.jsx(z,{name:"title",control:S,rules:{required:!0},render:({field:{onChange:n,value:D}})=>e.jsx(q,{label:"نام بخش",fullWidth:!0,error:!!I.title,value:D,onChange:n})}),e.jsx(z,{name:"type",control:S,rules:{required:!0},render:({field:{onChange:n,value:D}})=>e.jsx(le,{items:v,label:"نوع بخش",fullWidth:!0,error:!!I.type,value:D,onchange:n})})]})}),e.jsx(de,{onClose:()=>s(!1),onSubmit:f(ee),submitlabel:l?"ثبت":"افزودن"})]})]})};class je{constructor(){this.updateAsync=async a=>{const t=await M.put("DepartmentServiceDetailGroup",a);return t==null?void 0:t.data},this.getAsync=async a=>{const t=await M.get(`DepartmentServiceDetailGroup/${a}`);return t==null?void 0:t.data},this.deleteAsync=async a=>{const t=await M.delete(`DepartmentServiceDetailGroup/${a}`);return t==null?void 0:t.data}}}const Se=({onClose:m})=>{const{rowSelected:a}=k(),t=new je,{getServiceDetailGroupSelectListAsync:x}=ue(),[c,r]=i.useState(!1),[o,j]=i.useState(null),[d,y]=i.useState([]),p=async()=>{try{r(!0);const{data:s}=await t.getAsync(a.id);s&&j(s)}finally{r(!1)}},w=s=>pe.flatMap(s,l=>[{id:l.id,name:l.name},...w(l.child??[])]),S=async s=>{try{if(r(!0),!o)return;const{data:l}=await x({query:s});if(!l)return;const u=o.map(h=>h.serviceDetailGroupId),v=w(l).filter(h=>!u.includes(h.id));y(v)}finally{r(!1)}},f=async s=>{try{r(!0);const{message:l}=await t.updateAsync({departmentId:a.id,serviceDetailGroupIds:[...(o||[]).filter(u=>u.id!=s).map(u=>u.serviceDetailGroupId)]});b.success(l),S(),p()}finally{r(!1)}},I=async s=>{try{r(!0);const l=[...(o||[]).map(h=>h.serviceDetailGroupId),s.id],{message:u}=await t.updateAsync({departmentId:a.id,serviceDetailGroupIds:l});b.success(u);const v=d.filter(h=>h.id!==s.id);y(v),p()}finally{r(!1)}},C=async()=>{try{r(!0);const s=new Set([...(o||[]).map(u=>u.serviceDetailGroupId),...d.map(u=>u.id)]),{message:l}=await t.updateAsync({departmentId:a.id,serviceDetailGroupIds:Array.from(s)});b.success(l),y([]),p()}finally{r(!1)}};i.useEffect(()=>{p()},[]),i.useEffect(()=>{S()},[o]);const G=[{title:"نام گروه",key:"name",dataIndex:"name",width:150},{title:"حذف",key:"id",dataIndex:"id",width:40,render:s=>e.jsx(O,{size:"small",color:"error",className:"!m-0 !p-0",onClick:()=>f(s),children:e.jsx(g.DeleteIcon,{})})}],E=[{title:"نام گروه",key:"name",dataIndex:"name",width:150}];return e.jsx($,{onClose:m,title:`گروه خدمات بخش ${a==null?void 0:a.title}`,maxWidth:"sm",children:e.jsx(F,{className:"m-1 !p-2",children:e.jsxs("div",{className:"flex items-center gap-1 w-full",children:[e.jsx("div",{className:"w-1/2",children:e.jsx(P,{loading:c,rowKey:"id",columns:E,data:d,onClick:s=>I(s)})}),e.jsx(O,{className:"!bg-gray-600 !p-2 !text-gray-50",onClick:C,children:e.jsx(ne,{title:e.jsx("p",{className:"text-sm",children:"افزودن همه"}),children:e.jsx(g.ArrowBack,{})})}),e.jsx("div",{className:"w-1/2",children:e.jsx(P,{loading:c,rowKey:"id",columns:G,data:o||[]})})]})})})},Ie=()=>{const{DepartmentsList:m,loading:a,setRowSelected:t,rowSelected:x}=k(),[c,r]=i.useState(!1),o=d=>{t(d)},j=[{title:"عنوان",dataIndex:"title",key:"title",render:(d,y,p)=>me(d)},{title:"نوع بخش",dataIndex:"typeName",key:"typeName"},{title:"گروه خدمات",dataIndex:"id",key:"id",render:d=>e.jsx(O,{size:"small",color:"primary",className:"!m-0 !p-0",onClick:()=>r(!0),children:e.jsx(g.MedicalServices,{})})}];return e.jsxs(e.Fragment,{children:[e.jsx(P,{loading:a,rowKey:"id",columns:j||[],data:m||[],onClick:d=>o(d)}),c&&x&&e.jsx(Se,{onClose:()=>r(!1)})]})},we=()=>{const{userType:m}=L(o=>o.user),{search:a}=k(),[t,x]=i.useState(""),c=()=>{a(t)},r=()=>{x(""),a("")};return e.jsxs(A,{className:"h-full overflow-y-auto p-2 pb-10 border-l",spacing:1,children:[e.jsx(he,{className:+m===xe.accountant?"!bg-[#b0d8e7]":"",children:"نام بخش"}),e.jsx(q,{label:"بخش ...",value:t,onChange:x}),e.jsxs(A,{component:"form",direction:"row",justifyContent:"center",gap:1,children:[e.jsx(K,{fullWidth:!0,startIcon:e.jsx(g.SearchIcon,{}),onClick:c,children:"جستجو"}),e.jsx(K,{fullWidth:!0,startIcon:e.jsx(g.RefreshIcon,{}),onClick:r,children:"بازنشانی"})]})]})},ve=()=>e.jsxs(ye,{className:"!px-4 !py-2",children:[e.jsx(fe,{prevRoutes:[{path:"/",title:"داشبورد"}],currentRoute:"بخش ها"}),e.jsxs(R,{container:!0,xs:12,spacing:1,className:"!h-full",children:[e.jsx(R,{item:!0,xs:12,sm:2,children:e.jsx(we,{})}),e.jsxs(R,{item:!0,xs:12,sm:10,spacing:1,children:[e.jsx(ge,{}),e.jsx(Ie,{})]})]})]}),Ee=()=>e.jsx(De,{children:e.jsx(ve,{})});export{Ee as default};
