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

184 lines
7.7 KiB
TypeScript

'use client';
import {
Building2,
Users,
Boxes,
DollarSign,
} from 'lucide-react';
import { cn } from '@/lib/utils';
import Link from 'next/link';
import { DashboardHeaderNav } from '@/components/dashboard/DashboardHeaderNav';
import { useDashboardData } from '@/components/dashboard/useDashboardData';
import {
CARD,
formatInr,
CardTitle,
DeltaPill,
} from '@/components/dashboard/DashboardShared';
export default function B2BDashboardPage() {
const {
stats: displayStats,
loading,
refreshing,
fetchData,
activity,
} = useDashboardData();
const totalOrders = displayStats.total_orders ?? 0;
const averageOrderValue = totalOrders > 0 ? displayStats.revenue_all_time / totalOrders : 0;
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="B2B Wholesale 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">Total Wholesale Revenue</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">
<Building2 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">Active B2B Accounts</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
{displayStats.total_users}
</p>
</div>
<span className="w-10 h-10 rounded-full bg-blue-600 text-white flex items-center justify-center shrink-0">
<Users className="w-5 h-5" />
</span>
</div>
<div className="mt-2 flex items-center gap-2">
<DeltaPill value={activity.customersDelta} />
<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">Bulk Orders Volume</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">{totalOrders}</p>
</div>
<span className="w-10 h-10 rounded-full bg-emerald-600 text-white flex items-center justify-center shrink-0">
<Boxes className="w-5 h-5" />
</span>
</div>
<div className="mt-2 flex items-center gap-2">
<DeltaPill value={activity.ordersDelta} />
<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">Avg B2B Order Value</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
{formatInr(averageOrderValue)}
</p>
</div>
<span className="w-10 h-10 rounded-full bg-purple-600 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.aovDelta} />
<span className="text-[12px] text-muted-foreground">vs prior 7 days</span>
</div>
</div>
</div>
{/* Orders Ledger & Top Wholesale Products */}
<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 items-center justify-between gap-2 mb-4">
<CardTitle>B2B Wholesale Orders Ledger</CardTitle>
<Link href="/orders" className="text-xs font-semibold text-primary hover:underline">
View All Bulk Orders
</Link>
</div>
<div className="overflow-x-auto">
<table className="crm-data-table min-w-[550px]">
<thead>
<tr className="border-y border-border">
<th className="text-left font-medium text-muted-foreground px-4 py-2.5">Bulk Order #</th>
<th className="text-left font-medium text-muted-foreground px-4 py-2.5">Corporate Client</th>
<th className="text-left font-medium text-muted-foreground px-4 py-2.5">Order Value</th>
<th className="text-left font-medium text-muted-foreground px-4 py-2.5">Fulfillment</th>
</tr>
</thead>
<tbody>
{(displayStats.recent_orders ?? []).length === 0 ? (
<tr>
<td colSpan={4} className="px-4 py-8 text-center text-[13px] text-muted-foreground whitespace-normal">No wholesale orders yet</td>
</tr>
) : (displayStats.recent_orders ?? []).map((order) => (
<tr key={order.order_no} className="border-b border-border last:border-0">
<td className="px-4 py-3 font-semibold text-foreground">{order.order_no}</td>
<td className="px-4 py-3 font-medium text-foreground">{order.customer_name}</td>
<td className="px-4 py-3 font-bold text-foreground">{formatInr(order.amount)}</td>
<td className="px-4 py-3 text-muted-foreground capitalize">{order.status}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
<div className={cn(CARD, 'p-5 flex flex-col')}>
<CardTitle>Top Wholesale Products</CardTitle>
<div className="mt-4 space-y-3 flex-1">
{(displayStats.top_products ?? []).length === 0 ? (
<p className="text-[13px] text-muted-foreground text-center py-6">No wholesale data yet</p>
) : (
(displayStats.top_products ?? []).slice(0, 5).map((product) => (
<div key={product.name} className="flex items-center justify-between py-2 border-b border-border last:border-0">
<div>
<p className="text-xs font-semibold text-foreground truncate max-w-[160px]">{product.name}</p>
<p className="text-[11px] text-muted-foreground">{product.total_sold} units sold</p>
</div>
<span className="text-xs font-bold text-foreground">{formatInr(product.total_revenue)}</span>
</div>
))
)}
</div>
</div>
</div>
</div>
);
}