import 'package:flutter/material.dart'; import 'package:insparkspokalen_ui/services/teamService.dart'; import 'package:insparkspokalen_ui/models/teamModel.dart'; class CreateTeamsButton extends StatefulWidget { final TextEditingController controller; final Function(TeamModel) onTeamCreated; const CreateTeamsButton({ super.key, required this.controller, required this.onTeamCreated, }); @override CreateTeamsButtonState createState() => CreateTeamsButtonState(); } class CreateTeamsButtonState extends State { final TeamService groupService = TeamService(); @override Widget build(BuildContext context) { return AlertDialog( backgroundColor: const Color(0xFF3A3A3A), title: const Text( 'Skapa nytt lag', style: TextStyle(color: Colors.white70), ), content: Column( mainAxisSize: MainAxisSize.min, children: [ TextField( controller: widget.controller, style: const TextStyle(color: Colors.white), decoration: const InputDecoration( hintText: 'Lagnamn', hintStyle: TextStyle(color: Colors.white54), ), ), ], ), actions: [ TextButton( onPressed: () { Navigator.of(context).pop(); }, child: const Text('Avbryt', style: TextStyle(color: Colors.white70)), ), TextButton( onPressed: () async { String teamName = widget.controller.text.trim(); if (teamName.isNotEmpty) { final success = await groupService.createTeam(teamName); if (success) { // Hämta den uppdaterade listan från servern final updatedGroups = await groupService.showTeams(); // Försök hitta laget som nyss lades till final newGroup = updatedGroups.firstWhere( (group) => group.name == teamName, orElse: () => TeamModel(-1, teamName, 0), // fallback om ej hittat ); widget.onTeamCreated(newGroup); Navigator.of(context).pop(); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text("Lag '$teamName' skapades!")), ); } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text("Det gick inte att skapa laget"), ), ); } } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text("Vänligen ange ett lagnamn")), ); } }, child: const Text('Skapa', style: TextStyle(color: Colors.amber)), ), ], ); } }