import React, { useState, useEffect } from 'react'; import { Plus, Trash2, PlayCircle, Dumbbell, Save, X, ChevronRight, List, ArrowUp, ArrowDown, Scale } from 'lucide-react'; import { WorkoutPlan, ExerciseDef, PlannedSet, Language } from '../types'; import { getPlans, savePlan, deletePlan, getExercises } from '../services/storage'; import { t } from '../services/i18n'; interface PlansProps { userId: string; onStartPlan: (plan: WorkoutPlan) => void; lang: Language; } const Plans: React.FC = ({ userId, onStartPlan, lang }) => { const [plans, setPlans] = useState([]); const [isEditing, setIsEditing] = useState(false); const [editId, setEditId] = useState(null); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [steps, setSteps] = useState([]); const [availableExercises, setAvailableExercises] = useState([]); const [showExerciseSelector, setShowExerciseSelector] = useState(false); useEffect(() => { const loadData = async () => { const fetchedPlans = await getPlans(userId); setPlans(fetchedPlans); const fetchedExercises = await getExercises(userId); // Filter out archived exercises if (Array.isArray(fetchedExercises)) { setAvailableExercises(fetchedExercises.filter(e => !e.isArchived)); } else { setAvailableExercises([]); } }; loadData(); }, [userId]); const handleCreateNew = () => { setEditId(crypto.randomUUID()); setName(''); setDescription(''); setSteps([]); setIsEditing(true); }; const handleSave = () => { if (!name.trim() || !editId) return; const newPlan: WorkoutPlan = { id: editId, name, description, steps }; savePlan(userId, newPlan); setPlans(getPlans(userId)); setIsEditing(false); }; const handleDelete = (id: string, e: React.MouseEvent) => { e.stopPropagation(); if (confirm(t('delete_confirm', lang))) { deletePlan(userId, id); setPlans(getPlans(userId)); } }; const addStep = (ex: ExerciseDef) => { const newStep: PlannedSet = { id: crypto.randomUUID(), exerciseId: ex.id, exerciseName: ex.name, exerciseType: ex.type, isWeighted: false }; setSteps([...steps, newStep]); setShowExerciseSelector(false); }; const toggleWeighted = (stepId: string) => { setSteps(steps.map(s => s.id === stepId ? { ...s, isWeighted: !s.isWeighted } : s)); }; const removeStep = (stepId: string) => { setSteps(steps.filter(s => s.id !== stepId)); }; const moveStep = (index: number, direction: 'up' | 'down') => { if (direction === 'up' && index === 0) return; if (direction === 'down' && index === steps.length - 1) return; const newSteps = [...steps]; const targetIndex = direction === 'up' ? index - 1 : index + 1; [newSteps[index], newSteps[targetIndex]] = [newSteps[targetIndex], newSteps[index]]; setSteps(newSteps); }; if (isEditing) { return (

{t('plan_editor', lang)}

setName(e.target.value)} />