import{a as b,h as d,j as e,k as D,H as S,u as W,l as T,A as w,i as G,I as C}from"./index-Da-VPv7f.js";import{r as f}from"./mui-icons-CVqIXJUc.js";import{l as u,k as p,t as k,u as N,c as F,q as M,r as m,s as v,C as O}from"./mui-x-datagrid-D42xrz8e.js";import"./mui-x-charts-CP3e2jhe.js";import"./mui-x-pickers-BflNfF54.js";const q=b.injectEndpoints({endpoints:a=>({getInventoryConfigs:a.query({query:()=>({url:"inventory-configurations",method:"GET"})}),getInventoryConfigById:a.query({query:n=>({url:`inventory-configurations/${n}`,method:"GET"})}),saveInventoryConfig:a.mutation({query:n=>({url:"inventory-configurations",method:"POST",body:n})}),updateInventoryConfig:a.mutation({query:n=>({url:`inventory-configurations/${n.id}`,method:"PUT",body:n})}),deleteInventoryConfig:a.mutation({query:n=>({url:`inventory-configurations/${n}`,method:"DELETE"})})})}),{useGetInventoryConfigsQuery:z,useLazyGetInventoryConfigsQuery:X,useSaveInventoryConfigMutation:L,useUpdateInventoryConfigMutation:H,useDeleteInventoryConfigMutation:_}=q,j=["FIFO","WEIGHTED_AVG","STANDARD"],I={name:"",code:"",costingMethod:"WEIGHTED_AVG",expiryWarningDays:30,allowIssueExpired:!1,active:!0};function B({open:a,initial:n,onClose:l,onSubmit:r}){const[i,c]=f.useState(I);f.useEffect(()=>{c(n??I)},[n,a]);const s=(t,h)=>c(g=>({...g,[t]:h})),y=()=>{!i.name||!i.code||r(i)};return d(S,{open:a,title:n!=null&&n.id?"Edit Inventory Configuration":"Add Inventory Configuration",onClose:l,maxWidth:"sm",children:[d(u,{display:"flex",flexDirection:"column",gap:2,pt:1,children:[e(p,{label:"Name *",size:"small",fullWidth:!0,value:i.name??"",onChange:t=>s("name",t.target.value)}),e(p,{label:"Code *",size:"small",fullWidth:!0,value:i.code??"",onChange:t=>s("code",t.target.value)}),d(k,{size:"small",fullWidth:!0,children:[e(N,{children:"Costing Method"}),e(M,{label:"Costing Method",value:i.costingMethod??"WEIGHTED_AVG",onChange:t=>s("costingMethod",t.target.value),children:j.map(t=>e(F,{value:t,children:t},t))})]}),e(p,{label:"Expiry Warning Days",size:"small",fullWidth:!0,type:"number",value:i.expiryWarningDays??30,onChange:t=>s("expiryWarningDays",Number(t.target.value))}),e(v,{control:e(m,{checked:!!i.allowIssueExpired,onChange:t=>s("allowIssueExpired",t.target.checked)}),label:"Allow Issue Expired Batches"}),e(v,{control:e(m,{checked:!!i.active,onChange:t=>s("active",t.target.checked)}),label:"Active"})]}),e(D,{onSubmit:y,submitLabel:n!=null&&n.id?"Update":"Save",submitBtnType:"submit"})]})}const V={FIFO:"primary",WEIGHTED_AVG:"success",STANDARD:"warning"};function Y(){const{openSnackbar:a}=W(),[n,l]=f.useState(!1),[r,i]=f.useState(null),{data:c=[],isLoading:s,error:y}=z(),[t]=L(),[h]=H(),[g]=_(),x=o=>{i(o),l(!0)},E=async o=>{"data"in await g(o)?a("Configuration deleted","success"):a("Failed to delete configuration","error")};return d(u,{p:2,children:[d(T,{children:[e(u,{display:"flex",justifyContent:"flex-end",mb:2,children:e(w,{btnType:"create",onClick:()=>{i(null),l(!0)},children:"Add Configuration"})}),e(G,{columns:[{field:"name",headerName:"Name",flex:1,minWidth:180},{field:"code",headerName:"Code",flex:1,minWidth:120},{field:"costingMethod",headerName:"Costing Method",flex:1,minWidth:150,renderCell:o=>e(O,{label:o.value,color:V[o.value]??"default",size:"small"})},{field:"expiryWarningDays",headerName:"Expiry Warning (days)",flex:1,minWidth:160},{field:"allowIssueExpired",headerName:"Allow Issue Expired",flex:1,minWidth:140,renderCell:o=>e(m,{checked:!!o.value,disabled:!0,size:"small"})},{field:"active",headerName:"Active",flex:1,minWidth:80,renderCell:o=>e(m,{checked:!!o.value,disabled:!0,size:"small"})},{field:"actions",headerName:"Actions",flex:1,minWidth:120,sortable:!1,renderCell:o=>d(u,{display:"flex",gap:1,children:[e(C,{type:"Edit",onClick:()=>x(o.row)}),e(C,{type:"Delete",onClick:()=>E(o.row.id)})]})}],rows:c,loading:s})]}),e(B,{open:n,initial:r,onClose:()=>l(!1),onSubmit:async o=>{"data"in(r!=null&&r.id?await h({...o,id:r.id}):await t(o))?(a(`Configuration ${r!=null&&r.id?"updated":"created"}`,"success"),l(!1)):a("Failed to save configuration","error")}})]})}export{Y as default};
