import{a as W,r as l,j as s,b as e,f as A,G as v,T as x,g as F,i as I,bl as T,D as j,b8 as q,B as S,aK as B,v as E,w as P,x as k,y as a,z,S as M,I as L,s as O}from"./index-DWFi48i_.js";import{u as U,a as H,b as K,c as Q}from"./CustomerGroupApi-BKb7Y6qE.js";import{A as R}from"./Add-Mz7E_Hwf.js";const J=({open:f,initialData:w,isEdit:u,onSaved:b,onClose:c})=>{const{openSnackbar:d}=W(),[p,{isLoading:N}]=U(),[G,{isLoading:h}]=H(),[o,m]=l.useState(()=>w||{}),[i,D]=l.useState({}),g=(r,n)=>{m(C=>({...C,[r]:n})),i[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 s(j,{open:f,onClose:c,maxWidth:"sm",fullWidth:!0,children:[e(A,{children:u?"Edit Customer Group":"New Customer Group"}),e(F,{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:r=>g("groupCode",r.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:r=>g("groupName",r.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: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)})})]})}),s(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}=W(),w=q(),{data:u=[],isLoading:b,refetch:c}=K(),[d]=Q(),[p,N]=l.useState(""),[G,h]=l.useState(!1),[o,m]=l.useState(),i=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 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:t=>N(t.target.value),sx:{minWidth:250}}),e(S,{flex:1}),e(T,{variant:"contained",startIcon:e(R,{}),onClick:()=>{m(void 0),h(!0)},children:"New Customer Group"})]}),e(O,{component:B,variant:"outlined",children:s(E,{size:"small",children:[e(P,{children:s(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..."})}):i.length===0?e(k,{children:e(a,{colSpan:5,align:"center",children:"No customer groups found"})}):i.map(t=>s(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:s(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{Z as default};
