import{j as t,c as z,S as f,W as B,B as I}from"./vendor-mui-CBBEb17G.js";import{B as P}from"./index-BkhbTDKf.js";import{a as d}from"./vendor-react-BBazpZul.js";import{aO as q,v as W,by as S,aP as w,r as E,x as F,at as K,aQ as O,aX as U,I as y,z as L,J as j}from"./index-IP_FWtso.js";import{S as M}from"./index-AEfaHmzd.js";import{S as Q}from"./index-D7-0uPSw.js";import"./vendor-utils-BveVE6Uf.js";import"./vendor-charts-tGnAgELZ.js";const J=({onClose:c,onSubmit:x,selectedItem:o,loading:n,typeItems:h})=>{const{handleSubmit:u,control:s,formState:{errors:p}}=q({defaultValues:{code:(o==null?void 0:o.code)??"",value:(o==null?void 0:o.value)??"",type:1}}),g=r=>{x({code:r.code,value:r.value,type:r.type})};return t.jsxs(W,{title:o?"ویرایش جواب":"افزودن جواب جدید",maxWidth:"xs",onClose:c,children:[t.jsx(z,{className:"m-1 !p-2",children:t.jsxs(f,{component:"form",spacing:2,children:[t.jsx(S,{name:"code",control:s,rules:{required:!1},render:({field:{onChange:r,value:i}})=>t.jsx(w,{label:"کد",fullWidth:!0,error:!!p.code,className:"!mt-1",value:i,onChange:r})}),t.jsx(S,{name:"value",control:s,rules:{required:!0},render:({field:{onChange:r,value:i}})=>t.jsx(w,{label:"مقدار",fullWidth:!0,error:!!p.value,className:"!mt-1",value:i,onChange:r,required:!0})}),t.jsx(S,{name:"type",control:s,rules:{required:!0},render:({field:{onChange:r,value:i}})=>t.jsx(E,{label:"نوع جواب",items:h,fullWidth:!0,error:!!p.type,className:"!mt-1",value:i,onchange:r,required:!0})})]})}),t.jsx(F,{onClose:c,onSubmit:u(g),loading:n})]})},V={pageNumber:1,pageSize:50},X=()=>{const c=new M,x=K(),[o,n]=d.useState(!1),[h,u]=d.useState(!1),[s,p]=d.useState(null),[g,r]=d.useState(V),[i,b]=d.useState(null),[C,k]=d.useState([]),v=async()=>{try{n(!0);const{data:e}=await c.getTypesAsync();e&&k(e.map(a=>({label:a.title,value:a.id})))}finally{n(!1)}},m=async e=>{try{n(!0);let a={...g};b(null);const{data:l}=await c.getAsync(a);l&&p(l)}finally{n(!1)}},N=async e=>{try{n(!0),await c.toggleAsync(e),j.success("تغییر وضعیت با موفقیت انجام شد"),m()}finally{n(!1)}},A=async e=>{try{n(!0);let a=null;if(i){const{message:l}=await c.updateAsync({id:i.id,value:e.value,code:e.code,type:e.type});a=l}else{const{message:l}=await c.addAsync({value:e.value,code:e.code,type:e.type});a=l}a&&j.success(a),u(!1),m()}finally{n(!1)}},D=async()=>{try{if(n(!0),!i)return;x({content:"آیا از حذف این جواب اطمینان دارید؟"}).then(async()=>{const{message:e}=await c.deleteAsync(i.id);j.success(e),m()}).catch(()=>{})}finally{n(!1)}};d.useEffect(()=>{m(),v()},[]);const R=[{title:"ردیف",key:"num",width:70,render:(e,a,l)=>L(l+1)},{title:"کد",key:"code",dataIndex:"code",width:100},{title:"مقدار",key:"value",dataIndex:"value",width:200},{title:"نوع",key:"typeName",dataIndex:"typeName",width:150},{title:"وضعیت",key:"isDelete",dataIndex:"isDelete",width:70,render:(e,a)=>t.jsx(I,{variant:"contained",size:"small",color:e?"error":"success",onClick:()=>N(a.id),children:e?"غیر فعال":"فعال"})}],T=[{id:1,title:"جدید",icons:t.jsx(y.AddIcon,{color:"success"}),disabled:!1,onClick:()=>u(!0)},{id:2,title:"ویرایش",icons:t.jsx(y.EditIcon,{color:"primary"}),disabled:!i,onClick:()=>u(!0)},{id:3,title:"حذف",icons:t.jsx(y.DeleteIcon,{color:"error"}),disabled:!i,onClick:()=>D()},{id:4,title:"بازیابی",icons:t.jsx(y.RefreshIcon,{color:"info"}),disabled:!1,onClick:()=>m()}];return t.jsxs(f,{spacing:1,p:1,children:[t.jsx(f,{direction:"row",alignItems:"center",spacing:1,children:T.map(e=>t.jsx(B,{title:e.title,children:t.jsx(O,{onClick:e.onClick,disabled:e.disabled,children:e.icons})},e.id))}),t.jsx(f,{className:"h-[75vh]",children:t.jsx(U,{columns:R,data:(s==null?void 0:s.list)??[],rowKey:"id",loading:o,onClick:e=>b(e),totalCount:(s==null?void 0:s.pagination.totalCount)??0,pageNumber:((s==null?void 0:s.pagination.pageNumber)??1)-1,pageSize:(s==null?void 0:s.pagination.pageSize)??50,onPaginationLoad:(e,a)=>r(l=>({...l,pageNumber:e+1,pageSize:a}))})}),h&&t.jsx(J,{selectedItem:i,onSubmit:A,loading:o,onClose:()=>u(!1),typeItems:C})]})},se=()=>t.jsxs(Q,{className:"!px-4 !py-2 !space-y-2",children:[t.jsx(P,{prevRoutes:[{path:"/",title:"داشبورد"},{path:"/",title:"اطلاعات پایه"}],currentRoute:"جواب های آماده"}),t.jsx(X,{})]});export{se as default};
