import { useState, useEffect } from 'react' import { Plus, Pencil, Trash2, Save, X, Eye, EyeOff } from 'lucide-react' import { api } from '../utils/api' import EmojiIconPicker from '../components/EmojiIconPicker' const CATEGORIES = [ { value: 'body_composition', label: 'Körperzusammensetzung' }, { value: 'training', label: 'Training' }, { value: 'endurance', label: 'Ausdauer' }, { value: 'coordination', label: 'Koordination' }, { value: 'mental', label: 'Mental' }, { value: 'recovery', label: 'Erholung' }, { value: 'health', label: 'Gesundheit' }, { value: 'custom', label: 'Eigene' } ] export default function AdminFocusAreasPage() { const [data, setData] = useState({ areas: [], grouped: {}, total: 0 }) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) const [showInactive, setShowInactive] = useState(false) const [editingId, setEditingId] = useState(null) const [creating, setCreating] = useState(false) const [formData, setFormData] = useState({ key: '', name_de: '', name_en: '', icon: '', description: '', category: 'custom' }) useEffect(() => { loadData() }, [showInactive]) const loadData = async () => { try { setLoading(true) const result = await api.listFocusAreaDefinitions(showInactive) setData(result) setError(null) } catch (err) { console.error('Failed to load focus areas:', err) setError(err.message) } finally { setLoading(false) } } const handleCreate = async () => { if (!formData.key || !formData.name_de) { setError('Key und Name (DE) sind erforderlich') return } try { await api.createFocusAreaDefinition(formData) setCreating(false) setFormData({ key: '', name_de: '', name_en: '', icon: '', description: '', category: 'custom' }) await loadData() } catch (err) { setError(err.message) } } const handleUpdate = async (id) => { try { const area = data.areas.find(a => a.id === id) await api.updateFocusAreaDefinition(id, { name_de: area.name_de, name_en: area.name_en, icon: area.icon, description: area.description, category: area.category, is_active: area.is_active }) setEditingId(null) await loadData() } catch (err) { setError(err.message) } } const handleDelete = async (id) => { if (!confirm('Focus Area wirklich löschen?')) return try { await api.deleteFocusAreaDefinition(id) await loadData() } catch (err) { setError(err.message) } } const handleToggleActive = async (id) => { const area = data.areas.find(a => a.id === id) try { await api.updateFocusAreaDefinition(id, { is_active: !area.is_active }) await loadData() } catch (err) { setError(err.message) } } const updateField = (id, field, value) => { setData(prev => ({ ...prev, areas: prev.areas.map(a => a.id === id ? { ...a, [field]: value } : a ) })) } if (loading) { return (
{area.key}