import{L as S,r as c,j as l,b as e,B as p,T as y,ax as D,bI as W,R as w,ay as T,c6 as u,aL as C,Z as k,Y as G,a as N,c7 as F,Q as M,A as O,e as L,E as z,I as v,S as q}from"./index-ktJf6c6X.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:J,useSaveInventoryConfigMutation:j,useUpdateInventoryConfigMutation:H,useDeleteInventoryConfigMutation:V}=B,R=["FIFO","WEIGHTED_AVG","STANDARD"],I={name:"",code:"",costingMethod:"WEIGHTED_AVG",expiryWarningDays:30,allowIssueExpired:!1,active:!0};function $({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:R.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 P={FIFO:"primary",WEIGHTED_AVG:"success",STANDARD:"warning"};function K(){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(),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(L,{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(z,{label:a.value,color:P[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(q,{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($,{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{K as default};
