'use client' import { useState, useEffect } from 'react' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Download, Search, BarChart3 } from 'lucide-react' export default function ReportsPage() { const [clients, setClients] = useState([]) const [logs, setLogs] = useState([]) const [stats, setStats] = useState(null) const [loading, setLoading] = useState(false) const [filters, setFilters] = useState({ clientId: '', from: '', to: '' }) useEffect(() => { fetch('/api/clients').then(r => r.json()).then(setClients) loadReports() }, []) async function loadReports() { setLoading(true) const params = new URLSearchParams() if (filters.clientId) params.append('clientId', filters.clientId) if (filters.from) params.append('from', filters.from) if (filters.to) params.append('to', filters.to) const res = await fetch(`/api/reports?${params}`) const data = await res.json() setStats(data) setLogs(data.logs || []) setLoading(false) } async function handleExportCsv() { const params = new URLSearchParams({ format: 'csv' }) if (filters.clientId) params.append('clientId', filters.clientId) if (filters.from) params.append('from', filters.from) if (filters.to) params.append('to', filters.to) const res = await fetch(`/api/reports?${params}`) const blob = await res.blob() const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `sms-report-${Date.now()}.csv` a.click() URL.revokeObjectURL(url) } return (

Reports

SMS delivery statistics and logs

{/* Filters */} Filters
setFilters(f => ({ ...f, from: e.target.value }))} />
setFilters(f => ({ ...f, to: e.target.value }))} />
{/* Stats */} {stats && (
{stats.total}

Total SMS

{stats.success}

Delivered

{stats.failed}

Failed

)} {/* Logs table */} SMS Logs {loading ? (

Loading...

) : logs.length === 0 ? (

No records found

) : (
{logs.map((log: any) => ( ))}
Date Client Student Phone Event Status Error
{new Date(log.sentAt).toLocaleString('en-PH', { timeZone: 'Asia/Manila' })} {log.client.name}
{log.queue.studentName}
{log.queue.studentId}
{log.phone} {log.queue.event === 'time_in' ? '✅ In' : '🔴 Out'} {log.status} {log.errorReason || '-'}
)}
) }