import React, { useState, useEffect } from 'react' import api from '../utils/api' import { useAuth } from '../context/AuthContext' function ClubsPage() { const { user } = useAuth() const [activeTab, setActiveTab] = useState('clubs') const [clubs, setClubs] = useState([]) const [divisions, setDivisions] = useState([]) const [groups, setGroups] = useState([]) const [loading, setLoading] = useState(true) const [showModal, setShowModal] = useState(false) const [editing, setEditing] = useState(null) const [modalType, setModalType] = useState('club') // Form state const [formData, setFormData] = useState({}) const isAdmin = user?.role === 'admin' || user?.role === 'superadmin' const isTrainer = user?.role === 'trainer' || isAdmin const canCreateClub = ['admin', 'superadmin', 'trainer', 'user'].includes(user?.role) useEffect(() => { loadData() }, []) const loadData = async () => { try { const [clubsData, divisionsData, groupsData] = await Promise.all([ api.listClubs(), api.listDivisions(), api.listTrainingGroups() ]) setClubs(clubsData) setDivisions(divisionsData) setGroups(groupsData) } catch (err) { console.error('Failed to load data:', err) alert('Fehler beim Laden: ' + err.message) } finally { setLoading(false) } } const handleCreate = (type) => { setEditing(null) setModalType(type) if (type === 'club') { setFormData({ name: '', abbreviation: '', description: '', status: 'active' }) } else if (type === 'division') { setFormData({ club_id: '', name: '', focus_area: '' }) } else if (type === 'group') { setFormData({ club_id: '', division_id: '', name: '', focus: '', level: '', age_group: '', weekday: '', time_start: '', time_end: '', location: '', trainer_id: user?.id ?? '', co_trainer_ids: [], status: 'active' }) } setShowModal(true) } const handleEdit = (item, type) => { setEditing(item) setModalType(type) setFormData({ ...item }) setShowModal(true) } const handleDelete = async (item, type) => { const confirmMsg = { club: `Verein "${item.name}" wirklich löschen? Alle Sparten und Gruppen werden auch gelöscht!`, division: `Sparte "${item.name}" wirklich löschen?`, group: `Trainingsgruppe "${item.name}" wirklich löschen?` } if (!confirm(confirmMsg[type])) return try { if (type === 'club') await api.deleteClub(item.id) else if (type === 'division') await api.deleteDivision(item.id) else if (type === 'group') await api.deleteTrainingGroup(item.id) await loadData() } catch (err) { alert('Fehler beim Löschen: ' + err.message) } } const handleSubmit = async (e) => { e.preventDefault() try { if (modalType === 'club') { if (editing) { await api.updateClub(editing.id, formData) } else { await api.createClub(formData) } } else if (modalType === 'division') { if (editing) { await api.updateDivision(editing.id, formData) } else { await api.createDivision(formData) } } else if (modalType === 'group') { if (editing) { await api.updateTrainingGroup(editing.id, formData) } else { await api.createTrainingGroup(formData) } } setShowModal(false) await loadData() } catch (err) { alert('Fehler beim Speichern: ' + err.message) } } const updateFormField = (field, value) => { setFormData(prev => ({ ...prev, [field]: value })) } if (loading) { return (

Laden...

) } return (

Vereinsverwaltung

Für die Trainingsplanung wird mindestens ein Verein und eine Trainingsgruppe gebraucht. Sparten sind optional — typische Eckdaten einer Gruppe (Wochentag, Zeit, Ort) kannst du schrittweise eintragen.

{/* Tabs */}
{['clubs', 'divisions', 'groups'].map(tab => ( ))}
{/* Clubs Tab */} {activeTab === 'clubs' && ( <>

Vereine

{canCreateClub && ( )}
{clubs.length === 0 ? (

Noch kein Verein angelegt. {canCreateClub ? ' Nutze „+ Neuer Verein“ — ein Name reicht zum Start.' : ' Bitte einen Administrator oder Support um Anlage.'}

{canCreateClub && (

Danach im Tab Trainingsgruppen eine Gruppe diesem Verein zuordnen; Details sind optional.

)}
) : (
{clubs.map(club => (

{club.name} {club.abbreviation && ( ({club.abbreviation}) )}

{club.description && (

{club.description}

)} {club.status}
{isAdmin && (
)}
))}
)} )} {/* Divisions Tab */} {activeTab === 'divisions' && ( <>

Sparten

{isAdmin && ( )}
{divisions.length === 0 ? (

Keine Sparten gefunden

) : (
{divisions.map(division => (

{division.name}

Verein: {division.club_name}

{division.focus_area && ( {division.focus_area} )}
{isAdmin && (
)}
))}
)} )} {/* Training Groups Tab */} {activeTab === 'groups' && ( <>

Trainingsgruppen

{canCreateClub && ( )}
{groups.length === 0 ? (

Keine Trainingsgruppen gefunden

) : (
{groups.map(group => (

{group.name}

{group.club_name} {group.division_name && ` · ${group.division_name}`}

{group.weekday && group.time_start && (
📅 {group.weekday}, {group.time_start.slice(0,5)} - {group.time_end?.slice(0,5)}
)} {group.location &&
📍 {group.location}
} {group.trainer_name &&
👤 {group.trainer_name}
} {group.level &&
⭐ {group.level}
} {group.age_group &&
👶 {group.age_group}
}
{(isAdmin || group.trainer_id === user?.id) && (
{isAdmin && ( )}
)}
))}
)} )} {/* Modal */} {showModal && (

{editing ? (modalType === 'club' ? 'Verein bearbeiten' : modalType === 'division' ? 'Sparte bearbeiten' : 'Gruppe bearbeiten') : (modalType === 'club' ? 'Neuer Verein' : modalType === 'division' ? 'Neue Sparte' : 'Neue Gruppe') }

{/* Club Form */} {modalType === 'club' && ( <>
updateFormField('name', e.target.value)} required />
updateFormField('abbreviation', e.target.value)} />