import{j as t,S as x,W as h,c as L,M as j}from"./vendor-mui-CBBEb17G.js";import{B as N}from"./index-BkhbTDKf.js";import{S as z}from"./index-D7-0uPSw.js";import{at as H,aO as U,dD as f,c4 as V,et as F,bx as K,v as W,aP as y,x as Y,aX as O,z as X,e as _,c$ as q,ao as $,aT as J}from"./index-IP_FWtso.js";import{m as C}from"./index-Civ7hWff.js";import{a}from"./vendor-react-BBazpZul.js";import{T as Q}from"./index-Dz6vPtCj.js";import"./vendor-utils-BveVE6Uf.js";import"./vendor-charts-tGnAgELZ.js";const G=a.createContext(void 0),Z=({children:o})=>{const{getGroupsAsync:r}=C(),[n,e]=a.useState(!1),[s,l]=a.useState([]),[m,u]=a.useState(null),c=async d=>{e(!0);const{data:p}=await r(d||null);l(p),e(!1)};a.useEffect(()=>{c()},[]);const i={loading:n,getData:c,search:d=>{c(d)},materialGroupList:s,rowSelected:m,setRowSelected:u};return t.jsx(G.Provider,{value:i,children:o})},g=()=>a.useContext(G),tt=()=>{const{addGroupAsync:o,editGroupAsync:r,deleteGroupAsync:n}=C(),{rowSelected:e,setRowSelected:s,getData:l}=g(),m=H(),[u,c]=a.useState(!1),[S,i]=a.useState(!1),[d,p]=a.useState(!1),{register:v,handleSubmit:A,formState:{errors:M},reset:k,setValue:w}=U(),D=()=>{s(null),c(!1),k(),i(!0)},T=()=>{e&&(c(!0),w("title",e==null?void 0:e.title),i(!0))},E=()=>{e&&m({content:"آیا از حذف این گروه مطمئن هستید؟"}).then(async()=>{await n(e==null?void 0:e.id),l(),s(null)}).catch(()=>{})},I=async P=>{try{p(!0);const{title:R}=P,b={title:String(R)};if(u){const B={id:e==null?void 0:e.id,...b};await r(B)}else await o(b);s(null),i(!1),l()}finally{p(!1)}};return t.jsxs(t.Fragment,{children:[t.jsxs(x,{direction:"row",paddingY:1,spacing:1,children:[t.jsx(h,{title:"افزودن",placement:"top",children:t.jsx(f,{onClick:D,children:t.jsx(V,{fontSize:"large",color:"success"})})}),t.jsx(h,{title:"ویرایش",placement:"top",children:t.jsx(f,{onClick:T,children:t.jsx(F,{fontSize:"large",color:e?"primary":"disabled"})})}),t.jsx(h,{title:"حذف",placement:"top",children:t.jsx(f,{onClick:E,children:t.jsx(K,{fontSize:"large",color:e?"error":"disabled"})})})]}),S&&t.jsxs(W,{title:u?"ویرایش گروه مواد مصرفی":"افزودن گروه مواد مصرفی",maxWidth:"xs",onClose:()=>i(!1),children:[t.jsx(L,{children:t.jsx(x,{component:"form",paddingY:1.5,gap:1.5,children:t.jsx(x,{children:t.jsx(y,{label:"عنوان",name:"title",register:v,validation:{required:!0},error:!!M.title})})})}),t.jsx(Y,{onClose:()=>i(!1),onSubmit:A(I),submitlabel:u?"ثبت":"افزودن",loading:d})]})]})},et=[{title:"کد",dataIndex:"id",key:"id",width:20,render:o=>X(o)},{title:"نام گروه",dataIndex:"title",key:"title",width:150}],st=()=>{const{materialGroupList:o,setRowSelected:r,loading:n}=g(),e=s=>{r(s)};return t.jsx(O,{loading:n,boxProps:{height:"70vh"},rowKey:"id",columns:et||[],data:o||[],onClick:s=>e(s)})},at=()=>{const{userType:o}=_(l=>l.user),{search:r}=g(),[n,e]=a.useState(""),s=()=>{r(n)};return t.jsxs(x,{className:"h-full overflow-y-auto p-2 pb-10 border-l",spacing:1,children:[t.jsx(Q,{className:+o===q.accountant?"!bg-[#b0d8e7]":"",children:"نام گروه"}),t.jsx(y,{label:"گروه ...",onChange:e}),t.jsx(x,{component:"form",gap:1,children:t.jsx($,{fullWidth:!0,startIcon:t.jsx(J,{}),onClick:s,children:"جستجو"})})]})},ot=()=>t.jsxs(z,{className:"!px-4 !py-2",children:[t.jsx(N,{prevRoutes:[{path:"/",title:"داشبورد"},{path:"",title:"انبار"}],currentRoute:"گروه مواد مصرفی"}),t.jsxs(j,{container:!0,xs:12,spacing:1,className:"!h-full",children:[t.jsx(j,{item:!0,xs:12,sm:2,children:t.jsx(at,{})}),t.jsxs(j,{item:!0,xs:12,sm:10,spacing:1,children:[t.jsx(tt,{}),t.jsx(st,{})]})]})]}),mt=()=>t.jsx(Z,{children:t.jsx(ot,{})});export{mt as default};
