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