// lib/screens/group_manager_screen.dart import 'package:flutter/material.dart'; import '../services/api_service.dart'; import '../models/group.dart'; class GroupManagerScreen extends StatefulWidget { const GroupManagerScreen({super.key}); @override State createState() => _GroupManagerScreenState(); } class _GroupManagerScreenState extends State { List _groups = []; bool _isLoading = true; final _newGroupController = TextEditingController(); @override void initState() { super.initState(); _loadGroups(); } Future _loadGroups() async { setState(() => _isLoading = true); try { final apiService = ApiService(); final groups = await apiService.getGroups(); setState(() { _groups = groups; _isLoading = false; }); } catch (e) { setState(() => _isLoading = false); _showError('Gruppen konnten nicht geladen werden'); } } Future _addGroup() async { if (_newGroupController.text.trim().isEmpty) { _showError('Bitte geben Sie einen Namen ein'); return; } try { final apiService = ApiService(); final success = await apiService.createGroup(_newGroupController.text.trim()); if (success) { _newGroupController.clear(); await _loadGroups(); } else { _showError('Erstellen fehlgeschlagen'); } } catch (e) { _showError('Fehler: $e'); } } Future _deleteGroup(Group group) async { final confirm = await showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Gruppe löschen'), content: Text('Möchten Sie "${group.name}" wirklich löschen?'), actions: [ TextButton(onPressed: () => Navigator.pop(context, false), child: const Text('Abbrechen')), TextButton(onPressed: () => Navigator.pop(context, true), child: const Text('Löschen')), ], ), ); if (confirm == true) { try { final apiService = ApiService(); final success = await apiService.deleteGroup(group.gruppeID); if (success) { await _loadGroups(); } else { _showError('Löschen fehlgeschlagen'); } } catch (e) { _showError('Fehler: $e'); } } } void _showError(String message) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(message), backgroundColor: Colors.red), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Gruppenverwaltung'), actions: [ IconButton( icon: const Icon(Icons.refresh), onPressed: _loadGroups, ), ], ), body: Column( children: [ Padding( padding: const EdgeInsets.all(16), child: Row( children: [ Expanded( child: TextField( controller: _newGroupController, decoration: const InputDecoration( hintText: 'Neue Gruppe...', border: OutlineInputBorder(), ), ), ), const SizedBox(width: 8), ElevatedButton( onPressed: _addGroup, child: const Text('Hinzufügen'), ), ], ), ), Expanded( child: _isLoading ? const Center(child: CircularProgressIndicator()) : _groups.isEmpty ? const Center(child: Text('Keine Gruppen vorhanden')) : ListView.builder( itemCount: _groups.length, itemBuilder: (context, index) { final group = _groups[index]; return Card( margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4), child: ListTile( title: Text(group.name), trailing: IconButton( icon: const Icon(Icons.delete, color: Colors.red), onPressed: () => _deleteGroup(group), ), ), ); }, ), ), ], ), ); } }