import{j as e,c as w,M as p,S as y,W as R}from"./vendor-mui-CBBEb17G.js";import{a as c}from"./vendor-react-BBazpZul.js";import{fn as b,J as h,aX as k,v as N,aP as j,gn as P,x as _,dD as A,I as O}from"./index-DdWwwZXu.js";import{a as S}from"./vendor-utils-BveVE6Uf.js";import{B as T}from"./index-DQbmBtLN.js";import"./vendor-charts-tGnAgELZ.js";const L=async o=>(await S.post("/ExpertiseCustomCode",o)).data,M=async o=>(await S.put(`/ExpertiseCustomCode/${o.id}`,o)).data,E={add:L,update:M},D=c.createContext(null),U=({children:o})=>{const[t,n]=c.useState(null),{refetch:r}=b(),l={states:{selectedExpertiseCode:t},handlers:{setSelectedExpertiseCode:n},functions:{addCustomCode:async a=>{try{const{message:i}=await E.add(a);h.success(i),r()}catch{console.log("ERROR_IN_ADD_CUSTOM_CODE")}},updateCustomCode:async a=>{try{const{message:i}=await E.update(a);h.success(i),r()}catch{console.log("ERROR_IN_UPDATE_CUSTOM_CODE")}}}};return e.jsx(D.Provider,{value:l,children:o})},f=()=>c.useContext(D),B=()=>{const{handlers:{setSelectedExpertiseCode:o}}=f(),{data:t,isLoading:n}=b(),r=[{key:"title",dataIndex:"title",title:"نام",width:120},{key:"customCode",dataIndex:"customCode",title:"کد دلخواه",width:180}];return e.jsx(k,{rowKey:"id",loading:n,columns:r,data:t||[],onClick:s=>o(s),boxProps:{height:"80vh"}})},W=({onClose:o})=>{const{states:{selectedExpertiseCode:t},handlers:{setSelectedExpertiseCode:n},functions:{updateCustomCode:r,addCustomCode:s}}=f(),[u,l]=c.useState(!1),[a,i]=c.useState((t==null?void 0:t.customCode)??""),g=async d=>{try{if(!t)return;const C=!!t.customCode;if(d&&I(d),l(!0),a==="")return h.error("کد لخواه نمیتواند خالی باشد");if(C){if(t.customCode===a)return;const m={id:t.customCodeId,code:a,expertiseId:t.id};await r(m)}else{const m={code:a,expertiseId:t.id};await s(m)}x()}catch(C){console.log(C)}finally{l(!1)}},x=()=>{n(null),o()},I=d=>{d.preventDefault(),d.stopPropagation()},v=u;return t?e.jsx(N,{title:"ویرایش تخصص",onClose:x,children:e.jsx(w,{className:"!py-4 !px-2",children:e.jsxs("form",{onSubmit:g,className:"p-0",children:[e.jsxs(p,{container:!0,spacing:1,children:[e.jsx(p,{item:!0,xs:12,sm:6,children:e.jsx(j,{label:"کد دلخواه",value:t.title,disabled:!0,fullWidth:!0})}),e.jsx(p,{item:!0,xs:12,sm:6,children:e.jsx(j,{label:"کد دلخواه",disabled:v,value:a,onChange:d=>P(d,i),className:"py-1 px-3 pl-10 w-full outline-none ring-offset-0 border-0 disabled:bg-gray-200 bg-transparent",inputProps:{maxLength:5},fullWidth:!0})})]}),e.jsx(_,{onClose:x,onSubmit:g,loading:u})]})})}):null},F=()=>{const[o,t]=c.useState({add:!1,edit:!1}),{states:{selectedExpertiseCode:n}}=f(),r=[{title:"ویرایش",icon:e.jsx(O.EditIcon,{color:n?"primary":"disabled"}),onClick:()=>t(s=>({...s,edit:!0})),isDisabled:!n}];return e.jsxs(e.Fragment,{children:[e.jsx(y,{direction:"row",justifyItems:"start",alignItems:"center",gap:1,children:r.map(s=>e.jsx(R,{title:e.jsx("h1",{className:"text-sm",children:s.title}),placement:"top",children:e.jsx(A,{onClick:s.onClick,children:s.icon})},s.title))}),o.edit&&e.jsx(W,{onClose:()=>t(s=>({...s,edit:!1}))})]})},q=()=>e.jsxs(y,{className:"p-2 !space-y-2",children:[e.jsx(T,{prevRoutes:[{path:"/",title:"داشبورد"}],currentRoute:"کد تخصص"}),e.jsxs(U,{children:[e.jsx(F,{}),e.jsx(B,{})]})]});export{q as default};
