"use client";
import React, { useEffect, useState, startTransition } from "react";
import { Package, CalendarCheck, Clock, TrendingUp, ShoppingBag, Users, AlertTriangle, Euro } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { useAdmin } from "@/contexts/AdminContext";
import { useLanguage } from "@/contexts/LanguageContext";
import { getDashboardStats, DashboardStats } from "@/lib/api/admin";
function Skeleton() {
return (
{Array.from({ length: 4 }).map((_, i) => (
))}
);
}
function StatGrid({ cards }: { cards: { label: string; value: string | number; icon: React.ElementType; color: string; bg: string }[] }) {
return (
);
}
export default function AdminOverview() {
const { reservations } = useAdmin();
const { t, locale } = useLanguage();
const [stats, setStats] = useState(null);
useEffect(() => {
getDashboardStats().then(setStats).catch((e) => console.error("[admin] getDashboardStats failed:", e));
}, []);
const upcoming = [...reservations]
.filter((r) => r.status !== "cancelled")
.sort((a, b) => `${a.date}${a.time}`.localeCompare(`${b.date}${b.time}`))
.slice(0, 5);
const mainCards = stats
? [
{ label: t("admin.overview.orders_pending"), value: stats.orders_pending, icon: ShoppingBag, color: "text-orange-600", bg: "bg-orange-100" },
{ label: t("admin.overview.bookings_pending"), value: stats.bookings_pending, icon: Clock, color: "text-amber-600", bg: "bg-amber-100" },
{ label: t("admin.overview.bookings_confirmed"),value: stats.bookings_confirmed,icon: CalendarCheck,color: "text-green-600", bg: "bg-green-100" },
{ label: t("admin.overview.products_count"), value: stats.products_count, icon: Package, color: "text-blue-600", bg: "bg-blue-100" },
]
: [];
const revenueCards = stats
? [
{ label: t("admin.overview.revenue_today"), value: `${stats.revenue_today.toFixed(2)} €`, icon: Euro, color: "text-emerald-600", bg: "bg-emerald-100" },
{ label: t("admin.overview.revenue_week"), value: `${stats.revenue_week.toFixed(2)} €`, icon: TrendingUp, color: "text-emerald-600", bg: "bg-emerald-100" },
{ label: t("admin.overview.revenue_month"), value: `${stats.revenue_month.toFixed(2)} €`, icon: TrendingUp, color: "text-primary", bg: "bg-primary/10" },
{ label: t("admin.overview.new_customers"), value: stats.new_customers_month, icon: Users, color: "text-purple-600", bg: "bg-purple-100" },
]
: [];
return (
{t("admin.overview.title")}
{t("admin.overview.subtitle")}
{t("admin.overview.activity_section")}
{stats === null ? : }
{t("admin.overview.revenue_section")}
{stats === null ? : }
{stats && stats.low_stock_count > 0 && (
{t("admin.overview.low_stock", { n: stats.low_stock_count })}
)}
{t("admin.overview.upcoming_title")}
{upcoming.length === 0 ? (
{t("admin.overview.no_upcoming")}
) : (
{upcoming.map((r) => (
{r.clientName}
{r.service}
{new Date(r.date).toLocaleDateString(locale, { day: "2-digit", month: "short" })} à {r.time}
{r.status === "confirmed" ? t("admin.status.confirmed") : t("admin.status.pending")}
))}
)}
);
}