'use client'; import { useState, useEffect, useMemo } from 'react'; import { Package, Tag, RefreshCw, CheckCircle, DollarSign, Calendar, ChevronDown, ListFilter, ArrowUpRight, ArrowDownRight, } from 'lucide-react'; import { toast } from 'sonner'; import { useTheme } from 'next-themes'; import type { ApexOptions } from 'apexcharts'; import { catalogService, ProductResponse, CategoryResponse, BrandResponse, } from '@/services/api/catalogService'; import { ApexChart } from '@/components/charts/ApexChart'; import { cn } from '@/lib/utils'; import { apiFetch } from '@/services/api/client'; import { percentChange, signedPercent } from '@/components/dashboard/DashboardShared'; const PAGE_DATE_RANGE = '26 Jan 2026 - 26 Jan 2027'; const CARD_DATE_RANGE = '26 Jan 2026 - 26 Jan 2027'; const DAY_MS = 86400000; function TrendText({ value, label, invert = false, }: { value: number; label?: string; invert?: boolean; }) { const down = value < 0; const favorable = invert ? value <= 0 : value >= 0; const tone = value === 0 ? 'text-muted-foreground' : favorable ? 'text-success' : 'text-destructive'; const Icon = down ? ArrowDownRight : ArrowUpRight; return ( {signedPercent(value)} {label ? ` ${label}` : ''} ); } const CARD = 'min-w-0 overflow-hidden bg-card border border-border rounded-[5px] shadow-[0_1px_2px_rgba(16,24,40,0.04)]'; function PipelineStage({ label, value, pct, fill, isDark, }: { label: string; value: string; pct: number; fill: string; isDark: boolean; }) { const remainder = isDark ? `${fill}26` : `${fill}24`; const stripe = isDark ? `${fill}66` : `${fill}40`; return (
{label} - {value}
); } function WonLostRings({ values, track, }: { values: number[]; track: string; }) { const size = 180; const cx = size / 2; const cy = size / 2; const stroke = 11; const radii = [74, 54, 34]; const colors = ['#5BA4C7', '#3538CD', '#1B2559']; const arc = (r: number, pct: number) => { const start = -Math.PI / 2; const sweep = (Math.min(Math.max(pct, 0.01), 99.999) / 100) * Math.PI * 2; const end = start + sweep; const x1 = cx + r * Math.cos(start); const y1 = cy + r * Math.sin(start); const x2 = cx + r * Math.cos(end); const y2 = cy + r * Math.sin(end); const large = sweep > Math.PI ? 1 : 0; return `M ${x1} ${y1} A ${r} ${r} 0 ${large} 1 ${x2} ${y2}`; }; return ( {radii.map((r, i) => ( ))} ); } function ConversionGauge({ value, isDark, labelColor, }: { value: number; isDark: boolean; labelColor: string; }) { const ticks = 38; const filled = Math.round((value / 100) * ticks); const inactive = isDark ? '#2d3656' : '#E6E8EE'; const cx = 120; const cy = 118; const inner = 74; const outer = 104; return ( {Array.from({ length: ticks }, (_, i) => { const t = i / (ticks - 1); const angle = Math.PI - t * Math.PI; const x1 = cx + inner * Math.cos(angle); const y1 = cy - inner * Math.sin(angle); const x2 = cx + outer * Math.cos(angle); const y2 = cy - outer * Math.sin(angle); return ( ); })} {value.toFixed(1)}% ); } export default function ProductDashboardPage() { const { resolvedTheme } = useTheme(); const [products, setProducts] = useState([]); const [categories, setCategories] = useState([]); const [brands, setBrands] = useState([]); const [orders, setOrders] = useState([]); const [loading, setLoading] = useState(true); const [period, setPeriod] = useState<'weekly' | 'monthly' | 'yearly'>('weekly'); const isDark = resolvedTheme === 'dark'; const labelColor = isDark ? '#e0e5f2' : '#1b2559'; const fetchCatalogStats = async () => { setLoading(true); try { const [fetchedProducts, fetchedCategories, fetchedBrands] = await Promise.all([ catalogService.getProducts().catch(() => []), catalogService.getCategories().catch(() => []), catalogService.getBrands().catch(() => []), ]); const productRows = Array.isArray(fetchedProducts) ? fetchedProducts : fetchedProducts?.products ?? []; setProducts(productRows); setCategories(fetchedCategories || []); setBrands(fetchedBrands || []); // Load order stats non-blockingly so dashboard renders instantly (<100ms) apiFetch('/api/v1/admin/orders') .then((fetchedOrders) => { setOrders(Array.isArray(fetchedOrders) ? fetchedOrders : []); }) .catch(() => { setOrders([]); }); } catch (err: any) { toast.error(err?.message || 'Failed to fetch product dashboard stats'); } finally { setLoading(false); } }; useEffect(() => { fetchCatalogStats(); }, []); const totalProducts = products.length; const totalVariants = products.reduce((acc, p) => acc + (p.variants?.length || 0), 0); const totalCategories = categories.length; const totalBrands = brands.length; const activeProducts = products.filter((p) => p.status === 'active').length; const draftProducts = products.filter((p) => p.status === 'draft').length; const allPrices = products.flatMap((p) => p.variants?.map((v) => Number(v.price)) || []); const avgPrice = allPrices.length > 0 ? (allPrices.reduce((a, b) => a + b, 0) / allPrices.length).toFixed(2) : '0.00'; // Dynamic MTD, YTD, and Won vs Lost calculations from real backend orders const dynamicMetrics = useMemo(() => { const totalRevenue = orders.reduce((sum, o) => { if (['DELIVERED', 'COMPLETED', 'CONFIRMED', 'PROCESSING', 'SHIPPED', 'PAYMENT_CAPTURED', 'PAID'].includes((o.status || '').toUpperCase()) || (o.payment_status || '').toUpperCase() === 'PAID') { return sum + Number(o.final_amount || 0); } return sum; }, 0); const wonCount = orders.filter(o => ['DELIVERED', 'COMPLETED', 'CONFIRMED', 'PROCESSING', 'SHIPPED', 'PAID'].includes((o.status || '').toUpperCase()) || (o.payment_status || '').toUpperCase() === 'PAID').length; const lostCount = orders.filter(o => ['CANCELLED', 'RETURNED'].includes((o.status || '').toUpperCase())).length; const totalResolved = wonCount + lostCount; const conversion = totalResolved > 0 ? ((wonCount / totalResolved) * 100).toFixed(1) : '0.0'; // MTD / YTD calculations const now = new Date(); const currentMonth = now.getMonth(); const currentYear = now.getFullYear(); const mtdRev = orders.filter(o => { if (!o.created_at) return false; const d = new Date(o.created_at); return d.getMonth() === currentMonth && d.getFullYear() === currentYear; }).reduce((s, o) => s + Number(o.final_amount || 0), 0); const formatInrStr = (val: number) => { return new Intl.NumberFormat('en-IN', { style: 'currency', currency: 'INR', maximumFractionDigits: 0 }).format(val); }; // Dynamic Sales Pipeline Stage Calculations const pendingSum = orders .filter(o => ['PENDING', 'CREATED', 'DRAFT', 'PAYMENT_PENDING'].includes((o.status || '').toUpperCase())) .reduce((s, o) => s + Number(o.final_amount || o.total_amount || 0), 0); const proposalSum = orders .filter(o => ['CONFIRMED', 'PROCESSING', 'QUOTE_SENT', 'QUOTE_ACCEPTED'].includes((o.status || '').toUpperCase())) .reduce((s, o) => s + Number(o.final_amount || o.total_amount || 0), 0); const opportunitySum = orders .filter(o => ['SHIPPED', 'IN_TRANSIT', 'READY_FOR_DELIVERY'].includes((o.status || '').toUpperCase())) .reduce((s, o) => s + Number(o.final_amount || o.total_amount || 0), 0); const totalDealsSum = totalRevenue > 0 ? totalRevenue : (pendingSum + proposalSum + opportunitySum); const pProb = pendingSum; const pProp = proposalSum; const pOpp = opportunitySum; const pTot = totalDealsSum; const maxVal = Math.max(pTot, 1); const pipeline = [ { label: 'Probability', value: formatInrStr(pProb), pct: pTot > 0 ? Math.min(100, Math.round((pProb / maxVal) * 100)) : 0, fill: '#7C3AED', }, { label: 'Proposal Sent', value: formatInrStr(pProp), pct: pTot > 0 ? Math.min(100, Math.round((pProp / maxVal) * 100)) : 0, fill: '#01B574', }, { label: 'Opportunity', value: formatInrStr(pOpp), pct: pTot > 0 ? Math.min(100, Math.round((pOpp / maxVal) * 100)) : 0, fill: '#5BA4C7', }, { label: 'Total Deals', value: formatInrStr(pTot), pct: pTot > 0 ? 90 : 0, fill: '#1B2559', }, ]; const nowMs = now.getTime(); const monthStart = new Date(now.getFullYear(), now.getMonth(), 1).getTime(); const prevMonthStart = new Date(now.getFullYear(), now.getMonth() - 1, 1).getTime(); const yearStart = new Date(now.getFullYear(), 0, 1).getTime(); const prevYearStart = new Date(now.getFullYear() - 1, 0, 1).getTime(); const weekStart = nowMs - 7 * DAY_MS; const prevWeekStart = nowMs - 14 * DAY_MS; const stampOf = (order: { created_at?: string | null }) => { if (!order.created_at) return null; const stamp = new Date(order.created_at).getTime(); return Number.isNaN(stamp) ? null : stamp; }; const inWindow = (order: { created_at?: string | null }, start: number, end: number) => { const stamp = stampOf(order); return stamp !== null && stamp >= start && stamp < end; }; const orderAmount = (order: { final_amount?: number | string; total_amount?: number | string }) => Number(order.final_amount || order.total_amount || 0) || 0; const sumWindow = (start: number, end: number) => orders.reduce((sum, order) => (inWindow(order, start, end) ? sum + orderAmount(order) : sum), 0); const wonStatus = (order: { status?: string; payment_status?: string }) => ['DELIVERED', 'COMPLETED', 'CONFIRMED', 'PROCESSING', 'SHIPPED', 'PAID'].includes((order.status || '').toUpperCase()) || (order.payment_status || '').toUpperCase() === 'PAID'; const lostStatus = (order: { status?: string }) => ['CANCELLED', 'RETURNED'].includes((order.status || '').toUpperCase()); const countWindow = (start: number, end: number, match: (order: (typeof orders)[number]) => boolean) => orders.filter((order) => inWindow(order, start, end) && match(order)).length; const closeRate = (won: number, lost: number) => (won + lost > 0 ? (won / (won + lost)) * 100 : 0); const ytdRev = sumWindow(yearStart, nowMs + 1); const prevYtd = sumWindow(prevYearStart, yearStart); const prevMtd = sumWindow(prevMonthStart, monthStart); const wonWeek = countWindow(weekStart, nowMs + 1, wonStatus); const wonPrevWeek = countWindow(prevWeekStart, weekStart, wonStatus); const lostWeek = countWindow(weekStart, nowMs + 1, lostStatus); const lostPrevWeek = countWindow(prevWeekStart, weekStart, lostStatus); const wonMonth = countWindow(monthStart, nowMs + 1, wonStatus); const wonPrevMonth = countWindow(prevMonthStart, monthStart, wonStatus); const weekOrders = orders.filter((order) => inWindow(order, weekStart, nowMs + 1)); const prevWeekOrders = orders.filter((order) => inWindow(order, prevWeekStart, weekStart)); const average = (rows: typeof orders) => rows.length > 0 ? rows.reduce((sum, order) => sum + orderAmount(order), 0) / rows.length : 0; const sparkMtd = Array.from({ length: 7 }, (_, index) => { const start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - (6 - index)).setHours(0, 0, 0, 0); return sumWindow(start, start + DAY_MS); }); const sparkYtd = Array.from({ length: 7 }, (_, index) => { const start = new Date(now.getFullYear(), now.getMonth() - (6 - index), 1).getTime(); const end = new Date(now.getFullYear(), now.getMonth() - (5 - index), 1).getTime(); return sumWindow(start, end); }); return { mtdValue: formatInrStr(mtdRev), ytdValue: formatInrStr(ytdRev), pipelineValue: formatInrStr(pTot), conversion: Number(conversion), won: wonCount, lost: lostCount, lostShare: totalResolved > 0 ? (lostCount / totalResolved) * 100 : 0, pipeline, mtdChange: percentChange(mtdRev, prevMtd), ytdChange: percentChange(ytdRev, prevYtd), conversionChange: percentChange( closeRate(wonWeek, lostWeek), closeRate(wonPrevWeek, lostPrevWeek) ), wonVsLostChange: percentChange(wonMonth, wonPrevMonth), wonChange: percentChange(wonWeek, wonPrevWeek), lostChange: percentChange(lostWeek, lostPrevWeek), pipelineChange: percentChange(sumWindow(weekStart, nowMs + 1), sumWindow(prevWeekStart, weekStart)), dealSizeChange: percentChange(average(weekOrders), average(prevWeekOrders)), sparkMtd, sparkYtd, }; }, [orders]); const categoryRows = categories.map((cat) => { const count = products.filter((p) => p.category_id === cat.category_id).length; const pct = totalProducts > 0 ? (count / totalProducts) * 100 : 0; return { id: cat.category_id, name: cat.name, count, pct }; }); const sparkOptions = (color: string): ApexOptions => ({ chart: { type: 'bar', sparkline: { enabled: true }, toolbar: { show: false } }, plotOptions: { bar: { columnWidth: '52%', borderRadius: 1 } }, colors: [color], tooltip: { enabled: false }, grid: { show: false }, stroke: { width: 0 }, }); const avgPriceOptions = useMemo(() => ({ chart: { type: 'area', sparkline: { enabled: true }, toolbar: { show: false } }, stroke: { curve: 'smooth', width: 2, colors: ['#5BA4C7'] }, fill: { type: 'gradient', gradient: { shadeIntensity: 1, opacityFrom: 0.28, opacityTo: 0.04, stops: [0, 100] }, }, colors: ['#5BA4C7'], tooltip: { enabled: false }, }), []); if (loading) { return (
); } return (

Product Dashboard

{PAGE_DATE_RANGE}

Total Revenue

{CARD_DATE_RANGE}

{(['weekly', 'monthly', 'yearly'] as const).map((mode) => ( ))}
MTD

Total MTD Revenue

{dynamicMetrics.mtdValue}

{totalProducts} sales products

YTD

Total YTD Revenue

{dynamicMetrics.ytdValue}

{totalVariants} price variants

Conversion Rate

{CARD_DATE_RANGE}

Deals Won Vs Lost

Deals Won

{dynamicMetrics.won}

Deals Lost

{dynamicMetrics.lost}

Sales Pipeline Overview

{dynamicMetrics.pipelineValue}

{dynamicMetrics.pipeline.map((row) => ( ))}

Recently Created Deals

Weekly
{categoryRows.length === 0 ? ( ) : categoryRows.map((row) => ( ))}
Category Products Status
No catalog items found to graph.
{row.name} {row.count} {row.pct.toFixed(0)}%

Avg Deal Size

₹{avgPrice}

Computed from all SKUs

Catalog Health

Active / Draft products {activeProducts} / {draftProducts}
Brands / Categories {totalBrands} / {totalCategories}
{products.length === 0 ? (

All systems operational

) : (
Active Product SKUs {totalVariants}
)}
); }