import{j as r,c as k,M as s}from"./vendor-mui-CBBEb17G.js";import{a as o}from"./vendor-react-BBazpZul.js";import{fn as B,fF as J,cG as R,u as T,aO as V,v as H,by as n,aP as c,w as p,ao as y,J as E,fE as X,g as Y}from"./index-DdWwwZXu.js";import"./vendor-utils-BveVE6Uf.js";const Z={firstName:"",lastName:"",medicalCode:"",expertise:null,gender:null,province:null,city:null,mobile:"",address:""},te=({onClose:f})=>{const{data:u,isSuccess:g,isLoading:L}=B(),{data:m,isSuccess:N,isLoading:q}=J(),{data:j,isSuccess:C}=R(),D=T(),[G,W]=o.useState(null),[h,w]=o.useState(null),[A,O]=o.useState(null),[P,z]=o.useState(null),{control:l,formState:{errors:a,isSubmitting:I},handleSubmit:Q,watch:v}=V({defaultValues:Z}),F=async()=>{if(!j||!C)return;const e=j.map(i=>({label:i.name,value:i.id.toString()}));z(e),w(j)},K=()=>{f()},M=async e=>{var i,t,d,S;try{if(e.mobile&&e.mobile.length!==11)return E.error("شماره موبایل معتبر نمی‌باشد");const b={firstName:e.firstName,lastName:e.lastName,medicalCode:e.medicalCode,expertiseId:Number((i=e.expertise)==null?void 0:i.value),gender:Number((t=e.gender)==null?void 0:t.value),mobile:e.mobile?e.mobile:void 0,address:{provinceId:Number((d=e==null?void 0:e.province)==null?void 0:d.value),cityId:Number((S=e==null?void 0:e.city)==null?void 0:S.value),address:e.address}},{message:U}=await X.add(b);E.success(U),D(Y().getExternalDoctorsAction({})),K()}catch(b){console.log(b)}};o.useEffect(()=>{if(!g||!N)return;const e=(u==null?void 0:u.map(t=>({label:t.title,value:t.id.toString()})))||null,i=(m==null?void 0:m.map(t=>({label:t.title,value:t.id.toString()})))||null;O(i),W(e)},[u,g,m,N]),o.useEffect(()=>{F()},[]);const x=L||q;return r.jsx(H,{title:"افزودن پزشک معالج",onClose:f,children:r.jsx(k,{className:"m-1 !p-2",children:r.jsxs("form",{className:"space-y-10",onSubmit:Q(M),children:[r.jsxs(s,{container:!0,spacing:1,children:[r.jsx(s,{item:!0,xs:12,sm:6,children:r.jsx(n,{name:"firstName",control:l,rules:{required:!0},render:({field:e})=>r.jsx(c,{...e,label:"نام",required:!0,fullWidth:!0,error:!!a.firstName})})}),r.jsx(s,{item:!0,xs:12,sm:6,children:r.jsx(n,{name:"lastName",control:l,rules:{required:!0},render:({field:e})=>r.jsx(c,{...e,fullWidth:!0,label:"نام خانوادگی",required:!0,error:!!a.lastName})})}),r.jsx(s,{item:!0,xs:12,sm:6,children:r.jsx(n,{control:l,name:"gender",rules:{required:!0},render:({field:e})=>r.jsx(p,{...e,label:"جنسیت",size:"small",fullWidth:!0,selectOption:A||[],loading:x,error:!!a.gender,requierd:!0})})}),r.jsx(s,{item:!0,xs:12,sm:6,children:r.jsx(n,{name:"medicalCode",control:l,rules:{maxLength:10},render:({field:e})=>r.jsx(c,{...e,fullWidth:!0,label:"کد نظام پزشکی",error:!!a.medicalCode,inputProps:{maxLength:10}})})}),r.jsx(s,{item:!0,xs:12,sm:6,children:r.jsx(n,{name:"province",control:l,render:({field:e})=>r.jsx(p,{...e,label:"استان",size:"small",fullWidth:!0,selectOption:P||[],loading:x})})}),r.jsx(s,{item:!0,xs:12,sm:6,children:r.jsx(n,{name:"city",control:l,render:({field:e})=>{var i;return r.jsx(p,{...e,label:"شهر",size:"small",fullWidth:!0,selectOption:((i=h==null?void 0:h.find(t=>{var d;return t.id===Number((d=v("province"))==null?void 0:d.value)}))==null?void 0:i.cities.map(t=>({label:t.name,value:t.id.toString()})))||[],loading:x})}})}),r.jsx(s,{item:!0,xs:12,sm:6,children:r.jsx(n,{name:"expertise",control:l,rules:{required:!0},render:({field:e})=>r.jsx(p,{...e,label:"تخصص",size:"small",fullWidth:!0,selectOption:G||[],loading:x,requierd:!0,error:!!a.expertise})})}),r.jsx(s,{item:!0,xs:12,sm:6,children:r.jsx(n,{name:"mobile",control:l,rules:{required:!1},render:({field:e})=>r.jsx(c,{...e,label:"موبایل",size:"small",fullWidth:!0,error:!!a.mobile,inputProps:{maxLength:"11"}})})}),r.jsx(s,{item:!0,xs:12,children:r.jsx(n,{name:"address",control:l,render:({field:e})=>r.jsx(c,{...e,fullWidth:!0,label:"آدرس",multiline:!0,rows:2})})})]}),r.jsxs(s,{display:"flex",flex:1,justifyContent:"center",gap:1,children:[r.jsx(y,{className:"w-full max-w-[80px] !text-base",type:"submit",loading:I,color:"primary",children:"ثبت"}),r.jsx(y,{className:"w-full max-w-[80px] !text-base",onClick:()=>f(),type:"button",color:"error",children:"انصراف"})]})]})})})};export{te as A};
