ifixkart-admin/app/(admin)/dashboard/executive/page.tsx

312 lines
14 KiB
TypeScript

'use client';
import {
ArrowRight,
TrendingUp,
DollarSign,
Activity,
PackageCheck,
Users,
Boxes,
Building2,
Layers,
} from 'lucide-react';
import {
ComposedChart,
Bar,
Area,
XAxis,
YAxis,
Tooltip,
ResponsiveContainer,
CartesianGrid,
} from 'recharts';
import {
DashboardRevenuePoint,
DashboardMonthlyPoint,
} from '@/services/api/adminService';
import { cn } from '@/lib/utils';
import Link from 'next/link';
import { DashboardHeaderNav } from '@/components/dashboard/DashboardHeaderNav';
import { useDashboardData } from '@/components/dashboard/useDashboardData';
import {
CARD,
formatCompact,
formatInr,
samplePoints,
CardTitle,
PeriodPills,
DeltaPill,
} from '@/components/dashboard/DashboardShared';
export default function ExecutiveDashboardPage() {
const {
stats: displayStats,
loading,
refreshing,
chartMode,
setChartMode,
fetchData,
activity,
} = useDashboardData();
const byStatus = displayStats.orders_by_status ?? {};
const countOf = (key: string) => byStatus[key] ?? 0;
const totalOrders = displayStats.total_orders ?? 0;
const delivered = countOf('delivered');
const conversion = totalOrders > 0 ? (delivered / totalOrders) * 100 : 0;
const daily: DashboardRevenuePoint[] = displayStats.revenue_chart_daily ?? [];
const monthly: DashboardMonthlyPoint[] = displayStats.revenue_chart_monthly ?? [];
const sourceChart: Array<DashboardRevenuePoint | DashboardMonthlyPoint> =
chartMode === 'weekly' ? samplePoints(daily, 12) : monthly;
const rawPoints = sourceChart.map((row) => ({
name:
'date' in row
? row.date?.replace('08-', '') ?? ''
: row.label?.slice(0, 3) ?? '',
revenue: row.revenue,
orders: row.orders,
}));
const maxRev = Math.max(...rawPoints.map((row) => row.revenue), 1);
const maxOrd = Math.max(...rawPoints.map((row) => row.orders), 1);
const salesScale = (maxRev * 0.72) / maxOrd;
const chartPoints = rawPoints.map((row) => ({
...row,
sales: row.orders * salesScale,
}));
if (loading) {
return (
<div className="flex flex-col gap-4 animate-pulse">
<div className="h-8 w-48 bg-muted crm-radius-section" />
<div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">
{[...Array(4)].map((_, i) => (
<div key={i} className="h-28 bg-muted crm-radius-section" />
))}
</div>
</div>
);
}
return (
<div className="flex flex-col gap-4">
<DashboardHeaderNav
title="Executive C-Suite Dashboard"
refreshing={refreshing}
onRefresh={() => fetchData(true)}
/>
{/* KPI Cards Row */}
<div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">
<div className={cn(CARD, 'p-4')}>
<div className="flex items-start justify-between">
<div>
<p className="text-[13px] text-muted-foreground">Gross Revenue (All Time)</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
{formatInr(displayStats.revenue_all_time)}
</p>
</div>
<span className="w-10 h-10 rounded-full bg-primary text-white flex items-center justify-center shrink-0">
<DollarSign className="w-5 h-5" />
</span>
</div>
<div className="mt-2 flex items-center gap-2">
<DeltaPill value={activity.revenueDelta} />
<span className="text-[12px] text-muted-foreground">vs prior 7 days</span>
</div>
</div>
<div className={cn(CARD, 'p-4')}>
<div className="flex items-start justify-between">
<div>
<p className="text-[13px] text-muted-foreground">30-Day Enterprise Run-Rate</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
{formatInr(displayStats.revenue_last_30_days)}
</p>
</div>
<span className="w-10 h-10 rounded-full bg-emerald-600 text-white flex items-center justify-center shrink-0">
<TrendingUp className="w-5 h-5" />
</span>
</div>
<div className="mt-2 flex items-center gap-2">
<DeltaPill value={activity.revenue30Delta} />
<span className="text-[12px] text-muted-foreground">vs prior 30 days</span>
</div>
</div>
<div className={cn(CARD, 'p-4')}>
<div className="flex items-start justify-between">
<div>
<p className="text-[13px] text-muted-foreground">Average Order Value (AOV)</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
{formatInr(totalOrders > 0 ? displayStats.revenue_all_time / totalOrders : 0)}
</p>
</div>
<span className="w-10 h-10 rounded-full bg-blue-600 text-white flex items-center justify-center shrink-0">
<Activity className="w-5 h-5" />
</span>
</div>
<div className="mt-2 flex items-center gap-2">
<DeltaPill value={activity.aovDelta} />
<span className="text-[12px] text-muted-foreground">vs prior 7 days</span>
</div>
</div>
<div className={cn(CARD, 'p-4')}>
<div className="flex items-start justify-between">
<div>
<p className="text-[13px] text-muted-foreground">Gross Fulfillment Rate</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
{conversion.toFixed(1)}%
</p>
</div>
<span className="w-10 h-10 rounded-full bg-purple-600 text-white flex items-center justify-center shrink-0">
<PackageCheck className="w-5 h-5" />
</span>
</div>
<div className="mt-2 flex items-center gap-2">
<DeltaPill value={activity.fulfillmentDelta} />
<span className="text-[12px] text-muted-foreground">vs prior 7 days</span>
</div>
</div>
</div>
{/* Analytics & Portfolio Scope */}
<div className="grid grid-cols-1 lg:grid-cols-3 gap-4">
<div className={cn(CARD, 'lg:col-span-2 p-5 flex flex-col')}>
<div className="flex flex-wrap items-start justify-between gap-3">
<CardTitle>Executive Revenue Analytics &amp; Projections</CardTitle>
<PeriodPills value={chartMode} onChange={setChartMode} />
</div>
<div className="mt-4 flex flex-wrap items-end justify-between gap-3">
<div>
<p className="text-[26px] font-bold text-foreground tracking-tight leading-none">
{formatCompact(displayStats.revenue_last_30_days).replace('₹', '')}
</p>
<p className="text-[13px] text-muted-foreground mt-1">30-Day Consolidated Turnover (INR)</p>
</div>
</div>
<div className="h-[240px] w-full mt-4">
<ResponsiveContainer width="100%" height="100%">
<ComposedChart data={chartPoints} margin={{ top: 8, right: 8, left: 0, bottom: 0 }}>
<CartesianGrid vertical stroke="var(--border)" strokeDasharray="4 4" horizontal={false} />
<XAxis dataKey="name" tick={{ fontSize: 11, fill: 'var(--muted-foreground)' }} axisLine={false} tickLine={false} />
<YAxis tick={{ fontSize: 11, fill: 'var(--muted-foreground)' }} axisLine={false} tickLine={false} tickFormatter={(v: number) => (v >= 1000 ? `₹${(v / 1000).toFixed(0)}k` : `₹${v}`)} />
<Tooltip content={({ active, payload, label }) => {
if (!active || !payload?.length) return null;
const row = payload[0]?.payload as { revenue: number; orders: number };
return (
<div className="bg-card border border-border crm-radius-control px-3 py-2 shadow-sm text-xs">
<p className="text-muted-foreground mb-0.5">{label}</p>
<p className="font-semibold text-foreground">{formatInr(row.revenue)}</p>
<p className="text-muted-foreground">{row.orders} corporate transactions</p>
</div>
);
}} />
<Area type="monotone" dataKey="sales" fill="#EEF0F4" stroke="none" />
<Bar dataKey="revenue" fill="#3B82F6" radius={[2, 2, 0, 0]} />
</ComposedChart>
</ResponsiveContainer>
</div>
</div>
<div className={cn(CARD, 'p-5 flex flex-col')}>
<CardTitle>Enterprise Portfolio Scope</CardTitle>
<div className="mt-4 space-y-3.5 flex-1">
<div className="p-3 rounded-lg bg-card border border-border flex items-center justify-between">
<div className="flex items-center gap-3">
<span className="w-8 h-8 rounded-lg bg-blue-500/10 text-blue-500 flex items-center justify-center">
<Users className="w-4 h-4" />
</span>
<div>
<p className="text-xs text-muted-foreground">Total Customer Accounts</p>
<p className="text-sm font-bold text-foreground">{displayStats.total_users}</p>
</div>
</div>
<DeltaPill value={activity.customersDelta} />
</div>
<div className="p-3 rounded-lg bg-card border border-border flex items-center justify-between">
<div className="flex items-center gap-3">
<span className="w-8 h-8 rounded-lg bg-purple-500/10 text-purple-500 flex items-center justify-center">
<Boxes className="w-4 h-4" />
</span>
<div>
<p className="text-xs text-muted-foreground">Product Portfolio</p>
<p className="text-sm font-bold text-foreground">{displayStats.total_products} SKUs</p>
</div>
</div>
<span className="text-xs font-semibold text-muted-foreground">{displayStats.total_stock_units} Units</span>
</div>
<div className="p-3 rounded-lg bg-card border border-border flex items-center justify-between">
<div className="flex items-center gap-3">
<span className="w-8 h-8 rounded-lg bg-amber-500/10 text-amber-500 flex items-center justify-center">
<Building2 className="w-4 h-4" />
</span>
<div>
<p className="text-xs text-muted-foreground">Partner Brands</p>
<p className="text-sm font-bold text-foreground">{displayStats.total_brands} Brands</p>
</div>
</div>
<span className="text-xs font-semibold text-muted-foreground">{displayStats.total_categories} Categories</span>
</div>
<div className="p-3 rounded-lg bg-card border border-border flex items-center justify-between">
<div className="flex items-center gap-3">
<span className="w-8 h-8 rounded-lg bg-emerald-500/10 text-emerald-500 flex items-center justify-center">
<Layers className="w-4 h-4" />
</span>
<div>
<p className="text-xs text-muted-foreground">Supported Devices</p>
<p className="text-sm font-bold text-foreground">{displayStats.total_device_models} Models</p>
</div>
</div>
<span className="text-xs font-semibold text-muted-foreground">{displayStats.total_device_series} Series</span>
</div>
</div>
</div>
</div>
{/* Transaction Ledger Table */}
<div className={CARD}>
<div className="px-5 py-4 flex items-center justify-between gap-3">
<CardTitle>Executive Transaction Ledger</CardTitle>
<Link href="/orders" className="inline-flex items-center gap-1 text-[13px] font-medium text-primary hover:underline">
Audit Full Orders <ArrowRight className="w-3.5 h-3.5" />
</Link>
</div>
<div className="overflow-x-auto">
<table className="crm-data-table min-w-[680px]">
<thead>
<tr className="border-y border-border">
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Order Ref</th>
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Client / Customer</th>
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Transaction Value</th>
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Fulfillment Stage</th>
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Priority</th>
</tr>
</thead>
<tbody>
{(displayStats.recent_orders ?? []).map((order) => (
<tr key={order.order_no} className="border-b border-border last:border-0">
<td className="px-5 py-3 font-semibold text-foreground">{order.order_no}</td>
<td className="px-5 py-3 text-foreground font-medium">{order.customer_name}</td>
<td className="px-5 py-3 font-bold text-foreground">{formatInr(order.amount)}</td>
<td className="px-5 py-3 text-muted-foreground capitalize">{order.status}</td>
<td className="px-5 py-3">
<span className="inline-flex items-center h-6 px-2.5 rounded-full text-[11px] font-semibold bg-primary/10 text-primary border border-primary/20">
High Priority
</span>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
}