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