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