import{a as F,r as l,j as i,b as e,f as W,G as v,T as x,g as A,i as I,bn as T,D as j,ba as q,B as S,aM as B,w as E,x as M,y as k,z as a,F as P,S as z,I as L,v as O}from"./index-CYy8zBsj.js";import{u as U,a as H,b as Q,c as R}from"./CustomerGroupApi-Dz_yEVlc.js";import{A as J}from"./Add-CJyM08CI.js";const K=({open:f,initialData:w,isEdit:u,onSaved:b,onClose:c})=>{const{openSnackbar:d}=F(),[p,{isLoading:N}]=U(),[G,{isLoading:h}]=H(),[o,m]=l.useState(()=>w||{}),[s,D]=l.useState({}),g=(r,n)=>{m(C=>({...C,[r]:n})),s[r]&&D(C=>({...C,[r]:""}))},y=()=>{var n,C;const r={};return(n=o.groupName)!=null&&n.trim()||(r.groupName="Group name is required"),(C=o.groupCode)!=null&&C.trim()||(r.groupCode="Group code is required"),D(r),Object.keys(r).length===0},t=async()=>{var r;if(y())try{u?(await G(o).unwrap(),d("Customer group updated","success")):(await p(o).unwrap(),d("Customer group created","success")),b==null||b()}catch(n){d(((r=n==null?void 0:n.data)==null?void 0:r.message)||"Failed to save","error")}};return i(j,{open:f,onClose:c,maxWidth:"sm",fullWidth:!0,children:[e(W,{children:u?"Edit Customer Group":"New Customer Group"}),e(A,{children:i(v,{container:!0,spacing:2,sx:{mt:.5},children:[e(v,{item:!0,xs:12,sm:6,children:e(x,{fullWidth:!0,required:!0,label:"Group Code",value:o.groupCode||"",onChange:r=>g("groupCode",r.target.value),error:!!s.groupCode,helperText:s.groupCode,disabled:u})}),e(v,{item:!0,xs:12,sm:6,children:e(x,{fullWidth:!0,required:!0,label:"Group Name",value:o.groupName||"",onChange:r=>g("groupName",r.target.value),error:!!s.groupName,helperText:s.groupName})}),e(v,{item:!0,xs:12,children:e(x,{fullWidth:!0,multiline:!0,rows:2,label:"Description",value:o.description||"",onChange:r=>g("description",r.target.value)})}),e(v,{item:!0,xs:12,sm:6,children:e(x,{fullWidth:!0,type:"number",label:"Discount %",value:o.discountPercent??"",onChange:r=>g("discountPercent",Number(r.target.value)||void 0)})})]})}),i(I,{children:[e(T,{onClick:c,children:"Cancel"}),e(T,{variant:"contained",onClick:t,disabled:N||h,children:u?"Update":"Create"})]})]})},Z=()=>{const{openSnackbar:f}=F(),w=q(),{data:u=[],isLoading:b,refetch:c}=Q(),[d]=R(),[p,N]=l.useState(""),[G,h]=l.useState(!1),[o,m]=l.useState(),s=u.filter(t=>{var r,n;return!p.trim()||((r=t.groupName)==null?void 0:r.toLowerCase().includes(p.toLowerCase()))||((n=t.groupCode)==null?void 0:n.toLowerCase().includes(p.toLowerCase()))}),D=l.useCallback(t=>{m(t),h(!0)},[]),g=l.useCallback(()=>{h(!1),m(void 0),c()},[c]),y=l.useCallback(async t=>{if(await w({message:"Delete this customer group?"}))try{await d(t).unwrap(),f("Customer group deleted","success"),c()}catch{f("Failed to delete","error")}},[d,c,f,w]);return i(S,{p:2,children:[i(S,{display:"flex",gap:2,mb:2,alignItems:"center",children:[e(x,{size:"small",placeholder:"Search name, code...",value:p,onChange:t=>N(t.target.value),sx:{minWidth:250}}),e(S,{flex:1}),e(T,{variant:"contained",startIcon:e(J,{}),onClick:()=>{m(void 0),h(!0)},children:"New Customer Group"})]}),e(O,{component:B,variant:"outlined",children:i(E,{size:"small",children:[e(M,{children:i(k,{children:[e(a,{children:"Code"}),e(a,{children:"Name"}),e(a,{children:"Description"}),e(a,{align:"right",children:"Discount %"}),e(a,{align:"center",children:"Actions"})]})}),e(P,{children:b?e(k,{children:e(a,{colSpan:5,align:"center",children:"Loading..."})}):s.length===0?e(k,{children:e(a,{colSpan:5,align:"center",children:"No customer groups found"})}):s.map(t=>i(k,{hover:!0,children:[e(a,{children:t.groupCode}),e(a,{children:t.groupName}),e(a,{children:t.description||"-"}),e(a,{align:"right",children:t.discountPercent??"-"}),e(a,{align:"center",children:i(z,{direction:"row",spacing:.5,alignItems:"center",justifyContent:"center",children:[e(L,{type:"Edit",onClick:()=>D(t)}),e(L,{type:"Delete",onClick:()=>y(t.id)})]})})]},t.id))})]})}),G&&e(K,{open:G,initialData:o,isEdit:!!o,onSaved:g,onClose:()=>{h(!1),m(void 0)}})]})};export{Z as default};
