"use client"; import { formatCurrencyFull } from "@/lib/analytics-utils"; import { MENU_CATEGORIES } from "@/lib/constants"; import type { ProductSalesStats } from "@/lib/types"; import { useMemo, useState } from "react"; interface ProductTableProps { data: ProductSalesStats[]; } const categoryName = (id: string) => MENU_CATEGORIES.find((c) => c.id === id)?.name ?? id; /** * Sortable product sales table. * Click column headers to sort ascending/descending. */ export function ProductTable({ data }: ProductTableProps) { const [sortKey, setSortKey] = useState("revenue"); const [sortDir, setSortDir] = useState<"asc" | "desc">("desc"); const sorted = useMemo( () => [...data].sort((a, b) => { const av = a[sortKey] as number; const bv = b[sortKey] as number; return sortDir === "desc" ? bv - av : av - bv; }), [data, sortKey, sortDir], ); const handleSort = (key: keyof ProductSalesStats) => { if (key === sortKey) setSortDir((d) => (d === "desc" ? "asc" : "desc")); else { setSortKey(key); setSortDir("desc"); } }; const sortIcon = (col: keyof ProductSalesStats) => ( ); const SortTh = ({ col, label, className = "", }: { col: keyof ProductSalesStats; label: string; className?: string; }) => ( handleSort(col)} > {label} {sortIcon(col)} ); return (
{sorted.map((row, i) => ( ))}
# Sản phẩm Danh mục Giá nhập Giá bán
{i + 1} {row.name} {categoryName(row.category)} {row.unitsSold.toLocaleString()} {formatCurrencyFull(row.revenue)} {formatCurrencyFull(row.costPrice)} {formatCurrencyFull(row.sellingPrice)} {formatCurrencyFull(row.profit)} = 70 ? "bg-green-100 text-green-700" : row.profitMargin >= 60 ? "bg-yellow-100 text-yellow-700" : "bg-red-100 text-red-600" }`} > {row.profitMargin.toFixed(1)}%
); }