import{a as W,r as l,j as i,b as e,f as A,G as v,T as x,g as F,i as I,$ as T,D as j,bQ as E,B as S,W as q,w as B,x as P,y as k,z as a,E as z,S as M,I as L,v as O}from"./index-BiWH3VAt.js";import{u as Q,a as U,b as H,c as R}from"./CustomerGroupApi-CmH3h-Yz.js";import{A as $}from"./Add-Dk5aKU9x.js";const J=({open:f,initialData:w,isEdit:u,onSaved:b,onClose:c})=>{const{openSnackbar:d}=W(),[p,{isLoading:N}]=Q(),[G,{isLoading:h}]=U(),[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(A,{children:u?"Edit Customer Group":"New Customer Group"}),e(F,{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"})]})]})},Y=()=>{const{openSnackbar:f}=W(),w=E(),{data:u=[],isLoading:b,refetch:c}=H(),[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($,{}),onClick:()=>{m(void 0),h(!0)},children:"New Customer Group"})]}),e(O,{component:q,variant:"outlined",children:i(B,{size:"small",children:[e(P,{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(z,{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(M,{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(J,{open:G,initialData:o,isEdit:!!o,onSaved:g,onClose:()=>{h(!1),m(void 0)}})]})};export{Y as default};
