import{j as s,c as v,S as y}from"./vendor-mui-CBBEb17G.js";import{B as V}from"./index-BkhbTDKf.js";import{at as W,J as I,aO as F,v as R,by as A,aP as T,bE as w,x as $,dD as C,I as S,aX as q}from"./index-IP_FWtso.js";import{a as o}from"./vendor-react-BBazpZul.js";import{a as g}from"./vendor-utils-BveVE6Uf.js";import"./vendor-charts-tGnAgELZ.js";class z{constructor(){this.addAsync=async t=>{const e=await g.post("CustomerClub/CouponSetting",t);return e==null?void 0:e.data},this.updateAsync=async t=>{const e=await g.put(`CustomerClub/CouponSetting/${t.id}`,t);return e==null?void 0:e.data},this.getAsync=async t=>{const e=await g.get("CustomerClub/CouponSetting",{params:t});return e==null?void 0:e.data},this.getByIdAsync=async t=>{const e=await g.get(`CustomerClub/CouponSetting/${t}`);return e==null?void 0:e.data},this.deleteAsync=async t=>{const e=await g.delete(`CustomerClub/CouponSetting/${t}`);return e==null?void 0:e.data},this.toggleAsync=async t=>{const e=await g.post(`CustomerClub/CouponSetting/${t}/Toggle`);return e==null?void 0:e.data}}}const N=o.createContext(null),K=({children:i})=>{const{getAsync:t,addAsync:e,updateAsync:h,getByIdAsync:x,deleteAsync:u,toggleAsync:d}=new z,r=W(),[f,n]=o.useState(!1),[a,j]=o.useState(null),[c,D]=o.useState(null),[k,B]=o.useState(null),b=async(l=1,m=25)=>{try{n(!0);const{data:p}=await t({ignoreValidationLimit:!0,pageNumber:l,pageSize:m});j(p),D(null)}finally{n(!1)}},E={states:{loading:f,data:a,detail:k,selectedItem:c},actions:{setSelectedItem:D},handlers:{fetchData:b,fetchDetail:async()=>{try{if(n(!0),!c)return;const{data:l}=await x(c.id);B(l)}finally{n(!1)}},handleSubmitData:async l=>{try{n(!0);let m=null;if(c){const{message:p}=await h({...l,id:c.id});m=p}else{const{message:p}=await e(l);m=p}if(!m)return;I.success(m),b()}finally{n(!1)}},handleDelete:()=>{try{if(n(!0),!c)return;r({content:"آیا از حذف این کد تخفیف اطمینان دارید؟"}).then(async()=>{const{message:l}=await u(c.id);I.success(l),b()}).catch(()=>{})}finally{n(!1)}}}};return s.jsx(N.Provider,{value:E,children:i})},P=()=>o.useContext(N),L=({onClose:i})=>{const{states:{selectedItem:t,detail:e},actions:{setSelectedItem:h},handlers:{handleSubmitData:x,fetchDetail:u}}=P(),{control:d,reset:r,handleSubmit:f,formState:{errors:n}}=F({defaultValues:{discountPercent:0,scoresNeeded:0,title:""}});return o.useEffect(()=>{e&&r({discountPercent:(e==null?void 0:e.discountPercent)??0,scoresNeeded:(e==null?void 0:e.scoresNeeded)??0,title:(e==null?void 0:e.title)??""})},[e]),o.useEffect(()=>{t&&u()},[t]),s.jsxs(R,{onClose:i,title:t?"ویرایش کد تخفیف":"افزودن کد تخفیف",maxWidth:"sm",isFixWidth:!0,children:[s.jsx(v,{className:"flex-1 m-1 !p-2",children:s.jsxs(y,{spacing:2,component:"form",children:[s.jsx(A,{name:"title",control:d,rules:{required:!0},render:({field:a})=>s.jsx(T,{...a,label:"عنوان",fullWidth:!0,error:!!n.title})}),s.jsx(A,{name:"scoresNeeded",control:d,rules:{required:!1},render:({field:a})=>s.jsx(w,{...a,label:"امتیاز مورد نیاز",fullWidth:!0,error:!!n.scoresNeeded,minValue:0})}),s.jsx(A,{name:"discountPercent",control:d,rules:{required:!1},render:({field:a})=>s.jsx(w,{...a,label:"درصد تخفیف",fullWidth:!0,error:!!n.discountPercent,minValue:0,maxValue:100})})]})}),s.jsx($,{onClose:i,onSubmit:f(x)})]})},M=()=>{const{states:{selectedItem:i,data:t,loading:e},actions:{setSelectedItem:h},handlers:{handleDelete:x,fetchData:u}}=P(),[d,r]=o.useState(!1),f=()=>{r(!1),h(null)},n=[{title:"عنوان",key:"title",dataIndex:"title",width:150},{title:"امتیاز مورد نیاز",key:"scoresNeeded",dataIndex:"scoresNeeded",width:100},{title:"درصد تخفیف",key:"discountPercent",dataIndex:"discountPercent",width:100}];return s.jsxs(y,{className:"h-full",children:[s.jsxs(y,{direction:"row",alignItems:"center",spacing:2,children:[s.jsx(C,{onClick:()=>r(!0),children:s.jsx(S.AddIcon,{color:"success"})}),s.jsx(C,{disabled:!i,onClick:()=>r(!0),children:s.jsx(S.EditIcon,{color:"primary"})}),s.jsx(C,{disabled:!i,onClick:()=>x(),children:s.jsx(S.DeleteIcon,{color:"error"})}),s.jsx(C,{onClick:()=>u(),children:s.jsx(S.RefreshIcon,{color:"info"})})]}),s.jsx(y,{className:"h-full",children:s.jsx(q,{columns:n,data:(t==null?void 0:t.list)??[],rowKey:"id",loading:e,totalCount:(t==null?void 0:t.pagination.totalCount)??0,pageNumber:((t==null?void 0:t.pagination.pageNumber)??1)-1,pageSize:(t==null?void 0:t.pagination.pageSize)??25,onPaginationLoad:(a,j)=>u(a+1,j),onClick:a=>h(a)})}),d&&s.jsx(L,{onClose:f})]})},Z=()=>s.jsxs(y,{className:"h-full p-2 px-4",children:[s.jsx(V,{prevRoutes:[{path:"/",title:"خدمات"}],currentRoute:"کد تخفیف"}),s.jsx(K,{children:s.jsx(M,{})})]});export{Z as default};
