import 'package:flutter/material.dart'; import '../api.dart'; import '../main.dart'; /// Self-registration as a Gemeinde Verantwortliche/r: enter the KC invite /// code, pick your Gemeinde, send the request. The result is a PENDING /// membership a Leitungsteam member has to approve. class VerantwortlicheRegisterScreen extends StatefulWidget { const VerantwortlicheRegisterScreen({super.key}); @override State createState() => _VerantwortlicheRegisterScreenState(); } class _VerantwortlicheRegisterScreenState extends State { final _code = TextEditingController(); String? _kcName; List<(String id, String name)> _gemeinden = []; String? _selectedGemeinde; bool _busy = false; String? _error; String? _done; Api get _api => AppScope.of(context).api; Future _resolve() async { setState(() { _busy = true; _error = null; _kcName = null; _gemeinden = []; }); try { final res = await _api.resolveInvite(_code.text.trim()); setState(() { _kcName = res['kcName'] as String?; _gemeinden = ((res['gemeinden'] as List?) ?? []) .map((g) => (g['id'] as String, g['name'] as String)) .toList(); }); } catch (e) { setState(() => _error = '$e'); } finally { if (mounted) setState(() => _busy = false); } } Future _submit() async { if (_selectedGemeinde == null) return; setState(() { _busy = true; _error = null; }); try { final res = await _api.registerVerantwortliche(_code.text.trim(), _selectedGemeinde!); setState(() => _done = 'Anfrage gesendet (Status: ${res['status']}). Ein Leitungsteam-Mitglied ' 'muss dich noch freischalten.'); } catch (e) { setState(() => _error = '$e'); } finally { if (mounted) setState(() => _busy = false); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Als Verantwortliche/r registrieren')), body: Center( child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 420), child: ListView( padding: const EdgeInsets.all(24), children: [ if (_done != null) ...[ const Icon(Icons.check_circle, color: Colors.green, size: 48), const SizedBox(height: 12), Text(_done!, textAlign: TextAlign.center), const SizedBox(height: 20), FilledButton( onPressed: () => AppScope.of(context).logout(), child: const Text('Abmelden'), ), ] else ...[ TextField( controller: _code, decoration: const InputDecoration( labelText: 'KC-Einladungscode', border: OutlineInputBorder(), ), ), const SizedBox(height: 12), OutlinedButton( onPressed: _busy ? null : _resolve, child: const Text('KC suchen'), ), if (_kcName != null) ...[ const SizedBox(height: 20), Text('KC: $_kcName', style: Theme.of(context).textTheme.titleMedium), const SizedBox(height: 8), DropdownButtonFormField( initialValue: _selectedGemeinde, decoration: const InputDecoration( labelText: 'Deine Gemeinde', border: OutlineInputBorder(), ), items: [ for (final g in _gemeinden) DropdownMenuItem(value: g.$1, child: Text(g.$2)), ], onChanged: (v) => setState(() => _selectedGemeinde = v), ), const SizedBox(height: 16), FilledButton( onPressed: (_busy || _selectedGemeinde == null) ? null : _submit, child: const Text('Anfrage senden'), ), ], if (_error != null) ...[ const SizedBox(height: 16), Text(_error!, style: TextStyle(color: Theme.of(context).colorScheme.error)), ], ], ], ), ), ), ); } }