"use client" import { useState } from "react" import { useAuth } from "@/lib/auth" import { apiFetch, ITrainingHomeworkAssignment, PaginatedResponse } from "@/lib/api" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Avatar, AvatarFallback } from "@/components/ui/avatar" import { Progress } from "@/components/ui/progress" import { Sheet, SheetContent, SheetHeader, SheetTitle } from "@/components/ui/sheet" import { FadeIn } from "@/components/ui/animations" import { Check, X, Loader2, BookOpen, Calendar, Dumbbell } from "lucide-react" import { toast } from "sonner" const groupConfig = { kids: { label: "Kinder", class: "bg-primary/10 text-primary" }, youth: { label: "Jugend", class: "bg-secondary/10 text-secondary" }, adults: { label: "Erwachsene", class: "bg-accent/10 text-accent" }, } const categoryConfig = { warmup: { label: "Aufwärmen", class: "bg-primary/10 text-primary" }, kraft: { label: "Kraft", class: "bg-destructive/10 text-destructive" }, technik: { label: "Technik", class: "bg-secondary/10 text-secondary" }, ausdauer: { label: "Ausdauer", class: "bg-accent/10 text-accent" }, spiele: { label: "Spiele", class: "bg-muted text-muted-foreground" }, cool_down: { label: "Abkühlung", class: "bg-primary/5 text-primary" }, } interface GroupedByWrestler { wrestlerId: number wrestlerName: string wrestlerGroup: string assignments: ITrainingHomeworkAssignment[] completedCount: number totalCount: number } interface WrestlerCentricViewProps { assignments: ITrainingHomeworkAssignment[] onToggleComplete: (id: number, current: boolean) => void togglingId: number | null } export function WrestlerCentricView({ assignments, onToggleComplete, togglingId }: WrestlerCentricViewProps) { const [selectedWrestler, setSelectedWrestler] = useState(null) // Group assignments by wrestler const groupedByWrestler: GroupedByWrestler[] = assignments.reduce((acc, assignment) => { const existing = acc.find(g => g.wrestlerId === assignment.wrestler) if (existing) { existing.assignments.push(assignment) if (assignment.is_completed) existing.completedCount++ existing.totalCount++ } else { acc.push({ wrestlerId: assignment.wrestler, wrestlerName: assignment.wrestler_name, wrestlerGroup: assignment.wrestler_group, assignments: [assignment], completedCount: assignment.is_completed ? 1 : 0, totalCount: 1, }) } return acc }, [] as GroupedByWrestler[]) // Sort wrestlers by: those with incomplete homework first, then by name groupedByWrestler.sort((a, b) => { const aIncomplete = a.totalCount - a.completedCount const bIncomplete = b.totalCount - b.completedCount if (aIncomplete !== bIncomplete) return bIncomplete - aIncomplete return a.wrestlerName.localeCompare(b.wrestlerName) }) const getProgress = (wrestler: GroupedByWrestler) => { if (wrestler.totalCount === 0) return 0 return Math.round((wrestler.completedCount / wrestler.totalCount) * 100) } return ( <>
{groupedByWrestler.map((wrestler) => { const progress = getProgress(wrestler) const isComplete = progress === 100 return ( setSelectedWrestler(wrestler)} >
{wrestler.wrestlerName?.split(" ").map(n => n[0]).slice(0,2).join("")}
{wrestler.wrestlerName} {groupConfig[wrestler.wrestlerGroup as keyof typeof groupConfig]?.label}
{wrestler.completedCount}/{wrestler.totalCount}
{isComplete ? 'Alle erledigt' : 'Offen'}
{progress}%
Letztes Training: {wrestler.assignments[0]?.training_date ? new Date(wrestler.assignments[0].training_date).toLocaleDateString("de-DE") : "Keine"}
) })}
{/* Detail Sheet */} setSelectedWrestler(null)}> {selectedWrestler?.wrestlerName} {selectedWrestler && (
{selectedWrestler.assignments .sort((a, b) => new Date(b.training_date).getTime() - new Date(a.training_date).getTime()) .map(assignment => (
{new Date(assignment.training_date).toLocaleDateString("de-DE")} {assignment.is_completed ? "Erledigt" : "Offen"}
{assignment.exercises.map(ex => (
{categoryConfig[ex.exercise_category as keyof typeof categoryConfig]?.label} {ex.exercise_name} {ex.reps && {ex.reps}x} {ex.time_minutes && {ex.time_minutes}s}
))}
{assignment.notes && (

{assignment.notes}

)}
))}
)}
) }