'use client'; import { useState, useEffect } from 'react'; import { Search, Download, Eye, CheckCircle, AlertCircle, XCircle, DollarSign, Filter } from 'lucide-react'; interface Payment { id: string; tenant: string; plan: string; amount: number; status: string; method: string; date: string; invoiceId: string; } const statusMap: Record = { paid: { label: 'Pago', cls: 'badge-success', icon: CheckCircle }, pending: { label: 'Pendente', cls: 'badge-warning', icon: AlertCircle }, failed: { label: 'Falhou', cls: 'badge-danger', icon: XCircle }, refunded: { label: 'Estornado', cls: 'badge-info', icon: DollarSign }, }; export default function Payments() { const [payments, setPayments] = useState([]); const [search, setSearch] = useState(''); const [filterStatus, setFilterStatus] = useState('all'); const [filterMonth, setFilterMonth] = useState('all'); const [loading, setLoading] = useState(true); useEffect(() => { fetch('/api/payments') .then(res => res.json()) .then(data => { setPayments(Array.isArray(data) ? data : []); }) .catch(err => console.error(err)) .finally(() => setLoading(false)); }, []); const filtered = payments.filter(p => { const matchSearch = p.tenant.toLowerCase().includes(search.toLowerCase()) || p.invoiceId.toLowerCase().includes(search.toLowerCase()); const matchStatus = filterStatus === 'all' || p.status === filterStatus; const matchMonth = filterMonth === 'all' || p.date.startsWith(filterMonth); return matchSearch && matchStatus && matchMonth; }); const totalPaid = filtered.filter(p => p.status === 'paid').reduce((s, p) => s + p.amount, 0); const totalPending = filtered.filter(p => p.status === 'pending').reduce((s, p) => s + p.amount, 0); const totalFailed = filtered.filter(p => p.status === 'failed').reduce((s, p) => s + p.amount, 0); // Helper to get unique months for the filter const months = Array.from(new Set(payments.map(p => p.date.substring(0, 7)))).sort().reverse(); const handleExportCSV = () => { if (filtered.length === 0) return; const headers = ['Fatura', 'Estabelecimento', 'Plano', 'Valor', 'Status', 'Metodo', 'Data']; const rows = filtered.map(p => [ p.invoiceId, p.tenant, p.plan, p.amount.toFixed(2), statusMap[p.status]?.label || p.status, p.method, new Date(p.date + 'T12:00:00').toLocaleDateString('pt-BR') ]); const csvContent = [headers.join(','), ...rows.map(e => e.map(val => `"${val}"`).join(','))].join('\n'); const blob = new Blob([new Uint8Array([0xEF, 0xBB, 0xBF]), csvContent], { type: 'text/csv;charset=utf-8;' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.setAttribute('href', url); link.setAttribute('download', `pagamentos_${new Date().toISOString().slice(0,10)}.csv`); document.body.appendChild(link); link.click(); document.body.removeChild(link); }; if (loading) return
Carregando pagamentos...
; return (
R$ {totalPaid.toFixed(2)}
Total Recebido
R$ {totalPending.toFixed(2)}
Pendente
R$ {totalFailed.toFixed(2)}
Falhas
setSearch(e.target.value)} />
{filtered.map(p => { const st = statusMap[p.status] || statusMap.pending; return ( ); })} {filtered.length === 0 && }
FaturaEstabelecimentoPlanoValorStatusMétodoData
{p.invoiceId} {p.tenant} {p.plan} R$ {p.amount.toFixed(2)} {st.label} {p.method} {new Date(p.date + 'T12:00:00').toLocaleDateString('pt-BR')}
Nenhum pagamento encontrado
); }