import Link from "next/link"; import { endOfMonth, startOfMonth, startOfDay, endOfDay, subWeeks, startOfWeek, addDays, format as fnsFormat } from "date-fns"; import { format } from "date-fns"; import { de } from "date-fns/locale"; import { BarChart3, Calendar, CheckCircle2, Clock, Users, AlertTriangle, ArrowRight } from "lucide-react"; import { prisma } from "@/lib/prisma"; import { LatestBookingsPanel } from "@/components/admin/latest-bookings-panel"; export const dynamic = "force-dynamic"; export default async function AdminOverviewPage() { const now = new Date(); const eightWeeksAgo = subWeeks(now, 7); const [ activeResources, totalResources, upcomingAppointments, monthTotal, monthCancelled, monthNoShow, openDeliveryIssues, todayAppointments, weeklyAppointments ] = await Promise.all([ prisma.calendarConn.count({ where: { user: { role: "STAFF", isActive: true } } }), prisma.calendarConn.count({ where: { user: { role: "STAFF" } } }), prisma.appointment.count({ where: { status: "CONFIRMED", startAt: { gte: now } } }), prisma.appointment.count({ where: { status: "CONFIRMED", startAt: { gte: startOfMonth(now), lte: endOfMonth(now) } } }), prisma.appointment.count({ where: { status: "CANCELLED", startAt: { gte: startOfMonth(now), lte: endOfMonth(now) } } }), prisma.appointment.count({ where: { status: "CONFIRMED", noShowAt: { not: null }, startAt: { gte: startOfMonth(now), lte: endOfMonth(now) } } }), prisma.deliveryIssue.count({ where: { resolvedAt: null } }), prisma.appointment.findMany({ where: { status: "CONFIRMED", startAt: { gte: startOfDay(now), lte: endOfDay(now) } }, include: { staff: { select: { name: true } } }, orderBy: { startAt: "asc" } }), prisma.appointment.findMany({ where: { status: "CONFIRMED", startAt: { gte: eightWeeksAgo } }, select: { startAt: true } }) ]); // Build weekly chart data const weeks: { label: string; count: number }[] = []; for (let i = 7; i >= 0; i--) { const weekStart = startOfWeek(subWeeks(now, i), { weekStartsOn: 1 }); const weekEnd = addDays(weekStart, 6); const count = weeklyAppointments.filter((a) => a.startAt >= weekStart && a.startAt <= weekEnd).length; weeks.push({ label: format(weekStart, "dd.MM.", { locale: de }), count }); } const maxCount = Math.max(1, ...weeks.map((w) => w.count)); return (

Dashboard

{format(now, "EEEE, d. MMMM yyyy", { locale: de })}

{/* Stats */}
{[ { icon: Calendar, iconBg: "bg-indigo-50", iconColor: "text-indigo-600", value: upcomingAppointments, label: "Offene Buchungen" }, { icon: Users, iconBg: "bg-emerald-50", iconColor: "text-emerald-600", value: <>{activeResources}/{totalResources}, label: "Aktive Kalender" }, { icon: AlertTriangle, iconBg: "bg-amber-50", iconColor: "text-amber-600", value: openDeliveryIssues, label: "Zustellfehler" }, { icon: CheckCircle2, iconBg: "bg-slate-900", iconColor: "text-white", value: Aktiv, label: "System läuft" } ].map((stat, i) => (
{stat.value}
{stat.label}
))}
{/* Chart + Today's appointments side by side */}
{/* Weekly chart */}

Buchungen

letzte 8 Wochen
{weeks.map((week, i) => (
{week.count}
{week.label}
))}
{/* Today's appointments */}

Heutige Termine

{todayAppointments.length}
Alle
{todayAppointments.length === 0 ? (
Keine Termine für heute.
) : (
{todayAppointments.map((a) => (
{format(new Date(a.startAt), "HH:mm")}

{a.customerFirstName} {a.customerLastName}

{a.customerEmail}

))}
)}
{/* Latest bookings */}
Kalender verwalten Termine anzeigen
); }