257 lines
10 KiB
TypeScript
257 lines
10 KiB
TypeScript
'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<string>('sales');
|
|
const [downloadingFormat, setDownloadingFormat] = useState<string | null>(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 (
|
|
<div className="space-y-6 pb-12">
|
|
{/* Top Header */}
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 border-b border-border/60 pb-5">
|
|
<div>
|
|
<div className="flex items-center gap-2 text-xs font-semibold uppercase tracking-wider text-primary mb-1">
|
|
<FileSpreadsheet className="w-4 h-4 text-primary" />
|
|
<span>Enterprise Intelligence & Audit Reports</span>
|
|
</div>
|
|
<h1 className="text-2xl sm:text-3xl font-extrabold tracking-tight text-foreground">
|
|
Reporting & Analytics Hub
|
|
</h1>
|
|
<p className="text-sm text-muted-foreground mt-1">
|
|
Export comprehensive multi-dimensional business reports in Excel (.xlsx), CSV, and PDF formats.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Category Selection Cards */}
|
|
<div>
|
|
<label className="text-xs font-semibold text-muted-foreground uppercase tracking-wider block mb-3">
|
|
Select Business Domain
|
|
</label>
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-3">
|
|
{REPORT_CATEGORIES.map((cat) => {
|
|
const Icon = cat.icon;
|
|
const isSelected = selectedCategory === cat.id;
|
|
return (
|
|
<button
|
|
key={cat.id}
|
|
type="button"
|
|
onClick={() => setSelectedCategory(cat.id)}
|
|
className={`p-4 rounded-2xl border text-left transition relative flex flex-col justify-between ${
|
|
isSelected
|
|
? 'border-primary bg-primary/10 shadow-sm ring-1 ring-primary'
|
|
: 'border-border/80 bg-card hover:bg-muted/50'
|
|
}`}
|
|
>
|
|
<div>
|
|
<div className="flex items-center justify-between mb-3">
|
|
<div className={`p-2 rounded-xl ${
|
|
isSelected ? 'bg-primary text-primary-foreground' : 'bg-muted text-muted-foreground'
|
|
}`}>
|
|
<Icon className="w-4 h-4" />
|
|
</div>
|
|
<span className="text-[10px] font-bold uppercase tracking-wider px-2 py-0.5 rounded-full bg-muted/80 text-muted-foreground">
|
|
{cat.badge}
|
|
</span>
|
|
</div>
|
|
<h3 className="text-sm font-bold text-foreground">
|
|
{cat.name}
|
|
</h3>
|
|
</div>
|
|
<div className="mt-3 text-[11px] text-muted-foreground line-clamp-2">
|
|
{cat.description}
|
|
</div>
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Active Report Config & 1-Click Export Actions */}
|
|
<div className="rounded-2xl border border-border/80 bg-card p-6 shadow-sm">
|
|
<div className="flex flex-col lg:flex-row lg:items-center justify-between gap-6 pb-6 border-b border-border/60">
|
|
<div className="space-y-1 max-w-xl">
|
|
<div className="flex items-center gap-2">
|
|
<span className="px-2.5 py-0.5 rounded-full bg-primary/15 text-primary text-[10px] font-bold uppercase">
|
|
Active Selection
|
|
</span>
|
|
<h2 className="text-xl font-extrabold text-foreground">
|
|
{activeCategory.name}
|
|
</h2>
|
|
</div>
|
|
<p className="text-xs text-muted-foreground leading-relaxed">
|
|
{activeCategory.description}
|
|
</p>
|
|
</div>
|
|
|
|
{/* 3 Download Buttons */}
|
|
<div className="flex flex-wrap items-center gap-3">
|
|
{/* Excel Button */}
|
|
<button
|
|
onClick={() => handleDownload('excel')}
|
|
disabled={!!downloadingFormat}
|
|
className="inline-flex items-center gap-2 px-4 py-2.5 rounded-xl bg-emerald-600 hover:bg-emerald-700 text-white font-semibold text-xs shadow transition disabled:opacity-50"
|
|
>
|
|
<FileSpreadsheet className="w-4 h-4" />
|
|
<span>{downloadingFormat === 'excel' ? 'Exporting...' : 'Export Excel (.xlsx)'}</span>
|
|
</button>
|
|
|
|
{/* CSV Button */}
|
|
<button
|
|
onClick={() => handleDownload('csv')}
|
|
disabled={!!downloadingFormat}
|
|
className="inline-flex items-center gap-2 px-4 py-2.5 rounded-xl bg-sky-600 hover:bg-sky-700 text-white font-semibold text-xs shadow transition disabled:opacity-50"
|
|
>
|
|
<FileCode className="w-4 h-4" />
|
|
<span>{downloadingFormat === 'csv' ? 'Exporting...' : 'Export CSV (.csv)'}</span>
|
|
</button>
|
|
|
|
{/* PDF Button */}
|
|
<button
|
|
onClick={() => handleDownload('pdf')}
|
|
disabled={!!downloadingFormat}
|
|
className="inline-flex items-center gap-2 px-4 py-2.5 rounded-xl bg-rose-600 hover:bg-rose-700 text-white font-semibold text-xs shadow transition disabled:opacity-50"
|
|
>
|
|
<FileText className="w-4 h-4" />
|
|
<span>{downloadingFormat === 'pdf' ? 'Exporting...' : 'Export PDF Document'}</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Schema Column Blueprint */}
|
|
<div className="pt-6">
|
|
<div className="flex items-center justify-between mb-3">
|
|
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground">
|
|
Included Dataset Schema & Attributes
|
|
</h3>
|
|
<span className="text-[11px] font-semibold text-primary">
|
|
{activeCategory.columns.length} columns included
|
|
</span>
|
|
</div>
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
{activeCategory.columns.map((col, idx) => (
|
|
<span
|
|
key={idx}
|
|
className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-xl bg-muted/60 border border-border/60 text-xs font-mono font-medium text-foreground"
|
|
>
|
|
<CheckCircle2 className="w-3.5 h-3.5 text-emerald-500" />
|
|
<span>{col}</span>
|
|
</span>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|