158 lines
5.5 KiB
Dart
158 lines
5.5 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
|
|
Stream<List<Person>> getMembers(String teamId) {
|
|
return _supabase
|
|
.from('members')
|
|
.stream(primaryKey: ['id'])
|
|
.eq('team_id', teamId)
|
|
.order('name', ascending: true)
|
|
.map((data) => data.map((json) => Person.fromMap(json)).toList());
|
|
}
|
|
|
|
// 2. APAGAR
|
|
Future<void> deletePerson(String personId) async {
|
|
try {
|
|
await _supabase.from('members').delete().eq('id', personId);
|
|
} catch (e) {
|
|
debugPrint("Erro ao eliminar: $e");
|
|
}
|
|
}
|
|
|
|
// 3. DIÁLOGOS
|
|
void showAddPersonDialog(BuildContext context, String teamId) {
|
|
_showForm(context, teamId: teamId);
|
|
}
|
|
|
|
void showEditPersonDialog(BuildContext context, String teamId, Person person) {
|
|
_showForm(context, teamId: teamId, person: person);
|
|
}
|
|
|
|
// --- O POPUP ESTÁ AQUI ---
|
|
void _showForm(BuildContext context, {required String teamId, Person? person}) {
|
|
final isEdit = person != null;
|
|
final nameCtrl = TextEditingController(text: person?.name ?? '');
|
|
final numCtrl = TextEditingController(text: person?.number ?? '');
|
|
|
|
// Define o valor inicial
|
|
String selectedType = person?.type ?? 'Jogador';
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => StatefulBuilder(
|
|
builder: (ctx, setState) => AlertDialog(
|
|
title: Text(isEdit ? "Editar" : "Adicionar"),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
// NOME
|
|
TextField(
|
|
controller: nameCtrl,
|
|
decoration: const InputDecoration(labelText: "Nome"),
|
|
textCapitalization: TextCapitalization.sentences,
|
|
),
|
|
const SizedBox(height: 10),
|
|
|
|
// FUNÇÃO
|
|
DropdownButtonFormField<String>(
|
|
value: selectedType,
|
|
decoration: const InputDecoration(labelText: "Função"),
|
|
items: ["Jogador", "Treinador"]
|
|
.map((e) => DropdownMenuItem(value: e, child: Text(e)))
|
|
.toList(),
|
|
onChanged: (v) {
|
|
if (v != null) setState(() => selectedType = v);
|
|
},
|
|
),
|
|
|
|
// NÚMERO (Só aparece se for Jogador)
|
|
if (selectedType == "Jogador") ...[
|
|
const SizedBox(height: 10),
|
|
TextField(
|
|
controller: numCtrl,
|
|
decoration: const InputDecoration(labelText: "Número da Camisola"),
|
|
keyboardType: TextInputType.text, // Aceita texto para evitar erros
|
|
),
|
|
],
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text("Cancelar")
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF00C853)),
|
|
onPressed: () async {
|
|
print("--- 1. CLICOU EM GUARDAR ---");
|
|
|
|
// Validação Simples
|
|
if (nameCtrl.text.trim().isEmpty) {
|
|
print("ERRO: Nome vazio");
|
|
return;
|
|
}
|
|
|
|
// Lógica do Número:
|
|
// Se for Treinador -> envia NULL
|
|
// Se for Jogador e estiver vazio -> envia NULL
|
|
// Se tiver texto -> envia o Texto
|
|
String? numeroFinal;
|
|
if (selectedType == "Treinador") {
|
|
numeroFinal = null;
|
|
} else {
|
|
numeroFinal = numCtrl.text.trim().isEmpty ? null : numCtrl.text.trim();
|
|
}
|
|
|
|
print("--- 2. DADOS A ENVIAR ---");
|
|
print("Nome: ${nameCtrl.text}");
|
|
print("Tipo: $selectedType");
|
|
print("Número: $numeroFinal");
|
|
|
|
try {
|
|
if (isEdit) {
|
|
await _supabase.from('members').update({
|
|
'name': nameCtrl.text.trim(),
|
|
'type': selectedType,
|
|
'number': numeroFinal,
|
|
}).eq('id', person!.id);
|
|
} else {
|
|
await _supabase.from('members').insert({
|
|
'team_id': teamId, // Verifica se este teamId é válido!
|
|
'name': nameCtrl.text.trim(),
|
|
'type': selectedType,
|
|
'number': numeroFinal,
|
|
});
|
|
}
|
|
|
|
print("--- 3. SUCESSO! FECHANDO DIÁLOGO ---");
|
|
if (ctx.mounted) Navigator.pop(ctx);
|
|
|
|
} catch (e) {
|
|
print("--- X. ERRO AO GUARDAR ---");
|
|
print(e.toString());
|
|
|
|
// MOSTRA O ERRO NO TELEMÓVEL
|
|
if (ctx.mounted) {
|
|
ScaffoldMessenger.of(ctx).showSnackBar(
|
|
SnackBar(
|
|
content: Text("Erro: $e"),
|
|
backgroundColor: Colors.red,
|
|
duration: const Duration(seconds: 4),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
},
|
|
child: const Text("Guardar", style: TextStyle(color: Colors.white)),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |