import { useState, useEffect } from 'react' import DashboardLayout from '@/components/layout/DashboardLayout' import { Line, Bar, Doughnut } from 'react-chartjs-2' import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, BarElement, ArcElement, Title, Tooltip, Legend, } from 'chart.js' import { format, subMonths } from 'date-fns' import { GetServerSideProps } from 'next' import { getServerSession } from 'next-auth' import { authOptions } from '../api/auth/[...nextauth]' import { Session } from 'next-auth' ChartJS.register( CategoryScale, LinearScale, PointElement, LineElement, BarElement, ArcElement, Title, Tooltip, Legend ) type ReportData = { disbursementTrend: { labels: string[] datasets: { label: string data: number[] borderColor: string tension: number }[] } loanStatusDistribution: { labels: string[] datasets: { data: number[] backgroundColor: string[] }[] } repaymentPerformance: { labels: string[] datasets: { label: string data: number[] backgroundColor: string }[] } } export const getServerSideProps: GetServerSideProps = async (context) => { const session = await getServerSession(context.req, context.res, authOptions) if (!session) { return { redirect: { destination: '/auth/signin', permanent: false, }, } } return { props: { session, }, } } interface ReportsProps { session: Session } export default function Reports({ session }: ReportsProps) { const [reportData, setReportData] = useState({ disbursementTrend: { labels: [], datasets: [], }, loanStatusDistribution: { labels: [], datasets: [], }, repaymentPerformance: { labels: [], datasets: [], }, }) useEffect(() => { const fetchReportData = async () => { try { const res = await fetch('/api/reports') const data = await res.json() setReportData(data) } catch (error) { console.error('Error fetching report data:', error) } } fetchReportData() }, []) return (

Loan Reports

Comprehensive overview of loan performance and statistics

{/* Disbursement Trend */}

Loan Disbursement Trend

{/* Loan Status Distribution */}

Loan Status Distribution

{/* Repayment Performance */}

Repayment Performance

{/* Summary Statistics */}

Total Active Loans

245

↑ 12% vs last month

Total Disbursed Amount

KES 12.5M

↑ 8% vs last month

Average Loan Size

KES 51,020

↓ 3% vs last month

Default Rate

2.3%

↓ 0.5% vs last month

) }