import{r as f,c5 as T,a as B,j as m,b as l,B as S,T as k,c6 as I,aJ as $,Z as j,Y as O,c7 as P,c8 as z,c9 as M,ca as U,q,Q as R,A as Z,e as G,I as E,S as H}from"./index-DlNZgIG9.js";import{W as Q}from"./Warehouse-D1JfcIxl.js";const F={levelName:"",prefix:"",isStorage:!1};function J({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}),x=g??[],W=g!==void 0,{openSnackbar:N}=B(),b=!!(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=x.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()?x.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=x.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(O,{open:s,title:b?"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($,{control:l(I,{checked:!!a.isStorage,onChange:e=>c("isStorage",e.target.checked)}),label:"Is Storage Level (locations at this level can hold stock)"})]}),l(j,{onSubmit:D,submitLabel:p?b?"Updating...":"Saving...":b?"Update":"Save",submitBtnType:"submit",disabled:p})]})}function K(){const{openSnackbar:s}=B(),t=P(),[C,d]=f.useState(!1),[a,v]=f.useState(null),{data:r=[],isLoading:h,isFetching:p,error:w,refetch:g}=T(),[x]=z(),[W]=M(),[N]=U(),b=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(Z,{btnType:"create",onClick:()=>{v(null),d(!0)},children:"Add Level"})}),l(G,{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(I,{checked:!!e.value,disabled:!0,size:"small"})},{field:"actions",headerName:"Actions",flex:1,minWidth:120,sortable:!1,renderCell:e=>m(H,{direction:"row",spacing:1,children:[l(E,{type:"Edit",onClick:()=>b(e.row)}),l(E,{type:"Delete",onClick:()=>c(e.row.id)})]})}],rows:r,loading:h||p})]}),l(J,{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 x(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{K as default};
