import{L as C,r as u,j as i,b as o,B as b,T as m,c6 as S,aJ as I,Z as A,Y as k,a as D,c7 as E,Q as W,A as w,e as N,I as v,S as L}from"./index-DlNZgIG9.js";import{S as T}from"./StorageLocationPicker-D8cb1bBZ.js";const q=C.injectEndpoints({endpoints:a=>({getReorderRules:a.query({query:()=>({url:"reorder-rules",method:"GET"})}),getReorderRuleById:a.query({query:e=>({url:`reorder-rules/${e}`,method:"GET"})}),saveReorderRule:a.mutation({query:e=>({url:"reorder-rules",method:"POST",body:e})}),updateReorderRule:a.mutation({query:e=>({url:`reorder-rules/${e.id}`,method:"PUT",body:e})}),deleteReorderRule:a.mutation({query:e=>({url:`reorder-rules/${e}`,method:"DELETE"})})})}),{useGetReorderRulesQuery:F,useSaveReorderRuleMutation:M,useUpdateReorderRuleMutation:P,useDeleteReorderRuleMutation:j}=q,g={productId:void 0,locationId:void 0,minQty:0,maxQty:0,reorderQty:0,active:!0};function z({open:a,initial:e,onClose:h,onSubmit:n}){const[r,s]=u.useState(g),[p,c]=u.useState(null);u.useEffect(()=>{s(e??g),c(null)},[e,a]);const l=(t,R)=>s(x=>({...x,[t]:R})),f=t=>{c(t),l("locationId",(t==null?void 0:t.id)??void 0)},y=()=>{!r.productId||!r.locationId||n(r)};return i(k,{open:a,title:e!=null&&e.id?"Edit Reorder Rule":"Add Reorder Rule",onClose:h,maxWidth:"sm",children:[i(b,{display:"flex",flexDirection:"column",gap:2,pt:1,children:[o(m,{label:"Product ID *",size:"small",fullWidth:!0,type:"number",value:r.productId??"",onChange:t=>l("productId",Number(t.target.value)||void 0)}),o(T,{value:p,onChange:f,required:!0}),o(m,{label:"Min Qty",size:"small",fullWidth:!0,type:"number",value:r.minQty??0,onChange:t=>l("minQty",Number(t.target.value))}),o(m,{label:"Max Qty",size:"small",fullWidth:!0,type:"number",value:r.maxQty??0,onChange:t=>l("maxQty",Number(t.target.value))}),o(m,{label:"Reorder Qty",size:"small",fullWidth:!0,type:"number",value:r.reorderQty??0,onChange:t=>l("reorderQty",Number(t.target.value)),helperText:"0 = order up to max qty"}),o(I,{control:o(S,{checked:!!r.active,onChange:t=>l("active",t.target.checked)}),label:"Active"})]}),o(A,{onSubmit:y,submitLabel:e!=null&&e.id?"Update":"Save",submitBtnType:"submit"})]})}function J(){const{openSnackbar:a}=D(),e=E(),[h,n]=u.useState(!1),[r,s]=u.useState(null),{data:p=[],isLoading:c,error:l}=F(),[f]=M(),[y]=P(),[t]=j(),R=d=>{s(d),n(!0)},x=async d=>{if(!await e({message:"Delete this reorder rule?"}))return;"data"in await t(d)?a("Reorder rule deleted","success"):a("Failed to delete reorder rule","error")};return i(b,{p:2,children:[i(W,{children:[o(b,{display:"flex",justifyContent:"flex-end",mb:2,children:o(w,{btnType:"create",onClick:()=>{s(null),n(!0)},children:"Add Rule"})}),o(N,{columns:[{field:"productId",headerName:"Product ID",flex:1,minWidth:110},{field:"locationId",headerName:"Location ID",flex:1,minWidth:110},{field:"minQty",headerName:"Min Qty",flex:1,minWidth:100},{field:"maxQty",headerName:"Max Qty",flex:1,minWidth:100},{field:"reorderQty",headerName:"Reorder Qty",flex:1,minWidth:120},{field:"active",headerName:"Active",flex:1,minWidth:80,renderCell:d=>o(S,{checked:!!d.value,disabled:!0,size:"small"})},{field:"actions",headerName:"Actions",flex:1,minWidth:120,sortable:!1,renderCell:d=>i(L,{direction:"row",spacing:1,children:[o(v,{type:"Edit",onClick:()=>R(d.row)}),o(v,{type:"Delete",onClick:()=>x(d.row.id)})]})}],rows:p,loading:c})]}),o(z,{open:h,initial:r,onClose:()=>n(!1),onSubmit:async d=>{"data"in(r!=null&&r.id?await y({...d,id:r.id}):await f(d))?(a(`Reorder rule ${r!=null&&r.id?"updated":"created"}`,"success"),n(!1)):a("Failed to save reorder rule","error")}})]})}export{J as default};
