import 'package:flutter/material.dart'; import 'package:playmaker/screens/team_stats_page.dart'; import '../controllers/team_controller.dart'; import '../models/team_model.dart'; import '../widgets/team_widgets.dart'; import 'dart:math' as math; // <-- IMPORTANTE: Adicionar para o cálculo class TeamsPage extends StatefulWidget { const TeamsPage({super.key}); @override State createState() => _TeamsPageState(); } class _TeamsPageState extends State { final TeamController controller = TeamController(); final TextEditingController _searchController = TextEditingController(); String _selectedSeason = 'Todas'; String _currentSort = 'Recentes'; String _searchQuery = ''; @override void dispose() { _searchController.dispose(); super.dispose(); } // --- POPUP DE FILTROS --- void _showFilterDialog(BuildContext context, double sf) { showDialog( context: context, builder: (context) { return StatefulBuilder( builder: (context, setModalState) { return AlertDialog( backgroundColor: const Color(0xFF2C3E50), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20 * sf)), title: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "Filtros de pesquisa", style: TextStyle(color: Colors.white, fontSize: 18 * sf, fontWeight: FontWeight.bold), ), IconButton( icon: Icon(Icons.close, color: Colors.white, size: 20 * sf), onPressed: () => Navigator.pop(context), ) ], ), content: Column( mainAxisSize: MainAxisSize.min, children: [ const Divider(color: Colors.white24), SizedBox(height: 16 * sf), Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Coluna Temporada Expanded( child: _buildPopupColumn( title: "TEMPORADA", options: ['Todas', '2023/24', '2024/25', '2025/26'], currentValue: _selectedSeason, sf: sf, onSelect: (val) { setState(() => _selectedSeason = val); setModalState(() {}); }, ), ), SizedBox(width: 20 * sf), // Coluna Ordenar Expanded( child: _buildPopupColumn( title: "ORDENAR POR", options: ['Recentes', 'Nome', 'Tamanho'], currentValue: _currentSort, sf: sf, onSelect: (val) { setState(() => _currentSort = val); setModalState(() {}); }, ), ), ], ), ], ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: Text("CONCLUÍDO", style: TextStyle(color: const Color(0xFFE74C3C), fontWeight: FontWeight.bold, fontSize: 14 * sf)), ), ], ); }, ); }, ); } Widget _buildPopupColumn({ required String title, required List options, required String currentValue, required double sf, required Function(String) onSelect, }) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(title, style: TextStyle(color: Colors.grey, fontSize: 11 * sf, fontWeight: FontWeight.bold)), SizedBox(height: 12 * sf), ...options.map((opt) { final isSelected = currentValue == opt; return InkWell( onTap: () => onSelect(opt), child: Padding( padding: EdgeInsets.symmetric(vertical: 8.0 * sf), child: Text( opt, style: TextStyle( color: isSelected ? const Color(0xFFE74C3C) : Colors.white70, fontWeight: isSelected ? FontWeight.bold : FontWeight.normal, fontSize: 14 * sf, ), ), ), ); }).toList(), ], ); } @override Widget build(BuildContext context) { // 👇 CÁLCULO DA ESCALA (sf) PARA SE ADAPTAR A QUALQUER ECRÃ 👇 final double wScreen = MediaQuery.of(context).size.width; final double hScreen = MediaQuery.of(context).size.height; final double sf = math.min(wScreen, hScreen) / 400; return Scaffold( backgroundColor: const Color(0xFFF5F7FA), appBar: AppBar( title: Text("Minhas Equipas", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20 * sf)), backgroundColor: const Color(0xFFF5F7FA), elevation: 0, actions: [ IconButton( icon: Icon(Icons.filter_list, color: const Color(0xFFE74C3C), size: 24 * sf), onPressed: () => _showFilterDialog(context, sf), ), ], ), body: Column( children: [ _buildSearchBar(sf), Expanded(child: _buildTeamsList(sf)), ], ), floatingActionButton: FloatingActionButton( backgroundColor: const Color(0xFFE74C3C), child: Icon(Icons.add, color: Colors.white, size: 24 * sf), onPressed: () => _showCreateDialog(context, sf), ), ); } Widget _buildSearchBar(double sf) { return Padding( padding: EdgeInsets.all(16.0 * sf), child: TextField( controller: _searchController, onChanged: (v) => setState(() => _searchQuery = v.toLowerCase()), style: TextStyle(fontSize: 16 * sf), decoration: InputDecoration( hintText: 'Pesquisar equipa...', hintStyle: TextStyle(fontSize: 16 * sf), prefixIcon: Icon(Icons.search, color: const Color(0xFFE74C3C), size: 22 * sf), filled: true, fillColor: Colors.white, border: OutlineInputBorder(borderRadius: BorderRadius.circular(15 * sf), borderSide: BorderSide.none), ), ), ); } Widget _buildTeamsList(double sf) { return StreamBuilder>>( stream: controller.teamsStream, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const Center(child: CircularProgressIndicator()); } if (!snapshot.hasData || snapshot.data!.isEmpty) { return Center(child: Text("Nenhuma equipa encontrada.", style: TextStyle(fontSize: 16 * sf))); } var data = List>.from(snapshot.data!); // --- 1. FILTROS --- if (_selectedSeason != 'Todas') { data = data.where((t) => t['season'] == _selectedSeason).toList(); } if (_searchQuery.isNotEmpty) { data = data.where((t) => t['name'].toString().toLowerCase().contains(_searchQuery)).toList(); } // --- 2. ORDENAÇÃO --- data.sort((a, b) { bool favA = a['is_favorite'] ?? false; bool favB = b['is_favorite'] ?? false; if (favA && !favB) return -1; if (!favA && favB) return 1; if (_currentSort == 'Nome') { return a['name'].toString().compareTo(b['name'].toString()); } else { return (b['created_at'] ?? '').toString().compareTo((a['created_at'] ?? '').toString()); } }); return ListView.builder( padding: EdgeInsets.symmetric(horizontal: 16 * sf), itemCount: data.length, itemBuilder: (context, index) { final team = Team.fromMap(data[index]); return GestureDetector( onTap: () { Navigator.push( context, MaterialPageRoute(builder: (context) => TeamStatsPage(team: team)), ); }, child: TeamCard( team: team, controller: controller, sf: sf, // Passar a escala para o Card onFavoriteTap: () => controller.toggleFavorite(team.id, team.isFavorite), ), ); }, ); }, ); } void _showCreateDialog(BuildContext context, double sf) { showDialog( context: context, builder: (context) => CreateTeamDialog( sf: sf, onConfirm: (name, season, imageUrl) => controller.createTeam(name, season, imageUrl), ), ); } }