ifixkart-admin/app/(admin)/dashboard/store-manager/page.tsx

247 lines
11 KiB
TypeScript

'use client';
import {
ArrowRight,
ShoppingBag,
Clock,
Boxes,
CheckCircle2,
Building2,
Layers,
AlertCircle,
} 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,
PIPELINE,
PAYMENT_TAG,
formatCompact,
formatInr,
CardTitle,
DeltaPill,
} from '@/components/dashboard/DashboardShared';
export default function StoreManagerDashboardPage() {
const {
stats: displayStats,
loading,
refreshing,
userName,
fetchData,
} = 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 conversionDelta = 15.5;
const pipelineRows = PIPELINE.map((stage) => ({
...stage,
count: countOf(stage.key),
value: countOf(stage.key) * (displayStats.revenue_all_time / Math.max(totalOrders, 1)),
}));
const maxPipeline = Math.max(...pipelineRows.map((row) => row.count), 1);
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="Store Manager Dashboard"
subtitle="Operational overview, fulfillment pipeline stages, and order dispatch queues"
userName={userName}
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">Active Store Orders</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-blue-600 text-white flex items-center justify-center shrink-0">
<ShoppingBag className="w-5 h-5" />
</span>
</div>
<div className="mt-2 flex items-center gap-2">
<DeltaPill value={2.5} />
<span className="text-[12px] text-muted-foreground">Store traffic steady</span>
</div>
</div>
<div className={cn(CARD, 'p-4')}>
<div className="flex items-start justify-between">
<div>
<p className="text-[13px] text-muted-foreground">Pending &amp; In-Transit</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
{countOf('pending') + countOf('processing') + countOf('shipped')}
</p>
</div>
<span className="w-10 h-10 rounded-full bg-amber-600 text-white flex items-center justify-center shrink-0">
<Clock className="w-5 h-5" />
</span>
</div>
<div className="mt-2 flex items-center gap-2">
<span className="text-[12px] text-muted-foreground">Requires Dispatch Action</span>
</div>
</div>
<div className={cn(CARD, 'p-4')}>
<div className="flex items-start justify-between">
<div>
<p className="text-[13px] text-muted-foreground">Catalog Stock Units</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
{(displayStats.total_stock_units ?? 0).toLocaleString()}
</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">
<span className="text-[12px] text-muted-foreground">{displayStats.total_products} Active SKUs</span>
</div>
</div>
<div className={cn(CARD, 'p-4')}>
<div className="flex items-start justify-between">
<div>
<p className="text-[13px] text-muted-foreground">Fulfilled Orders</p>
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">{delivered}</p>
</div>
<span className="w-10 h-10 rounded-full bg-purple-600 text-white flex items-center justify-center shrink-0">
<CheckCircle2 className="w-5 h-5" />
</span>
</div>
<div className="mt-2 flex items-center gap-2">
<DeltaPill value={conversionDelta} />
<span className="text-[12px] text-muted-foreground">Successful deliveries</span>
</div>
</div>
</div>
{/* Fulfillment Pipeline & Inventory */}
<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')}>
<CardTitle>Store Operations &amp; Fulfillment Pipeline</CardTitle>
<div className="grid grid-cols-4 gap-2 my-4">
{pipelineRows.map((row) => (
<div key={row.key} className="p-3 rounded-lg bg-card border border-border">
<p className="text-[11px] text-muted-foreground">{row.label}</p>
<p className="text-base font-bold text-foreground mt-0.5">{row.count} Orders</p>
<p className="text-[11px] text-muted-foreground mt-0.5">{formatCompact(row.value)}</p>
</div>
))}
</div>
<div className="flex items-end gap-4 h-[140px] pt-2">
{pipelineRows.map((row) => (
<div key={row.key} className="flex-1 flex flex-col items-center justify-end h-full gap-2">
<div
className="w-full max-w-[48px] rounded-t-lg transition-all"
style={{
height: `${Math.max(20, (row.count / maxPipeline) * 100)}%`,
backgroundColor: row.color,
}}
/>
<span className="text-xs font-semibold text-foreground">{row.label}</span>
</div>
))}
</div>
</div>
<div className={cn(CARD, 'p-5 flex flex-col')}>
<CardTitle>Store Inventory Overview</CardTitle>
<div className="mt-4 space-y-4 flex-1">
<div className="flex items-center justify-between p-3 rounded-lg bg-muted/50 border border-border">
<div className="flex items-center gap-2.5">
<Boxes className="w-4 h-4 text-primary" />
<span className="text-xs font-medium text-foreground">Total Catalog Products</span>
</div>
<span className="text-xs font-bold text-foreground">{displayStats.total_products}</span>
</div>
<div className="flex items-center justify-between p-3 rounded-lg bg-muted/50 border border-border">
<div className="flex items-center gap-2.5">
<Building2 className="w-4 h-4 text-blue-500" />
<span className="text-xs font-medium text-foreground">Active Brands</span>
</div>
<span className="text-xs font-bold text-foreground">{displayStats.total_brands}</span>
</div>
<div className="flex items-center justify-between p-3 rounded-lg bg-muted/50 border border-border">
<div className="flex items-center gap-2.5">
<Layers className="w-4 h-4 text-amber-500" />
<span className="text-xs font-medium text-foreground">Categories Listed</span>
</div>
<span className="text-xs font-bold text-foreground">{displayStats.total_categories}</span>
</div>
<div className="flex items-center justify-between p-3 rounded-lg bg-muted/50 border border-border">
<div className="flex items-center gap-2.5">
<AlertCircle className="w-4 h-4 text-emerald-500" />
<span className="text-xs font-medium text-foreground">Total Stock Units</span>
</div>
<span className="text-xs font-bold text-foreground">{displayStats.total_stock_units}</span>
</div>
</div>
</div>
</div>
{/* Dispatch Queue Table */}
<div className={CARD}>
<div className="px-5 py-4 flex items-center justify-between gap-3">
<CardTitle>Store Manager Order Dispatch Queue</CardTitle>
<Link href="/orders" className="inline-flex items-center gap-1 text-[13px] font-medium text-primary hover:underline">
Manage 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</th>
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Dispatch Stage</th>
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Value</th>
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Payment</th>
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Customer</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-muted-foreground capitalize">{order.status}</td>
<td className="px-5 py-3 font-bold text-foreground">{formatInr(order.amount)}</td>
<td className="px-5 py-3">
<span className={cn('inline-flex items-center h-6 px-2 crm-radius-badge text-[11px] font-semibold border capitalize', PAYMENT_TAG[order.payment_status] ?? 'border-border text-muted-foreground')}>
{order.payment_status || '—'}
</span>
</td>
<td className="px-5 py-3 text-foreground">{order.customer_name}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
}