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,bf as ae,B as l,bg as te,o as A,p as se,bh as ne,bi as E,q as N,bj as oe,a1 as ie,b2 as re,R as I,bk as k,bl as R,a2 as le,al as ce,c,I as de}from"./index-DlNZgIG9.js";import{u as he,a as ue}from"./BomApi-EjUHUCdx.js";import{l as pe,a as me,s as be,b as ge}from"./bomStorage-C187aNfO.js";import{E as fe}from"./Edit-BDl58YiG.js";import{K as Me}from"./KeyboardArrowUp-oxZwsPmP.js";import{F as ye}from"./Fab-hcyyge7s.js";function Ae(){const i=$(),T=H(),v=J(T.breakpoints.down("md")),[O,{isLoading:g}]=he(),[f]=ue(),{openSnackbar:P}=X(),{create:M,write:z}=Z("Master"),[y,d]=n.useState([]),[_,p]=n.useState(0),[m,L]=n.useState(pe),x=ee(m,500),[h,j]=n.useState(null),[S,V]=n.useState(null),[u,W]=n.useState(me),F=async()=>{try{const e=await O({page:u.page,size:u.pageSize,searchByName:{searchValue:x}}).unwrap();e&&e.data?(d(e.data),p(e.totalCount||e.data.length)):(d([]),p(0))}catch{d([]),p(0)}};n.useEffect(()=>{F()},[u,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:u,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};
