import{r as f,b5 as T,a as $,j as m,b as l,B as S,T as k,b6 as B,b7 as I,a6 as P,a$ as j,b8 as O,b9 as z,ba as M,bb as U,q,P as R,A as G,e as H,I as E,S as Z}from"./index-DWFi48i_.js";import{W as Q}from"./Warehouse-oflV5CfC.js";const F={levelName:"",prefix:"",isStorage:!1};function V({open:s,initial:t,onClose:C,onSubmit:d}){const[a,v]=f.useState(F),[r,h]=f.useState({}),[p,w]=f.useState(!1),{data:g}=T(void 0,{refetchOnMountOrArgChange:!0}),b=g??[],W=g!==void 0,{openSnackbar:N}=$(),x=!!(t!=null&&t.id);f.useEffect(()=>{v(t??F),h({}),w(!1)},[t,s]);const c=(e,o)=>{v(n=>({...n,[e]:o})),h(n=>({...n,[e]:""}))},A=()=>{var o,n;const e={};if(a.level==null||a.level<0)e.level="Level number is required and must be 0 or greater";else{const u=b.find(i=>i.level===a.level&&i.id!==(t==null?void 0:t.id));u&&(e.level=`Level ${a.level} already exists (${u.levelName})`)}if((o=a.levelName)!=null&&o.trim()?b.find(i=>{var L,y;return((L=i.levelName)==null?void 0:L.toLowerCase())===((y=a.levelName)==null?void 0:y.trim().toLowerCase())&&i.id!==(t==null?void 0:t.id)})&&(e.levelName=`"${a.levelName.trim()}" already exists`):e.levelName="Level name is required",(n=a.prefix)!=null&&n.trim()){const u=b.find(i=>{var L,y;return((L=i.prefix)==null?void 0:L.toLowerCase())===((y=a.prefix)==null?void 0:y.trim().toLowerCase())&&i.id!==(t==null?void 0:t.id)});u&&(e.prefix=`Prefix "${a.prefix.trim()}" already used by ${u.levelName}`)}return h(e),Object.keys(e).length===0},D=async()=>{if(!p){if(!W){N("Loading existing levels — please wait a moment and try again","warning");return}if(!A()){N("Please fix the highlighted errors","error");return}w(!0);try{await d(a)}finally{w(!1)}}};return m(j,{open:s,title:x?"Edit Level Config":"Add Level Config",onClose:C,maxWidth:"sm",children:[m(S,{display:"flex",flexDirection:"column",gap:2,pt:1,children:[l(k,{label:"Level Number *",size:"small",fullWidth:!0,type:"number",value:a.level??"",onChange:e=>c("level",Number(e.target.value)),helperText:r.level,error:!!r.level,placeholder:"1 = top level,  2= child, 3 = grandchild"}),l(k,{label:"Level Name *",size:"small",fullWidth:!0,value:a.levelName??"",onChange:e=>c("levelName",e.target.value),helperText:r.levelName,error:!!r.levelName,placeholder:"e.g. Warehouse, Zone, Rack, Bin"}),l(k,{label:"Prefix",size:"small",fullWidth:!0,value:a.prefix??"",onChange:e=>c("prefix",e.target.value),helperText:r.prefix,error:!!r.prefix,placeholder:"e.g. WH, ZN, BN — used for auto-numbering"}),l(I,{control:l(B,{checked:!!a.isStorage,onChange:e=>c("isStorage",e.target.checked)}),label:"Is Storage Level (locations at this level can hold stock)"})]}),l(P,{onSubmit:D,submitLabel:p?x?"Updating...":"Saving...":x?"Update":"Save",submitBtnType:"submit",disabled:p})]})}function X(){const{openSnackbar:s}=$(),t=O(),[C,d]=f.useState(!1),[a,v]=f.useState(null),{data:r=[],isLoading:h,isFetching:p,error:w,refetch:g}=T(),[b]=z(),[W]=M(),[N]=U(),x=e=>{v(e),d(!0)},c=async e=>{if(!await t({message:"Delete this warehouse level?"}))return;if("data"in await N(e))try{await g().unwrap(),s("Level deleted","success")}catch{s("Deleted successfully, but failed to refresh latest data","warning")}else s("Failed to delete level","error")};return m(S,{p:3,sx:{width:"100%",minHeight:"100vh"},children:[m(S,{display:"flex",alignItems:"center",gap:1,mb:2,children:[l(Q,{sx:{color:"primary.main"}}),l(q,{variant:"h6",fontWeight:600,children:"Warehouse"})]}),m(R,{children:[l(S,{display:"flex",justifyContent:"flex-end",mb:2,children:l(G,{btnType:"create",onClick:()=>{v(null),d(!0)},children:"Add Level"})}),l(H,{columns:[{field:"level",headerName:"Level",flex:1,minWidth:80},{field:"levelName",headerName:"Level Name",flex:1,minWidth:150},{field:"prefix",headerName:"Prefix",flex:1,minWidth:100},{field:"isStorage",headerName:"Is Storage",flex:1,minWidth:100,renderCell:e=>l(B,{checked:!!e.value,disabled:!0,size:"small"})},{field:"actions",headerName:"Actions",flex:1,minWidth:120,sortable:!1,renderCell:e=>m(Z,{direction:"row",spacing:1,children:[l(E,{type:"Edit",onClick:()=>x(e.row)}),l(E,{type:"Delete",onClick:()=>c(e.row.id)})]})}],rows:r,loading:h||p})]}),l(V,{open:C,initial:a,onClose:()=>d(!1),onSubmit:async e=>{const o=!!(a!=null&&a.id);if("data"in(o?await W({...e,id:a.id}):await b(e))){try{await g().unwrap(),s(`Level ${o?"updated":"created"}`,"success")}catch{s("Saved successfully, but failed to refresh latest data","warning")}d(!1)}else s("Failed to save level","error")}})]})}export{X as default};
