'use client'; import { useState } from 'react'; import { FileSpreadsheet, FileText, FileCode, Loader2, } from 'lucide-react'; import { API_BASE_URL } from '@/services/api/config'; import { toast } from 'sonner'; import { CustomSelect } from '@/components/ui/CustomSelect'; interface ReportCategoryOption { id: string; name: string; columns: string[]; } const REPORT_CATEGORIES: ReportCategoryOption[] = [ { id: 'sales', name: 'Sales & Revenue Ledger', columns: ['Order ID', 'Customer Name', 'Phone', 'Items Count', 'Total', 'Discount', 'Shipping', 'Final Amount', 'Order Status', 'Payment Status'], }, { id: 'inventory', name: 'Inventory & Stock Valuation', columns: ['Product ID', 'Product Name', 'SKU', 'Category', 'Brand', 'Stock Qty', 'Cost Price', 'Selling Price', 'Valuation (INR)'], }, { id: 'repairs', name: 'Service & Repair Operations', columns: ['Ticket No', 'Customer Name', 'Phone', 'Device Model', 'Assigned Tech', 'Stage', 'Type', 'Estimated (INR)', 'Date'], }, { id: 'logistics', name: 'Logistics & Shipping Deliveries', columns: ['Order ID', 'Customer Name', 'City', 'State', 'Pincode', 'Fulfillment', 'Payment Status', 'Shipping Cost', 'Created At'], }, { id: 'employee_activity', name: 'Employee Operational Activity', columns: ['User ID', 'Staff Name', 'Email', 'Role', 'Leads Handled', 'Deals Managed', 'Tasks Completed', 'Status'], }, ]; const shell = 'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0'; const primaryButton = 'inline-flex items-center justify-center gap-1.5 h-9 px-3 crm-radius-control bg-primary hover:bg-primary/90 text-white text-[13px] font-semibold cursor-pointer disabled:opacity-50'; export default function ReportsHubPage() { const [selectedCategory, setSelectedCategory] = useState('sales'); const [downloadingFormat, setDownloadingFormat] = useState(null); const activeCategory = REPORT_CATEGORIES.find(c => c.id === selectedCategory) || REPORT_CATEGORIES[0]; const handleDownload = async (format: 'excel' | 'csv' | 'pdf') => { try { setDownloadingFormat(format); toast.info(`Preparing ${activeCategory.name} in ${format.toUpperCase()} format...`); const url = `${API_BASE_URL}/api/v1/reports/export?category=${selectedCategory}&format=${format}`; const res = await fetch(url); if (!res.ok) { throw new Error(`Server responded with status ${res.status}`); } const blob = await res.blob(); const ext = format === 'excel' ? 'xlsx' : format; const filename = `ifixkart_${selectedCategory}_report_${Date.now()}.${ext}`; const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); toast.success(`${activeCategory.name} downloaded successfully!`); } catch (err: unknown) { console.error('Download error:', err); toast.error(`Failed to export ${format.toUpperCase()} report. Please try again.`); } finally { setDownloadingFormat(null); } }; return (

Reports

{REPORT_CATEGORIES.length}
({ value: cat.id, label: cat.name }))} />
{activeCategory.columns.map((column) => ( ))}
{column}
Export downloads this report.
); }