import{u as $,O as H,P as J,a as X,M as Z,r as n,N as ee,j as o,b as a,bg as ae,B as l,bh as te,o as A,p as se,bi as ne,bj as E,q as N,bk as oe,a1 as ie,bl as re,R as I,bm as k,bn as R,a6 as le,ap as ce,c,I as de}from"./index-ktJf6c6X.js";import{u as he,a as pe}from"./BomApi-BWihLC2M.js";import{l as ue,a as me,s as be,b as ge}from"./bomStorage-C187aNfO.js";import{E as fe}from"./Edit-DWIZDP8n.js";import{K as Me}from"./KeyboardArrowUp-GxOXrnkx.js";import{F as ye}from"./Fab-FCk4degS.js";function Ae(){const i=$(),T=H(),v=J(T.breakpoints.down("md")),[O,{isLoading:g}]=he(),[f]=pe(),{openSnackbar:P}=X(),{create:M,write:z}=Z("Master"),[y,d]=n.useState([]),[_,u]=n.useState(0),[m,L]=n.useState(ue),x=ee(m,500),[h,j]=n.useState(null),[S,V]=n.useState(null),[p,W]=n.useState(me),F=async()=>{try{const e=await O({page:p.page,size:p.pageSize,searchByName:{searchValue:x}}).unwrap();e&&e.data?(d(e.data),u(e.totalCount||e.data.length)):(d([]),u(0))}catch{d([]),u(0)}};n.useEffect(()=>{F()},[p,x]);const q=e=>{W(e),ge(e)},C=e=>{L(e),be(e)},G=e=>{d(t=>t.filter(s=>s.id!==e)),P("BOM deleted successfully!","success")},K=async e=>{try{const s=await f({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}})}},D=async e=>{try{const t=await f({id:e.id}).unwrap(),s=(t==null?void 0:t.bomData)||(t==null?void 0:t.data)||t,b=s&&s.bomComponentMetadataDetailsList||[],Y={...s||{},bomComponentMetadataDetailsList:b,materials:Array.isArray(b)?b.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}})}},w=()=>{i(c.MASTER_CREATE,{state:{initialData:null}})},B=()=>{V(null),j(null)},Q=()=>{h&&G(h.id),B()},U=[{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:z&&a(de,{type:"Edit",onClick:t=>{t.stopPropagation(),D(e.row)}})})}];return o(l,{p:2,sx:{width:"100%"},children:[o(ae,{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(te,{placeholder:"Search BOM Name...",variant:"outlined",size:"small",value:m,sx:{width:225},onChange:e=>C(e.target.value),InputProps:{startAdornment:a(A,{edge:"start",size:"small",children:a(ne,{style:{color:"#bdbdbd"}})}),endAdornment:!!m&&a(A,{edge:"end",onClick:()=>C(""),size:"small",children:a(se,{})})}})}),M&&a(E,{btnType:"create",onClick:w,children:"Add BOM"})]}),a(ie,{sx:{p:0},children:y.length===0&&!g?o(l,{sx:{mt:3,p:4,border:"1px dashed #ccc",borderRadius:2,textAlign:"center"},children:[a(N,{variant:"h6",gutterBottom:!0,children:"No Data Available"}),a(N,{variant:"body2",color:"text.secondary",mb:3,children:"You haven't added any BOMs yet. Click below to add your first BOM."}),M&&a(E,{btnType:"create",onClick:w,children:"Add BOM"})]}):a(oe,{columns:U,rows:y,checkboxSelection:!1,rowCount:_,paginationModel:p,pageSizeOptions:[10,25,50,100],handlePaginationChange:q,loading:g,enableManageColumns:!0,onRowClick:e=>K(e.row)})})]}),o(re,{anchorEl:S,open:!!S,onClose:B,PaperProps:{elevation:3,sx:{borderRadius:2,minWidth:200}},children:[o(I,{onClick:()=>h&&D(h),children:[a(k,{children:a(fe,{fontSize:"small"})}),a(R,{children:"Edit BOM"})]}),a(le,{}),o(I,{onClick:Q,sx:{color:"error.main"},children:[a(k,{children:a(ce,{fontSize:"small",color:"error"})}),a(R,{children:"Delete BOM"})]})]}),v&&a(ye,{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(Me,{})})]})}export{Ae as default};
