import React, { useState, useMemo } from 'react'; import { Receipt, Download, Mail, RefreshCw, Search, ArrowUpDown, ArrowDown, ArrowUp, X, Calendar, ChevronDown } from 'lucide-react'; import { Pagination } from '../common/Pagination'; interface PaymentsTabProps { payments: any[]; currentPage: number; perPage: number; downloadingId: string | null; sharingId: string | null; onPageChange: (p: number) => void; onPerPageChange: (pp: number) => void; onDownloadInvoice: (id: string) => void; onShareInvoice: (id: string) => void; } const MONTH_OPTIONS = [ { value: '01', label: 'January' }, { value: '02', label: 'February' }, { value: '03', label: 'March' }, { value: '04', label: 'April' }, { value: '05', label: 'May' }, { value: '06', label: 'June' }, { value: '07', label: 'July' }, { value: '08', label: 'August' }, { value: '09', label: 'September' }, { value: '10', label: 'October' }, { value: '11', label: 'November' }, { value: '12', label: 'December' }, ]; export const PaymentsTab: React.FC = ({ payments, currentPage, perPage, downloadingId, sharingId, onPageChange, onPerPageChange, onDownloadInvoice, onShareInvoice, }) => { const [searchQuery, setSearchQuery] = useState(''); const [sortOrder, setSortOrder] = useState<'desc' | 'asc'>('desc'); const [selectedYear, setSelectedYear] = useState('all'); const [selectedMonth, setSelectedMonth] = useState('all'); const [isCustomYear, setIsCustomYear] = useState(false); const [customYearInput, setCustomYearInput] = useState(''); // Dynamically extract all available years from payments (with fallback to current year) const availableYears = useMemo(() => { const currentYear = new Date().getFullYear(); const yearsSet = new Set([currentYear]); for (const p of payments) { if (!p?.created_at) continue; const d = typeof p.created_at === 'number' ? new Date(p.created_at * 1000) : new Date(p.created_at); if (!isNaN(d.getTime())) { yearsSet.add(d.getFullYear()); } } return Array.from(yearsSet).sort((a, b) => b - a); }, [payments]); // Filter and Sort payments const processedPayments = useMemo(() => { let result = [...payments]; // Filter by Year if (selectedYear !== 'all') { result = result.filter((p: any) => { if (!p?.created_at) return false; const d = typeof p.created_at === 'number' ? new Date(p.created_at * 1000) : new Date(p.created_at); if (isNaN(d.getTime())) return false; return d.getFullYear().toString() === selectedYear; }); } // Filter by Month if (selectedMonth !== 'all') { result = result.filter((p: any) => { if (!p?.created_at) return false; const d = typeof p.created_at === 'number' ? new Date(p.created_at * 1000) : new Date(p.created_at); if (isNaN(d.getTime())) return false; const m = String(d.getMonth() + 1).padStart(2, '0'); return m === selectedMonth; }); } // Filter by Search Query if (searchQuery.trim()) { const q = searchQuery.toLowerCase().trim(); result = result.filter((p: any) => { const id = (p.id || '').toLowerCase(); const email = (p.email || '').toLowerCase(); const orderId = (p.order_id || '').toLowerCase(); const contact = (p.contact || '').toLowerCase(); const method = (p.method || '').toLowerCase(); const invoiceNum = (p.invoice_number || '').toLowerCase(); const status = (p.status || '').toLowerCase(); return ( id.includes(q) || email.includes(q) || orderId.includes(q) || contact.includes(q) || method.includes(q) || invoiceNum.includes(q) || status.includes(q) ); }); } // Sort by created_at (default newest first) result.sort((a: any, b: any) => { const timeA = typeof a.created_at === 'number' ? a.created_at * 1000 : new Date(a.created_at || 0).getTime(); const timeB = typeof b.created_at === 'number' ? b.created_at * 1000 : new Date(b.created_at || 0).getTime(); return sortOrder === 'desc' ? timeB - timeA : timeA - timeB; }); return result; }, [payments, searchQuery, sortOrder, selectedYear, selectedMonth]); const hasDateFilter = selectedYear !== 'all' || selectedMonth !== 'all'; const handleClearDateFilters = () => { setSelectedYear('all'); setSelectedMonth('all'); setIsCustomYear(false); setCustomYearInput(''); onPageChange(1); }; const totalFiltered = processedPayments.length; const totalPages = Math.max(1, Math.ceil(totalFiltered / perPage)); const safePage = Math.min(currentPage, totalPages); const startIndex = (safePage - 1) * perPage; const paginatedPayments = processedPayments.slice(startIndex, startIndex + perPage); return (
{/* Search & Sort Controls Header */}
{/* Left Side: Search & Date Filters */}
{/* Search Input */}
{ setSearchQuery(e.target.value); onPageChange(1); }} className="w-full bg-white border border-slate-200 rounded-xl pl-9 pr-8 py-2 text-xs text-slate-800 font-semibold focus:outline-none focus:border-indigo-500 placeholder:text-slate-400 shadow-sm" /> {searchQuery && ( )}
{/* Date Filter Controls: Year & Month */}
{/* Year Selector (Dynamic with Custom Year support) */} {!isCustomYear ? (
) : (
{ const val = e.target.value.replace(/\D/g, '').slice(0, 4); setCustomYearInput(val); if (val.length === 4) { setSelectedYear(val); onPageChange(1); } else { setSelectedYear('all'); onPageChange(1); } }} className="w-34 bg-white border border-indigo-400 rounded-xl pl-8.5 pr-7 py-2 text-xs font-bold text-slate-800 placeholder:text-slate-400 focus:outline-none focus:ring-1 focus:ring-indigo-500 shadow-2xs transition-colors" title="Enter any custom year" />
)} {/* Month Selector */}
{hasDateFilter && ( )}
{/* Right Side: Sort & Count Controls */}
Showing {totalFiltered} of{' '} {payments.length}
{payments.length === 0 ? (

No payments captured on Razorpay gateway yet.

) : processedPayments.length === 0 ? (

No matching payments found

No payments match your {hasDateFilter && ( {' '}selected period ( {selectedMonth !== 'all' ? MONTH_OPTIONS.find((m) => m.value === selectedMonth)?.label : ''} {selectedMonth !== 'all' && selectedYear !== 'all' ? ' ' : ''} {selectedYear !== 'all' ? selectedYear : ''} ) )} {searchQuery ? ` and search query "${searchQuery}"` : ''}.

) : (
{paginatedPayments.map((p: any, i: number) => { const paymentDate = p.created_at ? typeof p.created_at === 'number' ? new Date(p.created_at * 1000).toLocaleDateString(undefined, { dateStyle: 'medium' }) : new Date(p.created_at).toLocaleDateString(undefined, { dateStyle: 'medium' }) : '—'; return ( ); })}
Payment ID Date Amount Status Method Email Order ID Invoice Actions
{p.id} {paymentDate} {p.currency === 'USD' ? '$' : p.currency === 'EUR' ? '€' : '₹'} {p.amount ? (p.amount / 100).toFixed(2) : '—'} {p.status} {p.method || '—'} {p.email || '—'} {p.order_id || '—'} {p.invoice_number ? ( {p.invoice_number} ) : ( Not generated )} {p.status === 'captured' && (
)}
)}
); };