import{u as U,O as X,P as $,a as H,N as J,r as n,j as o,b as a,aM as Z,B as l,aN as ee,o as A,p as ae,aO as te,aP as B,s as E,aQ as se,X as ne,aR as oe,Q as N,aS as I,aT as T,Y as ie,c,I as re}from"./index-BiWH3VAt.js";import{u as le,a as ce}from"./BomApi-CQbqJMie.js";import{l as de,a as pe,s as he,b as ue}from"./bomStorage-C187aNfO.js";import{E as me}from"./Edit-D-Mgl6aj.js";import{D as ge}from"./Delete-CeXZNhlr.js";import{K as fe}from"./KeyboardArrowUp-CyccSGSj.js";import{F as be}from"./Fab-CEPbPMUv.js";function Ae(){const i=U(),R=X(),k=$(R.breakpoints.down("md")),[v,{isLoading:f}]=le(),[b]=ce(),{openSnackbar:O}=H(),{create:M,write:P}=J("Master"),[y,d]=n.useState([]),[z,m]=n.useState(0),[p,_]=n.useState(de),[h,L]=n.useState(null),[x,V]=n.useState(null),[u,W]=n.useState(pe),j=async()=>{try{const e=await v({page:u.page,size:u.pageSize,searchByName:{searchValue:p}}).unwrap();e&&e.data?(d(e.data),m(e.totalCount||e.data.length)):(d([]),m(0))}catch{d([]),m(0)}};n.useEffect(()=>{j()},[u,p]);const F=e=>{W(e),ue(e)},S=e=>{_(e),he(e)},Q=e=>{d(t=>t.filter(s=>s.id!==e)),O("BOM deleted successfully!","success")},G=async e=>{try{const s=await b({id:e.id}).unwrap()||e;i(c.MASTER_VIEW.replace(":id",String(e.id)),{state:{initialData:s}})}catch{i(c.MASTER_VIEW.replace(":id",String(e.id)),{state:{initialData:e}})}},C=async e=>{try{const t=await b({id:e.id}).unwrap(),s=(t==null?void 0:t.bomData)||(t==null?void 0:t.data)||t,g=s&&s.bomComponentMetadataDetailsList||[],Y={...s||{},bomComponentMetadataDetailsList:g,materials:Array.isArray(g)?g.map(r=>({id:r.id,code:r.code||"",productName:r.productName||"",unit:r.unit||"",quantity:r.quantity??""})):[]};i(c.MASTER_EDIT.replace(":id",String(e.id)),{state:{initialData:Y}})}catch{i(c.MASTER_EDIT.replace(":id",String(e.id)),{state:{initialData:e}})}},D=()=>{i(c.MASTER_CREATE,{state:{initialData:null}})},w=()=>{V(null),L(null)},q=()=>{h&&Q(h.id),w()},K=[{field:"id",headerName:"ID",flex:1},{field:"productName",headerName:"Product Name",flex:1},{field:"bomName",headerName:"BOM Name",flex:1},{field:"actions",headerName:"Actions",flex:1,sortable:!1,renderCell:e=>a(l,{sx:{display:"flex",gap:.5},children:P&&a(re,{type:"Edit",onClick:t=>{t.stopPropagation(),C(e.row)}})})}];return o(l,{p:2,sx:{width:"100%"},children:[o(Z,{children:[o(l,{className:"wrapper-box",display:"flex",justifyContent:"space-between",flexWrap:"wrap",gap:2,children:[a(l,{className:"filter-box",display:"flex",gap:2,flexWrap:"wrap",children:a(ee,{placeholder:"Search BOM Name...",variant:"outlined",size:"small",value:p,sx:{width:225},onChange:e=>S(e.target.value),InputProps:{startAdornment:a(A,{edge:"start",size:"small",children:a(te,{style:{color:"#bdbdbd"}})}),endAdornment:!!p&&a(A,{edge:"end",onClick:()=>S(""),size:"small",children:a(ae,{})})}})}),M&&a(B,{btnType:"create",onClick:D,children:"Add BOM"})]}),a(ne,{sx:{p:0},children:y.length===0&&!f?o(l,{sx:{mt:3,p:4,border:"1px dashed #ccc",borderRadius:2,textAlign:"center"},children:[a(E,{variant:"h6",gutterBottom:!0,children:"No Data Available"}),a(E,{variant:"body2",color:"text.secondary",mb:3,children:"You haven't added any BOMs yet. Click below to add your first BOM."}),M&&a(B,{btnType:"create",onClick:D,children:"Add BOM"})]}):a(se,{columns:K,rows:y,checkboxSelection:!1,rowCount:z,paginationModel:u,pageSizeOptions:[10,25,50,100],handlePaginationChange:F,loading:f,enableManageColumns:!0,onRowClick:e=>G(e.row)})})]}),o(oe,{anchorEl:x,open:!!x,onClose:w,PaperProps:{elevation:3,sx:{borderRadius:2,minWidth:200}},children:[o(N,{onClick:()=>h&&C(h),children:[a(I,{children:a(me,{fontSize:"small"})}),a(T,{children:"Edit BOM"})]}),a(ie,{}),o(N,{onClick:q,sx:{color:"error.main"},children:[a(I,{children:a(ge,{fontSize:"small",color:"error"})}),a(T,{children:"Delete BOM"})]})]}),k&&a(be,{color:"primary","aria-label":"scroll to top",sx:{position:"fixed",bottom:16,right:16,background:"linear-gradient(135deg, #667eea 0%, #764ba2 100%)"},onClick:()=>window.scrollTo({top:0,behavior:"smooth"}),children:a(fe,{})})]})}export{Ae as default};
