import{r as m,j as t,C as O,i as h}from"./index-D0o5GN6H.js";import{u as U}from"./useDataTable-Kl0VByqj.js";import{D as k}from"./index.es-DEUihVAS.js";import{f as s}from"./format-CAPgRCzf.js";import"./floating-ui.dom-BFD2mO2A.js";const z=()=>{const[p,x]=m.useState({}),[r,f]=m.useState([new Date,new Date]),{data:y,totalItems:g,currentPage:D,totalPages:w,itemsPerPage:C,isLoading:M,error:j,totalAmount:S,handlePageChange:N,handlePageSizeChange:P,handleFilterChange:i,handleSearchChange:F,searchTerm:d,filters:R}=U("/transactions/report",{initialItemsPerPage:25,initialSort:{field:"created_at",direction:"DESC"},initialFilters:{startDate:s(new Date,"yyyy-MM-dd"),endDate:s(new Date,"yyyy-MM-dd")}});m.useEffect(()=>{(async()=>{try{const a=await h.get("/transactions/report/filters");x(a.data)}catch(a){console.error("Error fetching filter options:",a)}})()},[]);const v=e=>{const[a,o]=e;f(e);const n={};a&&(n.startDate=s(a,"yyyy-MM-dd")),o&&(n.endDate=s(o,"yyyy-MM-dd")),i(n)},E=e=>{const a={...e};r[0]&&(a.startDate=s(r[0],"yyyy-MM-dd")),r[1]&&(a.endDate=s(r[1],"yyyy-MM-dd")),i(a)},L=async()=>{try{const e=new URLSearchParams;r[0]&&e.append("startDate",s(r[0],"yyyy-MM-dd")),r[1]&&e.append("endDate",s(r[1],"yyyy-MM-dd")),d&&e.append("search",d),Object.entries(R).forEach(([l,c])=>{c&&c!==""&&l!=="startDate"&&l!=="endDate"&&l!=="search"&&e.append(l,c)});const a=await h.get(`/transactions/report/csv?${e.toString()}`,{responseType:"blob"}),o=window.URL.createObjectURL(new Blob([a.data])),n=document.createElement("a");n.href=o;const u=a.headers["content-disposition"];let b="transaction_report.csv";if(u){const l=u.match(/filename="(.+)"/);l&&(b=l[1])}n.setAttribute("download",b),document.body.appendChild(n),n.click(),n.remove(),window.URL.revokeObjectURL(o)}catch(e){console.error("Error downloading CSV:",e),alert("Error downloading CSV file. Please try again.")}},T=[{field:"mailboxNumber",label:"Mailbox Number",sortable:!0},{field:"user",label:"User",sortable:!0},{field:"packageNumber",label:"Package Number",sortable:!0},{field:"status",label:"Status",sortable:!0,render:e=>t.jsx("span",{className:"px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800",children:e.status})},{field:"method",label:"Payment Method",sortable:!0,render:e=>(e.method?e.method.split(", "):[]).map(o=>t.jsx("span",{className:"inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800 mr-1",children:o},o))},{field:"processor",label:"Processed By",sortable:!0},{field:"date",label:"Date",sortable:!0,render:e=>s(new Date(e.date),"MMM d, yyyy")},{field:"total",label:"Total (JMD)",sortable:!0,render:e=>`J$ ${Number(e.total).toLocaleString("en-US",{minimumFractionDigits:2,maximumFractionDigits:2})}`},{field:"courier",label:"Courier",sortable:!0},{field:"branch",label:"Branch",sortable:!0}];return t.jsxs("div",{className:"container mx-auto px-4 sm:px-6 lg:px-8 py-8",children:[t.jsxs("div",{className:"mb-6",children:[t.jsx("h1",{className:"text-2xl font-semibold text-gray-900",children:"Payments Report"}),t.jsxs("p",{className:"text-xl font-bold mt-2 text-green-600",children:["TOTAL: J$ ",S.toLocaleString("en-US",{minimumFractionDigits:2,maximumFractionDigits:2})]}),t.jsxs("p",{className:"text-sm text-gray-600 mt-1",children:["Showing only completed payments • ",g," total transactions"]})]}),t.jsx("div",{className:"mb-4 p-4 bg-gray-50 rounded-md",children:t.jsxs("div",{className:"flex flex-col md:flex-row gap-4 items-center",children:[t.jsxs("div",{className:"flex-1",children:[t.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Filter by Date Range"}),t.jsx(k,{selectsRange:!0,startDate:r[0],endDate:r[1],onChange:v,className:"block w-full px-3 py-2 border border-gray-300 rounded-md leading-5 bg-white placeholder-gray-500 focus:outline-none focus:placeholder-gray-400 focus:ring-1 focus:ring-[#ff6b00] focus:border-[#61CE70] sm:text-sm",placeholderText:"Select date range"})]}),t.jsxs("div",{className:"flex gap-2",children:[t.jsx("button",{onClick:()=>{f([null,null]),i({})},className:"inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[#ff6b00]",children:"Clear Date Filter"}),t.jsx("button",{onClick:L,className:"inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500",children:"📊 Download CSV"})]})]})}),t.jsx(O,{headers:T,data:y,pagination:{currentPage:D,totalPages:w,totalItems:g,itemsPerPage:C,onPageChange:N,onPageSizeChange:P},onFilterChange:E,onSearchChange:F,searchTerm:d,isLoading:M,error:j,filterOptions:p})]})};export{z as default};
