'use client' import { useState } 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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Download, FileText, Users, CreditCard, Activity, BarChart3 } from 'lucide-react' import { toast } from '@/hooks/use-toast' const reportTypes = [ { id: 'users', name: 'Users Report', description: 'Export all user data including registration info, roles, and verification status', icon: Users, }, { id: 'transactions', name: 'Transactions Report', description: 'Export all transaction data including payments, refunds, and balance changes', icon: CreditCard, }, { id: 'billing', name: 'Billing Report', description: 'Export billing records including service purchases and payment status', icon: FileText, }, { id: 'developer-requests', name: 'Developer Requests Report', description: 'Export developer hire requests and their status', icon: Activity, }, { id: 'summary', name: 'Summary Report', description: 'Comprehensive overview with key metrics and statistics', icon: BarChart3, }, ] export default function ReportsManagement() { const [selectedReport, setSelectedReport] = useState('') const [format, setFormat] = useState('json') const [dateFrom, setDateFrom] = useState('') const [dateTo, setDateTo] = useState('') const [generating, setGenerating] = useState(false) const handleGenerateReport = async () => { if (!selectedReport) { toast({ title: 'Error', description: 'Please select a report type', variant: 'destructive', }) return } setGenerating(true) try { const token = localStorage.getItem('token') || document.cookie .split('; ') .find((row) => row.startsWith('token=')) ?.split('=')[1] const params = new URLSearchParams({ type: selectedReport, format, ...(dateFrom && { dateFrom }), ...(dateTo && { dateTo }), }) const response = await fetch(`/api/admin/reports?${params}`, { headers: { Authorization: `Bearer ${token}`, }, }) if (!response.ok) { throw new Error('Failed to generate report') } if (format === 'csv') { // Handle CSV download const blob = await response.blob() const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `${selectedReport}_report_${new Date().toISOString().split('T')[0]}.csv` document.body.appendChild(a) a.click() window.URL.revokeObjectURL(url) document.body.removeChild(a) } else { // Handle JSON download const data = await response.json() const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }) const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `${selectedReport}_report_${new Date().toISOString().split('T')[0]}.json` document.body.appendChild(a) a.click() window.URL.revokeObjectURL(url) document.body.removeChild(a) } toast({ title: 'Success', description: 'Report generated and downloaded successfully', }) } catch (error) { console.error('Report generation error:', error) toast({ title: 'Error', description: 'Failed to generate report', variant: 'destructive', }) } finally { setGenerating(false) } } const handleQuickExport = async (reportType: string) => { setSelectedReport(reportType) setFormat('csv') // Trigger immediate export setTimeout(() => { handleGenerateReport() }, 100) } return (
{report.description}
Exported reports contain sensitive user data. Ensure compliance with data protection regulations and handle all exported data securely.
Use date filters to limit report scope. Large date ranges may result in slower generation times and larger file sizes.