import{j as e,S,o as y,c as T}from"./vendor-mui-CBBEb17G.js";import{B as M}from"./index-BkhbTDKf.js";import{a as t}from"./vendor-react-BBazpZul.js";import{J as k,c5 as R,I as j,aX as O,v as V,aP as _,bF as z,bE as G,x as H,bI as J,fI as Q}from"./index-IP_FWtso.js";import{C as X}from"./index-xit8Crv2.js";import{C as q}from"./changeValuePopUp-CC3wfnLO.js";import"./vendor-utils-BveVE6Uf.js";import"./vendor-charts-tGnAgELZ.js";const W=t.createContext(void 0),Y=({children:d})=>{const n=new X,[o,i]=t.useState(!1),[v,g]=t.useState(!1),[A,p]=t.useState([]),[l,b]=t.useState(null),[w,m]=t.useState(null),u=async(r="")=>{try{i(!0);const{data:h}=await n.getAsync();if(!h)return;const f=h.filter(L=>L.name.includes(r));h&&p(f)}finally{i(!1)}},x=async()=>{try{if(i(!0),!l)return;const{data:r}=await n.getByIdAsync(l.id);m(r)}finally{i(!1)}},C=async r=>{try{if(i(!0),!!l){const{message:f}=await n.updateAsync({...r,id:l.id});k.success(f)}else{const{message:f}=await n.addAsync(r);k.success(f)}u(),b(null),g(!1)}finally{i(!1)}};t.useEffect(()=>{l&&x()},[l]);const I={states:{loading:o,data:A,selectedItem:l,openAdd:v,selectedItemDetail:w},actions:{setSelectedItem:b,setOpenAdd:g,setSelectedItemDetail:m},handlers:{fetchData:u,handleSubmitForm:C}};return e.jsx(W.Provider,{value:I,children:d})},D=()=>t.useContext(W),Z=()=>{const{handlers:{fetchData:d},actions:{setOpenAdd:n},states:{selectedItem:o}}=D();return e.jsxs(S,{direction:"row",alignItems:"center",spacing:1,children:[e.jsx(R,{onChange:d}),e.jsx(y,{color:"success",onClick:()=>n(!0),children:e.jsx(j.AddIcon,{})}),e.jsx(y,{color:"primary",onClick:()=>n(!0),disabled:!o,children:e.jsx(j.EditIcon,{})})]})},$=()=>{const{states:{data:d,loading:n},actions:{setSelectedItem:o,setOpenAdd:i}}=D(),v=[{title:"عنوان",key:"name",dataIndex:"name",width:200},{title:"جزییات",key:"serviceDetails",dataIndex:"serviceDetails",width:70,render:g=>e.jsx(y,{onClick:()=>i(!0),children:e.jsx(j.Eye,{})})}];return e.jsx(S,{className:"h-[75dvh]",children:e.jsx(O,{columns:v,data:d,rowKey:"id",loading:n,onClick:o})})},ee=({onClose:d})=>{const{GetCenterServicesSelectList:n}=Q(),{states:{loading:o,selectedItemDetail:i,selectedItem:v},actions:{setSelectedItemDetail:g},handlers:{handleSubmitForm:A}}=D(),[p,l]=t.useState([]),[b,w]=t.useState([]),[m,u]=t.useState([]),[x,C]=t.useState(null),[I,r]=t.useState(""),[h,f]=t.useState(1),L=!!v,E=async s=>{const{data:a}=await n({query:s});a&&w(a.map(c=>({label:c.title??"",value:c.serviceDetailId.toString()})))},F=()=>{p.length&&(u(s=>[...s,...p.filter(a=>!s.some(c=>c.id===+a.value)).map(a=>({title:a.label,id:+a.value,order:h}))]),l([]))},K=s=>{u(a=>a.filter(c=>c.id!=s))},P=async()=>{if(!I){k.warning("عنوان را وارد نمایید");return}if(!m.length){k.warning("لیست خدمات نمی تواند خالی باشد");return}await A({name:I,serviceDetails:m.map(s=>({serviceDetailId:s.id,order:s.order}))})},N=()=>{l([]),w([]),u([]),r(""),g(null),d()},U=s=>{const a=m.findIndex(c=>c.id===(x==null?void 0:x.id));a<0||(u(c=>J.toUpdate(c,a,{...c[a],order:s})),C(null))};t.useEffect(()=>{E()},[]),t.useEffect(()=>{i&&(u(i.serviceDetails),r(i.name))},[i]);const B=[{title:"نام خدمت",key:"title",dataIndex:"title",width:150},{title:"اولویت",key:"order",dataIndex:"order",width:70,render:(s,a)=>e.jsxs("div",{className:"relative group inline-flex items-center w-full",onClick:()=>C(a),children:[e.jsx("div",{className:"w-full bg-white  !m-0 px-2 border rounded-lg text-center",children:s}),e.jsx(j.EditIcon,{className:"ml-2 !hidden text-gray-500 group-hover:!inline-block"})]})},{title:"حذف",key:"id",dataIndex:"id",width:50,render:s=>e.jsx(y,{color:"error",size:"small",className:"!m-0 !p-0",onClick:()=>K(s),children:e.jsx(j.DeleteIcon,{})})}];return e.jsxs(V,{onClose:N,title:L?"ویرایش لیست کار":"افزودن لیست کار جدید",children:[e.jsx(T,{className:"flex-1 h-full m-1 !p-2",children:e.jsxs(S,{spacing:2,className:"h-full",children:[e.jsx(_,{label:"عنوان",value:I,onChange:r}),e.jsxs(S,{direction:"row",alignItems:"center",spacing:1,children:[e.jsx(z,{label:"انتخاب خدمات",onChange:l,value:p,selectOption:b,fullWidth:!0,onSearch:E}),e.jsx(G,{label:"اولویت",value:h,onChange:f,minValue:1}),e.jsx(y,{onClick:()=>F(),color:"success",disabled:!p.length,children:e.jsx(j.AddIcon,{})})]}),e.jsx(O,{columns:B,data:m,rowKey:"id",loading:o})]})}),e.jsx(H,{onClose:N,onSubmit:P,loading:o}),x&&e.jsx(q,{onClose:()=>C(null),onChange:U,value:x.order,type:"number"})]})},te=()=>{const{states:{openAdd:d},actions:{setOpenAdd:n,setSelectedItem:o}}=D();return e.jsxs(S,{p:1,children:[e.jsx(Z,{}),e.jsx($,{}),d&&e.jsx(ee,{onClose:()=>{n(!1),o(null)}})]})},de=()=>e.jsxs(S,{className:"!h-full !px-4 !py-2 !space-y-2 overflow-hidden",children:[e.jsx(M,{prevRoutes:[{path:"/",title:"داشبورد"}],currentRoute:"لیست کار سفارشی آزمایشگاه"}),e.jsx(Y,{children:e.jsx(te,{})})]});export{de as default};
