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