'use client'; import React, { useState } from 'react'; import { FileSpreadsheet, FileText, FileCode, Download, Calendar, TrendingUp, Package, Wrench, Truck, Users, CheckCircle2, Sparkles, ArrowDownToLine } from 'lucide-react'; import { API_BASE_URL } from '@/services/api/config'; import { toast } from 'sonner'; interface ReportCategoryOption { id: string; name: string; description: string; icon: React.ComponentType<{ className?: string }>; color: string; badge: string; columns: string[]; } const REPORT_CATEGORIES: ReportCategoryOption[] = [ { id: 'sales', name: 'Sales & Revenue Ledger', description: 'Comprehensive order breakdown, itemized line items, GST subtotals, and payment status.', icon: TrendingUp, color: 'emerald', badge: 'Finance & Orders', columns: ['Order ID', 'Customer Name', 'Phone', 'Items Count', 'Total', 'Discount', 'Shipping', 'Final Amount', 'Order Status', 'Payment Status'] }, { id: 'inventory', name: 'Inventory & Stock Valuation', description: 'Master warehouse stock levels, variant SKUs, cost prices, selling prices, and total inventory asset valuation.', icon: Package, color: 'blue', badge: 'Warehouse & SKUs', columns: ['Product ID', 'Product Name', 'SKU', 'Category', 'Brand', 'Stock Qty', 'Cost Price', 'Selling Price', 'Valuation (INR)'] }, { id: 'repairs', name: 'Service & Repair Operations', description: '5-stage repair workshop intake, technician assignment, diagnostic issues, device serials, and repair estimates.', icon: Wrench, color: 'purple', badge: 'Workshop & Techs', columns: ['Ticket No', 'Customer Name', 'Phone', 'Device Model', 'Assigned Tech', 'Stage', 'Type', 'Estimated (INR)', 'Date'] }, { id: 'logistics', name: 'Logistics & Shipping Deliveries', description: 'Courier assignment, AWB tracking records, destination pin-codes, shipping charges, and fulfillment timeline.', icon: Truck, color: 'amber', badge: 'Shipments & Couriers', columns: ['Order ID', 'Customer Name', 'City', 'State', 'Pincode', 'Fulfillment', 'Payment Status', 'Shipping Cost', 'Created At'] }, { id: 'employee_activity', name: 'Employee Operational Activity', description: 'Operational work distribution: Inquiries contacted, qualified CRM deals managed, and technician tasks resolved. (No targets or commission calculations).', icon: Users, color: 'indigo', badge: 'Workforce & CRM', columns: ['User ID', 'Staff Name', 'Email', 'Role', 'Leads Handled', 'Deals Managed', 'Tasks Completed', 'Status'] } ]; 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 (
{/* Top Header */}
Enterprise Intelligence & Audit Reports

Reporting & Analytics Hub

Export comprehensive multi-dimensional business reports in Excel (.xlsx), CSV, and PDF formats.

{/* Category Selection Cards */}
{REPORT_CATEGORIES.map((cat) => { const Icon = cat.icon; const isSelected = selectedCategory === cat.id; return ( ); })}
{/* Active Report Config & 1-Click Export Actions */}
Active Selection

{activeCategory.name}

{activeCategory.description}

{/* 3 Download Buttons */}
{/* Excel Button */} {/* CSV Button */} {/* PDF Button */}
{/* Schema Column Blueprint */}

Included Dataset Schema & Attributes

{activeCategory.columns.length} columns included
{activeCategory.columns.map((col, idx) => ( {col} ))}
); }