import{r as n,ar as T,b as a,j as u,V as R,au as B,bG as L,av as N,R as p,B as x,a0 as j,U as M,X as E,L as O,G as I,af as G,q as k,e as U,bd as q}from"./index-DlNZgIG9.js";import{d as F}from"./Inventory2-zqScgfVM.js";import{r as W}from"./renderStatusBox-DfDz4TZo.js";const H=({onFilterChange:l})=>{const[s,d]=n.useState("all"),[h,i]=n.useState("all"),[f,S]=n.useState(null),[y,o]=n.useState(null),{data:c,isLoading:b}=T({}),C=(()=>{var t,m;return c?(t=c.userProfileDTO)!=null&&t.branchDetailsDTO?c.userProfileDTO.branchDetailsDTO:(m=c.userProfileDTO)!=null&&m.branches?c.userProfileDTO.branches:c.branchDetails?c.branchDetails:[]:[]})(),D=t=>{if(i(t),t==="all")S(null),o(null);else{const m=Number(t);S(E().subtract(m-1,"day")),o(E())}};return n.useEffect(()=>{l&&l({branchId:s==="all"?void 0:Number(s),fromDate:f,toDate:y})},[s,f,y,l]),a(M,{dateAdapter:R,children:u(j,{elevation:0,sx:{p:2,mb:3,borderRadius:3,display:"flex",justifyContent:"space-between",alignItems:"center"},children:[u(B,{size:"small",sx:{minWidth:150},children:[a(L,{id:"branch-select-label",children:"Branch"}),u(N,{labelId:"branch-select-label",label:"Branch",value:s,onChange:t=>d(t.target.value),children:[a(p,{value:"all",children:"All Branches"}),!b&&C.map(t=>a(p,{value:String(t.id??t.branchId),children:t.name??t.branchName??"Unknown"},t.id??t.branchId))]})]}),a(x,{display:"flex",alignItems:"center",gap:2,children:u(B,{size:"small",sx:{minWidth:150},children:[a(L,{children:"Date Range"}),u(N,{label:"Date Range",value:h,onChange:t=>D(t.target.value),children:[a(p,{value:"all",children:"All Dates"}),a(p,{value:"7",children:"Last 7 Days"}),a(p,{value:"30",children:"Last 30 Days"}),a(p,{value:"90",children:"Last 90 Days"})]})]})})]})})},V=O.injectEndpoints({endpoints:l=>({searchPackingListByUserId:l.mutation({query:({page:s,size:d,searchValue:h,branchId:i})=>({url:"packing-list/search-by-criteria-userId",method:"POST",params:{page:s,size:d},body:{searchValue:h},headers:i!==void 0&&i!==""?{"Branch-Id":String(i)}:void 0})}),getPackingListByBranch:l.mutation({query:({branchId:s})=>({url:"packing-list/by-branch",method:"POST",body:{branchId:s},headers:{"Branch-Id":String(s)}})})}),overrideExisting:!1}),{useSearchPackingListByUserIdMutation:Q,useGetPackingListByBranchMutation:J}=V;function K({branchId:l}){const[s]=Q(),[d,h]=n.useState([]),[i,f]=n.useState(0),[S,y]=n.useState(!1),[o,c]=n.useState({page:0,pageSize:10}),[b,v]=n.useState(l),[C,D]=n.useState(null),m=[{label:"Total Records",value:n.useMemo(()=>i>0?i:d.length>0?d.length:0,[d,i]),icon:a(F,{}),description:"Count of all items in your list"}],A=[{field:"packingNumber",headerName:"Packing No",flex:1},{field:"clientName",headerName:"Client Name",flex:1},{field:"phoneNumber",headerName:"Phone Number",flex:1},{field:"experience",headerName:"Experience",flex:1},{field:"status",headerName:"Status",flex:1,renderCell:e=>W(e.value||"")},{field:"createdDate",headerName:"Date",flex:1,valueGetter:e=>{var r;return(r=e.row.createdDate)==null?void 0:r.slice(0,10)}}],P=n.useCallback(async()=>{y(!0),D(null);try{const e=await s({page:o.page,size:o.pageSize,searchValue:"",branchId:b||void 0}).unwrap();let r=[],g=0;e&&(e.data!==void 0?Array.isArray(e.data)?(r=e.data,g=e.totalCount||e.totalElements||r.length):e.data&&typeof e.data=="object"&&(r=e.data.data||e.data.content||e.data.items||[],g=e.data.totalCount||e.data.totalElements||e.totalCount||e.totalElements||r.length):Array.isArray(e)?(r=e,g=e.length):e.content?(r=Array.isArray(e.content)?e.content:[],g=e.totalCount||e.totalElements||r.length):(r=[],g=e.totalCount||e.totalElements||0)),h(r),f(g)}catch(e){D(e),console.error("Error fetching packing list:",e),h([]),f(0)}finally{y(!1)}},[s,o.page,o.pageSize,b]);n.useEffect(()=>{P()},[P]),n.useEffect(()=>{l!==b&&v(l)},[l]);const z=n.useCallback(e=>{v(e.branchId),c({page:0,pageSize:o.pageSize})},[o.pageSize]),w=n.useCallback(e=>{c(e)},[]);return a(q,{error:C,children:u(x,{sx:{p:2,width:"100%"},children:[a(H,{onFilterChange:z}),a(I,{container:!0,spacing:2,mt:3,children:m.map((e,r)=>a(I,{item:!0,xs:12,sm:6,md:3,children:u(G,{sx:{p:2,textAlign:"center",height:"100%",display:"flex",flexDirection:"column",justifyContent:"space-between",transition:"transform 0.2s, box-shadow 0.2s","&:hover":{transform:"translateY(-4px)",boxShadow:4}},children:[a(x,{sx:{color:"primary.main",mb:1},children:e.icon}),a(k,{variant:"body2",color:"text.secondary",sx:{mb:1},children:e.label}),a(k,{variant:"h5",fontWeight:600,sx:{mb:.5},children:typeof e.value=="number"?e.value.toLocaleString():e.value}),a(k,{variant:"caption",color:"text.secondary",sx:{mt:1,fontSize:"0.7rem"},children:e.description})]})},r))}),a(x,{mt:4,children:a(U,{columns:A,rows:d,checkboxSelection:!1,rowCount:i,paginationModel:o,handlePaginationChange:w,loading:S,enableManageColumns:!0,defaultHiddenColumns:["id"],getRowId:e=>e.id||e.packingNumber,initialState:{pagination:{paginationModel:{pageSize:10,page:0}}}})})]})})}export{K as default};
