import{K as g,r as d,j as u,B as U,b as t,T as f,aO as x,aP as A,a6 as L,aQ as N,b8 as v,b9 as k,a7 as w,b1 as I,a as O,ba as G,U as W,A as q,e as D,H as F,I as y,S as P}from"./index-CYy8zBsj.js";const z=g.injectEndpoints({endpoints:a=>({getUomList:a.query({query:()=>({url:"uom",method:"GET"})}),getUomById:a.query({query:e=>({url:`uom/${e}`,method:"GET"})}),saveUom:a.mutation({query:e=>({url:"uom",method:"POST",body:e})}),updateUom:a.mutation({query:e=>({url:`uom/${e.id}`,method:"PUT",body:e})}),deleteUom:a.mutation({query:e=>({url:`uom/${e}`,method:"DELETE"})}),getUomConversions:a.query({query:()=>({url:"uom/conversions",method:"GET"})}),saveUomConversion:a.mutation({query:e=>({url:"uom/conversions",method:"POST",body:e})})})}),{useLazyGetUomListQuery:X,useGetUomListQuery:j,useSaveUomMutation:B,useUpdateUomMutation:H,useDeleteUomMutation:Q,useGetUomConversionsQuery:Z,useSaveUomConversionMutation:ee}=z,$=["WEIGHT","VOLUME","UNIT","LENGTH"],T={name:"",code:"",uomType:"UNIT",active:!0};function V({open:a,initial:e,onClose:m,onSubmit:l}){const[o,c]=d.useState(T);d.useEffect(()=>{c(e??T)},[e,a]);const r=(s,p)=>c(h=>({...h,[s]:p})),i=()=>{!o.name||!o.code||!o.uomType||l(o)};return u(I,{open:a,title:e!=null&&e.id?"Edit UoM":"Add UoM",onClose:m,maxWidth:"sm",children:[u(U,{display:"flex",flexDirection:"column",gap:2,pt:1,children:[t(f,{label:"Name *",size:"small",fullWidth:!0,value:o.name??"",onChange:s=>r("name",s.target.value)}),t(f,{label:"Code *",size:"small",fullWidth:!0,value:o.code??"",onChange:s=>r("code",s.target.value)}),u(x,{size:"small",fullWidth:!0,children:[t(A,{children:"UoM Type *"}),t(N,{label:"UoM Type *",value:o.uomType??"UNIT",onChange:s=>r("uomType",s.target.value),children:$.map(s=>t(L,{value:s,children:s},s))})]}),t(k,{control:t(v,{checked:!!o.active,onChange:s=>r("active",s.target.checked)}),label:"Active"})]}),t(w,{onSubmit:i,submitLabel:e!=null&&e.id?"Update":"Save",submitBtnType:"submit"})]})}const _={WEIGHT:"primary",VOLUME:"secondary",UNIT:"success",LENGTH:"warning"};function oe(){const{openSnackbar:a}=O(),e=G(),[m,l]=d.useState(!1),[o,c]=d.useState(null),{data:r=[],isLoading:i,error:s}=j(),[p]=B(),[h]=H(),[C]=Q(),S=()=>{c(null),l(!0)},E=n=>{c(n),l(!0)},b=async n=>{if(!await e({message:"Delete this UoM?"}))return;"data"in await C(n)?a("UoM deleted successfully","success"):a("Failed to delete UoM","error")};return u(U,{p:2,children:[u(W,{children:[t(U,{display:"flex",justifyContent:"flex-end",mb:2,children:t(q,{btnType:"create",onClick:S,children:"Add UoM"})}),t(D,{columns:[{field:"name",headerName:"Name",flex:1,minWidth:160},{field:"code",headerName:"Code",flex:1,minWidth:100},{field:"uomType",headerName:"Type",flex:1,minWidth:120,renderCell:n=>t(F,{label:n.value,color:_[n.value]??"default",size:"small"})},{field:"active",headerName:"Active",flex:1,minWidth:80,renderCell:n=>t(v,{checked:!!n.value,disabled:!0,size:"small"})},{field:"actions",headerName:"Actions",flex:1,minWidth:120,sortable:!1,renderCell:n=>u(P,{direction:"row",spacing:1,children:[t(y,{type:"Edit",onClick:()=>E(n.row)}),t(y,{type:"Delete",onClick:()=>b(n.row.id)})]})}],rows:r,loading:i})]}),t(V,{open:m,initial:o,onClose:()=>l(!1),onSubmit:async n=>{"data"in(o!=null&&o.id?await h({...n,id:o.id}):await p(n))?(a(`UoM ${o!=null&&o.id?"updated":"created"} successfully`,"success"),l(!1)):a("Failed to save UoM","error")}})]})}export{oe as default};
