import 'package:flutter/material.dart'; import 'package:insparkspokalen_ui/models/teamModel.dart'; import 'package:insparkspokalen_ui/models/userModel.dart'; import 'package:insparkspokalen_ui/services/teamUserSyncService.dart'; import 'createTeamsButton.dart'; import 'package:insparkspokalen_ui/services/backend/teamService.dart'; import 'package:provider/provider.dart'; import 'package:flutter_svg/svg.dart'; import 'package:insparkspokalen_ui/teams/teamImage.dart'; /// Sida för administratörer att hantera lag class TeamPageAdmin extends StatefulWidget { const TeamPageAdmin({super.key}); @override State createState() => _TeamPageAdminState(); } class _TeamPageAdminState extends State { late TeamService teamService; late TeamUserSyncService syncService; List groups = []; final TextEditingController _controller = TextEditingController(); @override void initState() { teamService = Provider.of(context, listen: false); syncService = Provider.of(context, listen: false); super.initState(); _loadGroups(); } Future _loadGroups() async { final result = await teamService.showTeams(); setState(() { groups = result; }); } void _removeTeamCard(int teamId, String teamName) async { setState(() { groups.removeWhere((g) => g.teamId == teamId); }); ScaffoldMessenger.of( context, ).showSnackBar(SnackBar(content: Text('Lag "$teamName" borttaget.'))); } /// Visar dialogruta för att skapa nytt lag void _openDialog() { showDialog( context: context, builder: (_) => CreateTeamsButton( controller: _controller, onTeamCreated: (newTeam) { setState(() { groups.add(newTeam); }); }, ), ); } /// Visar dialogruta för att bekräfta radering void _showDeleteDialog(int teamId, String teamName) { showDialog( context: context, builder: (context) { return AlertDialog( title: const Text('Bekräfta radering'), content: Text('Är du säker på att du vill radera "$teamName"?'), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text('Nej'), ), TextButton( onPressed: () async { Navigator.of(context).pop(); if(await syncService.handleTeamRemoved(teamId)){ _removeTeamCard(teamId, teamName); } }, child: const Text('Ja'), ), ], ); }, ); } //info om lagets medlemmar void _showMemberDialog(String groupName, int teamId) { showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Text("Medlemmar i $groupName"), content: FutureBuilder>( future: TeamService().getTeamMembers(teamId), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const Center(child: CircularProgressIndicator()); } else if (snapshot.hasError) { return const Text("Fel vid hämtning av medlemmar."); } else if (!snapshot.hasData || snapshot.data!.isEmpty) { return const Text("Inga medlemmar hittades."); } final members = snapshot.data!; return SizedBox( width: 300, child: ListView.builder( shrinkWrap: true, itemCount: members.length, itemBuilder: (context, index) { final user = members[index]; return ListTile( leading: CircleAvatar( backgroundImage: user.profilePicture != null ? NetworkImage(user.profilePicture!) : null, child: user.profilePicture == null ? const Icon(Icons.person) : null, ), title: Text(user.name), subtitle: Text(user.email), trailing: IconButton( icon: const Icon( Icons.remove_circle, color: Colors.red, ), onPressed: () { showDialog( context: context, builder: (context) => AlertDialog( title: const Text("Bekräfta borttagning"), content: Text( "Vill du verkligen ta bort ${user.name} från laget?", ), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text("Nej"), ), TextButton( onPressed: () async { Navigator.of( context, ).pop(); // Stäng dialog // final success = await groupService // .removeUserFromTeam( // user.email, // teamId, // ); print("DEBUG: Syncing"); final successSync = await syncService.handleUserKicked(user.email); if (successSync) { Navigator.of( context, ).pop(); // Stäng medlemsdialog _showMemberDialog( groupName, teamId, ); // Öppna på nytt } }, child: const Text("Ja"), ), ], ), ); }, ), ); }, ), ); }, ), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text("Stäng"), ), ], ); }, ); } /// Bygger UI:t för adminsidan @override Widget build(BuildContext context) { return Scaffold( backgroundColor: const Color(0xFF2E2E2E), body: Column( children: [ const SizedBox(height: 32), const Text( 'Admin - Hantera lag', style: TextStyle(color: Colors.amber, fontSize: 26), ), Expanded( child: ListView.builder( itemCount: groups.length, itemBuilder: (context, index) { final team = groups[index]; return Card( color: Colors.white, margin: const EdgeInsets.symmetric( vertical: 6, horizontal: 12, ), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), child: ListTile( leading: CircleAvatar( radius: 20, backgroundColor: Colors.white, child: TeamImage(imageUrl: team.imageUrl, size: 30), ), title: Text(team.name), subtitle: Align( alignment: Alignment.centerLeft, child: IntrinsicWidth( child: OutlinedButton( onPressed: () => _showMemberDialog(team.name, team.teamId), style: OutlinedButton.styleFrom( padding: const EdgeInsets.symmetric( horizontal: 12, vertical: 6, ), side: const BorderSide(color: Color(0xFF2E2E2E)), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8), ), ), child: const Text( 'Hantera medlemmar', style: TextStyle( fontSize: 10, color: Color(0xFF2E2E2E), ), ), ), ), ), trailing: IconButton( icon: const Icon(Icons.delete, color: Colors.red), onPressed: () => _showDeleteDialog(team.teamId, team.name), ), ), ); }, ), ), ElevatedButton( onPressed: _openDialog, style: ElevatedButton.styleFrom( backgroundColor: Colors.grey, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(40), ), ), child: const Text( 'Skapa nytt lag', style: TextStyle(color: Colors.white), ), ), const SizedBox(height: 16), const Text( 'Endast administratörer', style: TextStyle(color: Colors.white70), ), ], ), ); } }