import{s as B,N as O,r as s,j as t,C as V,i as G}from"./index-RapqkS1n.js";import{P as H}from"./ProtectedRoute-BvSTU-TT.js";import{f as K}from"./format-CAPgRCzf.js";import"./AccessDenied-DahwhRFy.js";import"./ArrowLeftIcon-B51iGo7A.js";const ee=()=>{var g,x;const{user:a}=B();O();const o=((g=a==null?void 0:a.courier)==null?void 0:g.id)||null;console.log("Auth State:",{user:a,courierId:o,hasUser:!!a,hasCourier:!!(a!=null&&a.courier),courierIdValue:(x=a==null?void 0:a.courier)==null?void 0:x.id});const[h,p]=s.useState([]),[N,S]=s.useState(0),[y,P]=s.useState(1),[l,n]=s.useState(1),[c,j]=s.useState(10),[D,v]=s.useState(0),[C,m]=s.useState(!1),[u,i]=s.useState(null),[I,A]=s.useState(""),[F,U]=s.useState("");s.useEffect(()=>{console.log("useEffect triggered with courierId:",o),console.log("Current user:",a),(async()=>{var b,f;m(!0),i(null);try{console.log("Making API request with params:",{courier_id:o,page:l,size:c});const r=await G.get("/uncollected-packages",{params:{...o&&{courier_id:o},page:l,size:c}});if(console.log("API Response:",r.data),!r.data||!Array.isArray(r.data.packages)){console.error("Invalid response format:",r.data),i("Invalid response format from server");return}const{totalItems:z,totalPages:M,currentPage:L,pageSize:$,packages:d}=r.data;console.log("Fetched packages:",d),p(d),S(z),P(M),n(L),j($);const J=d.reduce((R,q)=>R+(q.package_value||0),0);v(J)}catch(r){console.error("Error fetching Uncollected:",r),i(((f=(b=r.response)==null?void 0:b.data)==null?void 0:f.error)||"Failed to load Uncollected. Please try again.")}finally{m(!1)}})()},[o,l,c]);const k=[{field:"mailboxNumber",label:"Mailbox Number",sortable:!0},{field:"userDisplayName",label:"User",sortable:!0},{field:"packageNumber",label:"Package Number",sortable:!0},{field:"status",label:"Status",sortable:!0},{field:"createDate",label:"Create Date",sortable:!0},{field:"totalFormatted",label:"Total (JMD)",sortable:!0},{field:"branchName",label:"Branch",sortable:!0}],T=e=>t.jsx("span",{className:"px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800",children:e}),w=h.map(e=>({id:e.id,mailboxNumber:e.mailboxNumber||"—",userDisplayName:e.customer?`${e.customer.firstName} ${e.customer.lastName}`:"—",packageNumber:e.tracking_number,status:T(e.status),createDate:e.created_at?K(new Date(e.created_at),"MMM d, yyyy"):"—",totalFormatted:`J$ ${(e.package_value||0).toLocaleString("en-US",{minimumFractionDigits:2,maximumFractionDigits:2})}`,branchName:e.branch?e.branch.name:"—"})),E=e=>{n(e)},_=()=>{n(1)};return t.jsx(H,{permission:"packages_uncollected",children:t.jsxs("div",{className:"container mx-auto px-4 sm:px-6 lg:px-8 py-8",children:[t.jsxs("div",{className:"sm:flex sm:items-center justify-between mb-4",children:[t.jsxs("div",{children:[t.jsx("h1",{className:"text-2xl font-semibold text-gray-900",children:"Uncollected Packages"}),t.jsxs("p",{className:"text-xl font-bold mt-2",children:["TOTAL: J$"," ",D.toLocaleString("en-US",{minimumFractionDigits:2,maximumFractionDigits:2})]})]}),t.jsxs("div",{className:"flex gap-4 items-end",children:[t.jsxs("div",{children:[t.jsx("label",{className:"block text-sm font-medium text-gray-700",children:"Start Date"}),t.jsx("input",{type:"date",value:I,onChange:e=>A(e.target.value),className:"border rounded px-2 py-1"})]}),t.jsxs("div",{children:[t.jsx("label",{className:"block text-sm font-medium text-gray-700",children:"End Date"}),t.jsx("input",{type:"date",value:F,onChange:e=>U(e.target.value),className:"border rounded px-2 py-1"})]}),t.jsx("button",{onClick:_,className:"px-4 py-2 bg-black text-white text-sm rounded hover:bg-gray-800",children:"Submit"})]})]}),u&&t.jsx("div",{className:"mb-4 text-red-600",children:u}),t.jsx(V,{headers:k,data:w,pagination:{currentPage:l,totalPages:y,totalItems:N,onPageChange:E},onSort:()=>{},onFilter:()=>{},isLoading:C})]})})};export{ee as default};
