143 lines
5.0 KiB
Dart
143 lines
5.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
import '../models/person_model.dart';
|
|
|
|
class StatsController {
|
|
final SupabaseClient _supabase = Supabase.instance.client;
|
|
|
|
// --- 1. LER DADOS (STREAM) ---
|
|
Stream<List<Person>> getMembers(String teamId) {
|
|
return _supabase
|
|
.from('members')
|
|
.stream(primaryKey: ['id'])
|
|
.eq('team_id', teamId)
|
|
.order('name', ascending: true) // Ordena por nome
|
|
.map((data) => data.map((json) => Person.fromMap(json)).toList());
|
|
}
|
|
|
|
// --- 2. AÇÕES DE BASE DE DADOS ---
|
|
|
|
// Adicionar
|
|
Future<void> _addPersonToSupabase(String teamId, String name, String type, String number) async {
|
|
await _supabase.from('members').insert({
|
|
'team_id': teamId,
|
|
'name': name,
|
|
'type': type,
|
|
'number': number,
|
|
});
|
|
}
|
|
|
|
// Editar
|
|
Future<void> _updatePersonInSupabase(String personId, String name, String type, String number) async {
|
|
await _supabase.from('members').update({
|
|
'name': name,
|
|
'type': type,
|
|
'number': number,
|
|
}).eq('id', personId);
|
|
}
|
|
|
|
// Apagar
|
|
Future<void> deletePerson(String teamId, String personId) async {
|
|
try {
|
|
await _supabase.from('members').delete().eq('id', personId);
|
|
} catch (e) {
|
|
debugPrint("Erro ao apagar: $e");
|
|
}
|
|
}
|
|
|
|
// --- 3. DIÁLOGOS (UI) ---
|
|
|
|
// Mostrar Diálogo de Adicionar
|
|
void showAddPersonDialog(BuildContext context, String teamId) {
|
|
_showPersonDialog(context, teamId: teamId);
|
|
}
|
|
|
|
// Mostrar Diálogo de Editar
|
|
void showEditPersonDialog(BuildContext context, String teamId, Person person) {
|
|
_showPersonDialog(context, teamId: teamId, person: person);
|
|
}
|
|
|
|
// Função Genérica para o Diálogo (Serve para criar e editar)
|
|
void _showPersonDialog(BuildContext context, {required String teamId, Person? person}) {
|
|
final isEditing = person != null;
|
|
final nameController = TextEditingController(text: person?.name ?? '');
|
|
final numberController = TextEditingController(text: person?.number ?? '');
|
|
|
|
// Valor inicial do dropdown ('Jogador' por defeito)
|
|
String selectedType = person?.type ?? 'Jogador';
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
// Usamos StatefulBuilder para atualizar o Dropdown dentro do Dialog
|
|
return StatefulBuilder(
|
|
builder: (context, setState) {
|
|
return AlertDialog(
|
|
title: Text(isEditing ? 'Editar Membro' : 'Novo Membro'),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
// Nome
|
|
TextField(
|
|
controller: nameController,
|
|
decoration: const InputDecoration(labelText: 'Nome'),
|
|
textCapitalization: TextCapitalization.sentences,
|
|
),
|
|
const SizedBox(height: 10),
|
|
|
|
// Tipo (Jogador/Treinador)
|
|
DropdownButtonFormField<String>(
|
|
value: selectedType,
|
|
decoration: const InputDecoration(labelText: 'Função'),
|
|
items: const [
|
|
DropdownMenuItem(value: 'Jogador', child: Text('Jogador')),
|
|
DropdownMenuItem(value: 'Treinador', child: Text('Treinador')),
|
|
],
|
|
onChanged: (value) {
|
|
if (value != null) {
|
|
setState(() => selectedType = value);
|
|
}
|
|
},
|
|
),
|
|
const SizedBox(height: 10),
|
|
|
|
// Número (Só aparece se for Jogador)
|
|
if (selectedType == 'Jogador')
|
|
TextField(
|
|
controller: numberController,
|
|
decoration: const InputDecoration(labelText: 'Número da Camisola'),
|
|
keyboardType: TextInputType.number,
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('Cancelar'),
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF00C853)),
|
|
onPressed: () async {
|
|
if (nameController.text.isEmpty) return;
|
|
|
|
final name = nameController.text.trim();
|
|
final number = numberController.text.trim();
|
|
|
|
if (isEditing) {
|
|
await _updatePersonInSupabase(person!.id, name, selectedType, number);
|
|
} else {
|
|
await _addPersonToSupabase(teamId, name, selectedType, number);
|
|
}
|
|
|
|
if (context.mounted) Navigator.pop(context);
|
|
},
|
|
child: Text(isEditing ? 'Guardar' : 'Adicionar', style: const TextStyle(color: Colors.white)),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
} |