import { useState, useEffect } from 'react' import { api } from '../utils/api' import '../app.css' export default function AdminTrainingProfiles() { const [stats, setStats] = useState(null) const [trainingTypes, setTrainingTypes] = useState([]) const [templates, setTemplates] = useState([]) const [selectedType, setSelectedType] = useState(null) const [editingProfile, setEditingProfile] = useState(null) const [profileJson, setProfileJson] = useState('') const [loading, setLoading] = useState(true) const [error, setError] = useState('') const [success, setSuccess] = useState('') useEffect(() => { load() }, []) const load = async () => { try { setLoading(true) const [typesData, statsData, templatesData] = await Promise.all([ api.adminListTrainingTypes(), api.getProfileStats(), api.getProfileTemplates() ]) setTrainingTypes(typesData) setStats(statsData) setTemplates(templatesData) } catch (e) { setError(e.message) } finally { setLoading(false) } } const openEditor = (type) => { setSelectedType(type) setEditingProfile(type.profile || null) setProfileJson(JSON.stringify(type.profile || {}, null, 2)) setError('') setSuccess('') } const closeEditor = () => { setSelectedType(null) setEditingProfile(null) setProfileJson('') } const saveProfile = async () => { try { // Validate JSON const profile = JSON.parse(profileJson) // Update training type await api.adminUpdateTrainingType(selectedType.id, { profile }) setSuccess(`Profil für "${selectedType.name_de}" gespeichert`) closeEditor() load() } catch (e) { setError(e.message || 'Ungültiges JSON') } } const applyTemplate = async (typeId, templateKey) => { if (!confirm(`Template "${templateKey}" auf diesen Trainingstyp anwenden?`)) return try { await api.applyProfileTemplate(typeId, templateKey) setSuccess('Template erfolgreich angewendet') load() } catch (e) { setError(e.message) } } const batchReEvaluate = async () => { if (!confirm('Alle Aktivitäten neu evaluieren? Das kann einige Sekunden dauern.')) return try { const result = await api.batchEvaluateActivities() let message = `Batch-Evaluation abgeschlossen: ${result.stats.evaluated} evaluiert, ` + `${result.stats.skipped} übersprungen, ${result.stats.errors} Fehler` // Show error details if available if (result.stats.error_details && result.stats.error_details.length > 0) { message += '\n\nErste Fehler:\n' + result.stats.error_details.map(err => `- Aktivität ${err.activity_id} (Typ: ${err.training_type_id || 'keine'}): ${err.error}` ).join('\n') } if (result.stats.errors > 0) { setError(message) } else { setSuccess(message) } } catch (e) { setError(e.message) } } if (loading) return
return (Konfiguriere Bewertungsprofile für Trainingstypen
{error && (JSON-basierter Editor. Siehe Dokumentation für vollständige Struktur.