import{K as S,r as c,j as l,B as p,b as e,T as y,aO as D,aP as W,a6 as w,aQ as T,b8 as u,b9 as C,a7 as k,b1 as G,a as N,ba as F,U as M,A as O,e as z,H as q,I as v,S as L}from"./index-CYy8zBsj.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:H,useLazyGetInventoryConfigsQuery:X,useSaveInventoryConfigMutation:_,useUpdateInventoryConfigMutation:j,useDeleteInventoryConfigMutation:V}=B,P=["FIFO","WEIGHTED_AVG","STANDARD"],I={name:"",code:"",costingMethod:"WEIGHTED_AVG",expiryWarningDays:30,allowIssueExpired:!1,active:!0};function U({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 $={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(),[h]=_(),[m]=j(),[x]=V(),E=a=>{d(a),r(!0)},b=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:$[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:()=>E(a.row)}),e(v,{type:"Delete",onClick:()=>b(a.row.id)})]})}],rows:s,loading:g})]}),e(U,{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};
