import React, { useState } from 'react'; import { Calendar, Clock, TrendingUp, Scale, Pencil, Trash2, X, Save, ArrowRight, ArrowUp, Timer, Activity, Dumbbell, Percent } from 'lucide-react'; import { WorkoutSession, ExerciseType, WorkoutSet, Language } from '../types'; import { t } from '../services/i18n'; interface HistoryProps { sessions: WorkoutSession[]; onUpdateSession?: (session: WorkoutSession) => void; onDeleteSession?: (sessionId: string) => void; lang: Language; } const History: React.FC = ({ sessions, onUpdateSession, onDeleteSession, lang }) => { const [editingSession, setEditingSession] = useState(null); const [deletingId, setDeletingId] = useState(null); const calculateSessionWork = (session: WorkoutSession) => { const bw = session.userBodyWeight || 70; return session.sets.reduce((acc, set) => { let w = 0; if (set.type === ExerciseType.STRENGTH) { w = (set.weight || 0) * (set.reps || 0); } if (set.type === ExerciseType.BODYWEIGHT) { const percent = set.bodyWeightPercentage || 100; const effectiveBw = bw * (percent / 100); w = (effectiveBw + (set.weight || 0)) * (set.reps || 0); } return acc + Math.max(0, w); }, 0); }; const formatDateForInput = (timestamp: number) => { const d = new Date(timestamp); const pad = (n: number) => n < 10 ? '0' + n : n; return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`; }; const parseDateFromInput = (value: string) => { return new Date(value).getTime(); }; const handleSaveEdit = () => { if (editingSession && onUpdateSession) { onUpdateSession(editingSession); setEditingSession(null); } }; const handleUpdateSet = (setId: string, field: keyof WorkoutSet, value: number) => { if (!editingSession) return; const updatedSets = editingSession.sets.map(s => s.id === setId ? { ...s, [field]: value } : s ); setEditingSession({ ...editingSession, sets: updatedSets }); }; const handleDeleteSet = (setId: string) => { if (!editingSession) return; setEditingSession({ ...editingSession, sets: editingSession.sets.filter(s => s.id !== setId) }); }; const handleConfirmDelete = () => { if (deletingId && onDeleteSession) { onDeleteSession(deletingId); setDeletingId(null); } } if (sessions.length === 0) { return (

{t('history_empty', lang)}

); } return (

{t('tab_history', lang)}

{sessions.map((session) => { const totalWork = calculateSessionWork(session); return (
{new Date(session.startTime).toLocaleDateString(lang === 'ru' ? 'ru-RU' : 'en-US', { weekday: 'long', day: 'numeric', month: 'long' })}
{new Date(session.startTime).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})} {session.userBodyWeight && {session.userBodyWeight}kg}
{Array.from(new Set(session.sets.map(s => s.exerciseName))).slice(0, 4).map(exName => { const sets = session.sets.filter(s => s.exerciseName === exName); const count = sets.length; const bestSet = sets[0]; let detail = ""; if (bestSet.type === ExerciseType.HIGH_JUMP) detail = `${t('max', lang)}: ${Math.max(...sets.map(s => s.height || 0))}cm`; else if (bestSet.type === ExerciseType.LONG_JUMP) detail = `${t('max', lang)}: ${Math.max(...sets.map(s => s.distanceMeters || 0))}m`; else if (bestSet.type === ExerciseType.STRENGTH) detail = `${t('upto', lang)} ${Math.max(...sets.map(s => s.weight || 0))}kg`; return (
{exName} {detail && {detail}} {count}
); })} {new Set(session.sets.map(s => s.exerciseName)).size > 4 && (
+ ...
)}
{t('sets_count', lang)}: {session.sets.length} {totalWork > 0 && ( {(totalWork / 1000).toFixed(1)}t )}
{t('finished', lang)}
)})}
{/* DELETE CONFIRMATION DIALOG (MD3) */} {deletingId && (

{t('delete_workout', lang)}

{t('delete_confirm', lang)}

)} {/* EDIT SESSION FULLSCREEN DIALOG */} {editingSession && (

{t('edit', lang)}

{/* Meta Info */}
setEditingSession({...editingSession, startTime: parseDateFromInput(e.target.value)})} className="w-full bg-transparent text-on-surface focus:outline-none text-sm mt-1" />
setEditingSession({...editingSession, endTime: parseDateFromInput(e.target.value)})} className="w-full bg-transparent text-on-surface focus:outline-none text-sm mt-1" />
setEditingSession({...editingSession, userBodyWeight: parseFloat(e.target.value)})} className="w-full bg-transparent text-on-surface focus:outline-none text-lg mt-1" />

{t('sets_count', lang)} ({editingSession.sets.length})

{editingSession.sets.map((set, idx) => (
{idx + 1} {set.exerciseName}
{(set.type === ExerciseType.STRENGTH || set.type === ExerciseType.BODYWEIGHT || set.type === ExerciseType.STATIC) && (
handleUpdateSet(set.id, 'weight', parseFloat(e.target.value))} />
)} {(set.type === ExerciseType.STRENGTH || set.type === ExerciseType.BODYWEIGHT || set.type === ExerciseType.PLYOMETRIC) && (
handleUpdateSet(set.id, 'reps', parseFloat(e.target.value))} />
)} {(set.type === ExerciseType.BODYWEIGHT || set.type === ExerciseType.STATIC) && (
handleUpdateSet(set.id, 'bodyWeightPercentage', parseFloat(e.target.value))} />
)} {/* Add other fields similarly styled if needed */}
))}
)}
); }; export default History;