import{j as e,c as B,S as c,W as x}from"./vendor-mui-CBBEb17G.js";import{B as E}from"./index-BkhbTDKf.js";import{a as r}from"./vendor-react-BBazpZul.js";import{aO as F,v as O,by as T,aP as L,x as P,at as W,c5 as q,dD as h,I as p,aX as K,J as b,z as M}from"./index-IP_FWtso.js";import{S as J}from"./index-B7-psy45.js";import{S as Q}from"./index-D7-0uPSw.js";import"./vendor-utils-BveVE6Uf.js";import"./vendor-charts-tGnAgELZ.js";const U=({defaultValue:o,onClose:l,onSubmit:f,loading:S})=>{const{control:j,handleSubmit:d,formState:{errors:a}}=F({defaultValues:o??void 0}),g=async n=>{await f(n.name),l()};return e.jsxs(O,{title:o?"ویرایش اضافات":"افزودن اضافات",onClose:l,maxWidth:"xs",children:[e.jsx(B,{className:"m-1 !p-2",children:e.jsx(c,{component:"form",children:e.jsx(T,{name:"name",control:j,rules:{required:!0},render:({field:{onChange:n,value:u}})=>e.jsx(L,{label:"عنوان",fullWidth:!0,error:!!a.name,value:u,onChange:n,required:!0})})})}),e.jsx(P,{onClose:l,onSubmit:d(g),loading:S})]})},V=()=>{const{addSurplusage:o,deleteSurplusage:l,getSurplusage:f,updateSurplusage:S}=new J,j=W(),[d,a]=r.useState(!1),[g,n]=r.useState(!1),[u,w]=r.useState([]),[C,I]=r.useState([]),[s,y]=r.useState(null),i=async()=>{try{a(!0);const{data:t}=await f();t&&w(t),t&&I(t)}finally{a(!1)}},D=t=>{u.length&&I(u.filter(m=>m.name.includes(t)||m.id.toString().includes(t)))},k=()=>{n(!1),y(null)},N=()=>{y(null),n(!0)},A=async t=>{try{if(a(!0),s){const{message:v}=await S({name:t,id:s.id});b.success(v),i();return}const{message:m}=await o({name:t});b.success(m),i()}finally{a(!1)}},R=()=>{try{if(a(!0),!s)return;j({content:"آیا از حذف این مورد اطمینان دارید؟"}).then(async()=>{const{message:t}=await l(s.id);b.success(t),i()}).catch(()=>{})}finally{a(!1)}};r.useEffect(()=>{i()},[]);const z=[{title:"کد",key:"id",dataIndex:"id",width:70,render:t=>M(t??0)},{title:"عنوان",key:"name",dataIndex:"name",width:250}];return e.jsxs(c,{spacing:1,p:1,className:"h-full",children:[e.jsxs(c,{direction:{xs:"column",md:"row"},alignItems:{xs:"start",md:"center"},spacing:1,children:[e.jsx(q,{onChange:D}),e.jsxs(c,{direction:"row",spacing:1,alignItems:"center",children:[e.jsx(x,{title:e.jsx("h1",{className:"text-sm",children:"افزودن"}),placement:"top",children:e.jsx(h,{onClick:()=>N(),children:e.jsx(p.AddIcon,{fontSize:"large",color:"success"})})}),e.jsx(x,{title:e.jsx("h1",{className:"text-sm",children:"ویرایش"}),placement:"top",children:e.jsx(h,{onClick:()=>s&&n(!0),disabled:!s,children:e.jsx(p.EditIcon,{fontSize:"large",color:s?"primary":"disabled"})})}),e.jsx(x,{title:e.jsx("h1",{className:"text-sm",children:"حذف"}),placement:"top",children:e.jsx(h,{onClick:()=>s&&R(),disabled:!s,children:e.jsx(p.DeleteIcon,{fontSize:"large",color:s?"error":"disabled"})})}),e.jsx(x,{title:e.jsx("h1",{className:"text-sm",children:"تازه سازی"}),placement:"top",children:e.jsx(h,{onClick:()=>i(),children:e.jsx(p.RefreshIcon,{fontSize:"large",color:"info"})})})]})]}),e.jsx(c,{className:"h-[76dvh]",children:e.jsx(K,{columns:z,data:C,rowKey:"id",loading:d,onClick:t=>y(t)})}),g&&e.jsx(U,{onClose:k,defaultValue:s,loading:d,onSubmit:A})]})},te=()=>e.jsxs(Q,{children:[e.jsx(E,{prevRoutes:[{path:"/",title:"داشبورد"}],currentRoute:"اضافات"}),e.jsx(V,{})]});export{te as default};
