'use client'; import { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { api } from '@/lib/api'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Skeleton } from '@/components/ui/skeleton'; import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, Cell } from 'recharts'; const peso = (v: number) => '₱' + (v ?? 0).toLocaleString('en-PH', { minimumFractionDigits: 2 }); export default function ReportsPage() { const [from, setFrom] = useState(() => { const d = new Date(); d.setDate(1); return d.toISOString().split('T')[0]; }); const [to, setTo] = useState(() => new Date().toISOString().split('T')[0]); const { data: collection, isLoading: collLoading } = useQuery({ queryKey: ['reports', 'collection', from, to], queryFn: async () => { const res = await api.get(`/api/v1/reports/collection?from=${from}&to=${to}`); return res.data as Array<{ collector: string; totalAmount: number; paymentCount: number }>; }, }); const { data: aging, isLoading: agingLoading } = useQuery({ queryKey: ['reports', 'aging'], queryFn: async () => { const res = await api.get('/api/v1/reports/aging'); return res.data as Array<{ bucket: string; invoiceCount: number; totalAmount: number }>; }, }); const { data: subscribers, isLoading: subLoading } = useQuery({ queryKey: ['reports', 'subscribers'], queryFn: async () => { const res = await api.get('/api/v1/reports/subscribers'); return res.data as Array<{ plan: string; count: number; revenue: number }>; }, }); const { data: revenue, isLoading: revLoading } = useQuery({ queryKey: ['reports', 'revenue'], queryFn: async () => { const res = await api.get('/api/v1/reports/revenue'); return res.data as Array<{ month: string; total: number }>; }, }); return (
Financial and operational analytics
No collections in this period
) : (| Collector | Payments | Total |
|---|---|---|
| {row.collector} | {row.paymentCount} | {peso(row.totalAmount)} |
| Total | {collection?.reduce((s, r) => s + r.paymentCount, 0)} | {peso(collection?.reduce((s, r) => s + r.totalAmount, 0) ?? 0)} |
No overdue invoices 🎉
)}| Plan | Subscribers | Monthly Revenue |
|---|---|---|
| {row.plan ?? row.name ?? '—'} | {row.count ?? row.subscribers ?? 0} | {peso(row.revenue ?? row.monthlyRevenue ?? 0)} |