import{r as n,au as T,b as a,j as u,V as j,ax as B,bI as I,ay as L,R as p,B as D,a0 as R,U as M,X as N,L as O,G as E,aj as G,q as k,e as U,be as q}from"./index-ktJf6c6X.js";import{d as F}from"./Inventory2-BgmaL0vE.js";import{r as W}from"./renderStatusBox-CoIp7mNd.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:[]:[]})(),x=t=>{if(i(t),t==="all")S(null),o(null);else{const m=Number(t);S(N().subtract(m-1,"day")),o(N())}};return n.useEffect(()=>{l&&l({branchId:s==="all"?void 0:Number(s),fromDate:f,toDate:y})},[s,f,y,l]),a(M,{dateAdapter:j,children:u(R,{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(I,{id:"branch-select-label",children:"Branch"}),u(L,{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(D,{display:"flex",alignItems:"center",gap:2,children:u(B,{size:"small",sx:{minWidth:150},children:[a(I,{children:"Date Range"}),u(L,{label:"Date Range",value:h,onChange:t=>x(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,x]=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),x(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){x(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(D,{sx:{p:2,width:"100%"},children:[a(H,{onFilterChange:z}),a(E,{container:!0,spacing:2,mt:3,children:m.map((e,r)=>a(E,{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(D,{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(D,{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};
