import{j as s,c as v,S as C}from"./vendor-mui-CBBEb17G.js";import{a}from"./vendor-react-BBazpZul.js";import{e as L,at as P,J as g,aL as N,aO as V,v as k,aP as j,x as q}from"./index-IP_FWtso.js";import"./vendor-utils-BveVE6Uf.js";const A=a.createContext(void 0),M=({children:n})=>{const{AddReasonForReference:R,GetAllReasonForReference:h,deleteReeasonForReference:l,editReeasonForReference:y}=N(),{doctorId:r,clinicId:d,officeId:u}=L(t=>t.doctorData),x=P(),[p,e]=a.useState(!1),[o,m]=a.useState([]),[c,b]=a.useState(),f=async t=>{try{e(!0);const{data:i}=await h(r,{name:t});i&&m(i)}finally{e(!1)}},w=async(t,i)=>{try{e(!0);const{message:F,data:S}=await R({clinicId:d,officeId:u,title:t,comment:i});return f(),g.success(F),S==null?void 0:S.resonForReferenceId}finally{e(!1)}},D=async(t,i)=>{try{if(e(!0),!c)return;const{message:F}=await y({clinicId:d,officeId:u,title:t,comment:i,id:c.id});return f(),g.success(F),c.id}finally{e(!1)}},E=()=>{try{if(e(!0),!c)return;x({content:"آیا از حذف این علت مراجعه اطمینان دارید؟"}).then(async()=>{const{message:t}=await l(c.id);f(),g.success(t)}).catch(()=>{})}finally{e(!1)}};a.useEffect(()=>{f()},[]);const I={states:{loading:p,dataList:o,selectedItem:c},actions:{setSelectedItem:b},handlers:{fetchData:f,handleAdd:w,handleEdit:D,handleDelete:E}};return s.jsx(A.Provider,{value:I,children:n})},B=()=>a.useContext(A),O=({onClose:n})=>{const{handlers:{handleAdd:R,handleEdit:h},states:{selectedItem:l,loading:y}}=B(),r=!!l,{register:d,handleSubmit:u,setValue:x,formState:{errors:p}}=V(),e=async o=>{if(r){const m=await h(o.title,o.comment);n(m)}else{const m=await R(o.title,o.comment);n(m)}};return a.useEffect(()=>{r&&(x("title",l.title),x("comment",l.comment))},[r]),s.jsxs(k,{title:r?"ویرایش علت مراجعه":"افزودن علت مراجعه",onClose:n,maxWidth:"xs",children:[s.jsx(v,{className:"m-1 !p-2",children:s.jsx("form",{children:s.jsxs(C,{spacing:1,className:"p-3 border-2 rounded-xl",children:[s.jsx(j,{label:"عنوان",name:"title",register:d,validation:{required:!0},error:!!p.title}),s.jsx(j,{label:"توضیحات",name:"comment",register:d,multiline:!0,rows:3})]})})}),s.jsx(q,{onClose:n,onSubmit:u(e),loading:y})]})};export{M as R,O as a,B as u};
