import{L as b,r as d,j as u,b as t,B as U,T as f,ax as x,bI as L,R as A,ay as I,c6 as v,aL as N,Z as k,Y as w,a as G,c7 as O,Q as W,A as q,e as D,E as F,I as y,S as z}from"./index-ktJf6c6X.js";const P=b.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:K,useGetUomListQuery:j,useSaveUomMutation:B,useUpdateUomMutation:H,useDeleteUomMutation:Q,useGetUomConversionsQuery:X,useSaveUomConversionMutation:ee}=P,$=["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(w,{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(L,{children:"UoM Type *"}),t(I,{label:"UoM Type *",value:o.uomType??"UNIT",onChange:s=>r("uomType",s.target.value),children:$.map(s=>t(A,{value:s,children:s},s))})]}),t(N,{control:t(v,{checked:!!o.active,onChange:s=>r("active",s.target.checked)}),label:"Active"})]}),t(k,{onSubmit:i,submitLabel:e!=null&&e.id?"Update":"Save",submitBtnType:"submit"})]})}const Y={WEIGHT:"primary",VOLUME:"secondary",UNIT:"success",LENGTH:"warning"};function oe(){const{openSnackbar:a}=G(),e=O(),[m,l]=d.useState(!1),[o,c]=d.useState(null),{data:r=[],isLoading:i,error:s}=j(),[p]=B(),[h]=H(),[C]=Q(),E=()=>{c(null),l(!0)},S=n=>{c(n),l(!0)},M=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:E,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:Y[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(z,{direction:"row",spacing:1,children:[t(y,{type:"Edit",onClick:()=>S(n.row)}),t(y,{type:"Delete",onClick:()=>M(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};
