import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:intl/intl.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:uuid/uuid.dart'; import '/core/auth_provider.dart'; import '/models/payment.dart'; import '/models/child.dart'; import '/models/profile.dart'; const _bg = Color(0xFF0D1117); const _card = Color(0xFF161B22); const _blue = Color(0xFF4FC3F7); const _green = Color(0xFF2ECC71); const _amber = Color(0xFFFFB300); const _red = Color(0xFFE74C3C); class PaymentsScreen extends ConsumerStatefulWidget { const PaymentsScreen({super.key}); @override ConsumerState createState() => _State(); } class _State extends ConsumerState { bool _isAdmin = false; @override void initState() { super.initState(); _checkRole(); } Future _checkRole() async { final p = await ref.read(currentProfileProvider.future); if (mounted) setState(() => _isAdmin = p?.role == 'principal' || p?.role == 'admin'); } @override Widget build(BuildContext context) { final sb = Supabase.instance.client; return Scaffold( backgroundColor: _bg, appBar: AppBar( backgroundColor: _card, elevation: 0, title: const Text('Mensalidades', style: TextStyle(color: _blue, fontWeight: FontWeight.bold)), ), body: StreamBuilder>>( // Join com children para ter nomes stream: sb.from('payments').stream(primaryKey: ['id']).order('month', ascending: false), builder: (ctx, snap) { if (snap.hasError) return Center(child: Text('Erro: ${snap.error}', style: const TextStyle(color: _red))); if (!snap.hasData) return const Center(child: CircularProgressIndicator(color: _blue)); final payments = snap.data!.map(Payment.fromMap).toList(); final paid = payments.where((p) => p.status == 'paid').length; final pending = payments.where((p) => p.status == 'pending').length; final overdue = payments.where((p) => p.status == 'overdue').length; final total = payments.where((p) => p.status == 'paid') .fold(0, (sum, p) => sum + p.amount); return Column(children: [ // Resumo Container( color: _card, padding: const EdgeInsets.all(16), child: Column(children: [ Row(children: [ _SummaryTile(label: 'Pagos', count: paid, color: _green), const SizedBox(width: 8), _SummaryTile(label: 'Pendentes', count: pending, color: _amber), const SizedBox(width: 8), _SummaryTile(label: 'Atrasados', count: overdue, color: _red), ]), const SizedBox(height: 10), Container( width: double.infinity, padding: const EdgeInsets.symmetric(vertical: 10), decoration: BoxDecoration(color: _green.withOpacity(0.08), borderRadius: BorderRadius.circular(10), border: Border.all(color: _green.withOpacity(0.2))), child: Center(child: Text( 'Total recebido: Kz ${NumberFormat('#,###.##').format(total)}', style: const TextStyle(color: _green, fontWeight: FontWeight.bold, fontSize: 14))), ), ]), ), // Lista Expanded(child: payments.isEmpty ? Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.payment, size: 60, color: Colors.white.withOpacity(0.08)), const SizedBox(height: 10), const Text('Sem mensalidades registadas', style: TextStyle(color: Color(0xFF888888))), ])) : ListView.builder( padding: const EdgeInsets.all(14), itemCount: payments.length, itemBuilder: (_, i) => _PaymentCard( payment: payments[i], isAdmin: _isAdmin, onStatusChange: _isAdmin ? (p, status) => _updateStatus(p, status) : null, ), )), ]); }, ), floatingActionButton: _isAdmin ? FloatingActionButton.extended( backgroundColor: _blue, icon: const Icon(Icons.add, color: Colors.white), label: const Text('Nova Mensalidade', style: TextStyle(color: Colors.white)), onPressed: () => _showAddDialog(context), ) : null, ); } Future _updateStatus(Payment p, String status) async { final sb = Supabase.instance.client; try { await sb.from('payments').update({'status': status, 'paid_at': status == 'paid' ? DateTime.now().toIso8601String() : null}).eq('id', p.id); } catch (e) { if (mounted) ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Erro: $e'), backgroundColor: _red)); } } void _showAddDialog(BuildContext ctx) { showModalBottomSheet( context: ctx, isScrollControlled: true, backgroundColor: _card, shape: const RoundedRectangleBorder(borderRadius: BorderRadius.vertical(top: Radius.circular(20))), builder: (_) => const _AddPaymentForm()); } } class _PaymentCard extends StatelessWidget { final Payment payment; final bool isAdmin; final Function(Payment, String)? onStatusChange; const _PaymentCard({required this.payment, required this.isAdmin, this.onStatusChange}); Color get _statusColor => switch (payment.status) { 'paid' => _green, 'overdue' => _red, _ => _amber, }; String get _statusLabel => switch (payment.status) { 'paid' => 'Pago ✓', 'overdue' => 'Em Atraso', _ => 'Pendente', }; @override Widget build(BuildContext context) { return FutureBuilder( future: _getChildName(), builder: (ctx, snap) { final childName = snap.data ?? payment.childId.substring(0, 8) + '...'; return Container( margin: const EdgeInsets.only(bottom: 10), padding: const EdgeInsets.all(14), decoration: BoxDecoration( color: _card, borderRadius: BorderRadius.circular(14), border: Border.all(color: _statusColor.withOpacity(0.25)), ), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ Row(children: [ Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration(color: _statusColor.withOpacity(0.1), shape: BoxShape.circle), child: Icon(Icons.receipt_long, color: _statusColor, size: 18), ), const SizedBox(width: 12), Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(childName, style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 14)), Text(DateFormat('MMMM yyyy', 'pt_PT').format(payment.month), style: const TextStyle(color: Color(0xFF888888), fontSize: 12)), ])), Column(crossAxisAlignment: CrossAxisAlignment.end, children: [ Text('Kz ${NumberFormat('#,###').format(payment.amount)}', style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 15)), Container( margin: const EdgeInsets.only(top: 3), padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2), decoration: BoxDecoration(color: _statusColor.withOpacity(0.12), borderRadius: BorderRadius.circular(10)), child: Text(_statusLabel, style: TextStyle(color: _statusColor, fontSize: 11)), ), ]), ]), if (isAdmin && payment.status != 'paid') ...[ const SizedBox(height: 10), Row(children: [ Expanded(child: _ActionBtn( label: '✓ Marcar Pago', color: _green, onTap: () => onStatusChange?.call(payment, 'paid'))), const SizedBox(width: 8), Expanded(child: _ActionBtn( label: '⚠ Marcar Atraso', color: _red, onTap: () => onStatusChange?.call(payment, 'overdue'))), ]), ], ]), ); }, ); } Future _getChildName() async { try { final sb = Supabase.instance.client; final data = await sb.from('children').select('first_name,last_name') .eq('id', payment.childId).maybeSingle(); if (data == null) return 'Criança'; return '${data['first_name']} ${data['last_name']}'; } catch (_) { return 'Criança'; } } } class _ActionBtn extends StatelessWidget { final String label; final Color color; final VoidCallback onTap; const _ActionBtn({required this.label, required this.color, required this.onTap}); @override Widget build(BuildContext context) => GestureDetector( onTap: onTap, child: Container( padding: const EdgeInsets.symmetric(vertical: 8), decoration: BoxDecoration(color: color.withOpacity(0.1), borderRadius: BorderRadius.circular(8), border: Border.all(color: color.withOpacity(0.3))), child: Center(child: Text(label, style: TextStyle(color: color, fontSize: 11, fontWeight: FontWeight.bold))), ), ); } class _SummaryTile extends StatelessWidget { final String label; final int count; final Color color; const _SummaryTile({required this.label, required this.count, required this.color}); @override Widget build(BuildContext context) => Expanded(child: Container( padding: const EdgeInsets.symmetric(vertical: 10), decoration: BoxDecoration(color: color.withOpacity(0.08), borderRadius: BorderRadius.circular(10), border: Border.all(color: color.withOpacity(0.25))), child: Column(children: [ Text('$count', style: TextStyle(color: color, fontSize: 22, fontWeight: FontWeight.bold)), Text(label, style: const TextStyle(color: Color(0xFF888888), fontSize: 11)), ]), )); } class _AddPaymentForm extends ConsumerStatefulWidget { const _AddPaymentForm(); @override ConsumerState<_AddPaymentForm> createState() => _AddState(); } class _AddState extends ConsumerState<_AddPaymentForm> { final _amountCtrl = TextEditingController(); String? _childId; DateTime _month = DateTime(DateTime.now().year, DateTime.now().month); List _children = []; bool _saving = false; @override void initState() { super.initState(); _loadChildren(); } @override void dispose() { _amountCtrl.dispose(); super.dispose(); } Future _loadChildren() async { final sb = Supabase.instance.client; final data = await sb.from('children').select().order('first_name'); if (mounted) setState(() => _children = data.map((d) => Child.fromMap(d)).toList()); } Future _save() async { if (_childId == null || _amountCtrl.text.trim().isEmpty) return; setState(() => _saving = true); try { final sb = Supabase.instance.client; await sb.from('payments').insert({ 'id': const Uuid().v4(), 'child_id': _childId, 'guardian_id': _childId, // placeholder — adjust if you have guardian FK 'month': DateFormat('yyyy-MM-01').format(_month), 'amount': double.tryParse(_amountCtrl.text.replaceAll(',', '.')) ?? 0, 'status': 'pending', }); if (mounted) Navigator.pop(context); } catch (e) { if (mounted) ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Erro: $e'), backgroundColor: _red)); } finally { if (mounted) setState(() => _saving = false); } } @override Widget build(BuildContext context) => Padding( padding: EdgeInsets.only(left: 20, right: 20, top: 20, bottom: MediaQuery.of(context).viewInsets.bottom + 20), child: Column(mainAxisSize: MainAxisSize.min, children: [ const Text('Nova Mensalidade', style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)), const SizedBox(height: 16), DropdownButtonFormField( value: _childId, dropdownColor: _card, style: const TextStyle(color: Colors.white), decoration: _dec('Seleccionar criança', Icons.child_care), items: _children.map((c) => DropdownMenuItem(value: c.id, child: Text(c.fullName, style: const TextStyle(color: Colors.white)))).toList(), onChanged: (v) => setState(() => _childId = v), ), const SizedBox(height: 12), GestureDetector( onTap: () async { final picked = await showDatePicker(context: context, initialDate: _month, firstDate: DateTime(2020), lastDate: DateTime(2030), builder: (ctx, child) => Theme(data: ThemeData.dark().copyWith( colorScheme: const ColorScheme.dark(primary: _blue)), child: child!)); if (picked != null) setState(() => _month = DateTime(picked.year, picked.month)); }, child: Container( padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 14), decoration: BoxDecoration(color: Colors.white.withOpacity(0.04), borderRadius: BorderRadius.circular(12), border: Border.all(color: Colors.white.withOpacity(0.09))), child: Row(children: [ const Icon(Icons.calendar_month, color: _blue, size: 18), const SizedBox(width: 10), Text(DateFormat('MMMM yyyy', 'pt_PT').format(_month), style: const TextStyle(color: Colors.white)), ]), ), ), const SizedBox(height: 12), TextField(controller: _amountCtrl, keyboardType: TextInputType.number, style: const TextStyle(color: Colors.white), decoration: _dec('Valor (Kz)', Icons.attach_money)), const SizedBox(height: 16), GestureDetector(onTap: _saving ? null : _save, child: Container(height: 48, width: double.infinity, decoration: BoxDecoration(gradient: const LinearGradient(colors: [_blue, Color(0xFF0288D1)]), borderRadius: BorderRadius.circular(12)), child: Center(child: _saving ? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(color: Colors.white, strokeWidth: 2)) : const Text('Criar Mensalidade', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold))))), ]), ); InputDecoration _dec(String hint, IconData icon) => InputDecoration( hintText: hint, hintStyle: const TextStyle(color: Color(0xFF555555), fontSize: 13), prefixIcon: Icon(icon, color: _blue.withOpacity(0.6), size: 18), filled: true, fillColor: Colors.white.withOpacity(0.04), enabledBorder: OutlineInputBorder(borderRadius: BorderRadius.circular(12), borderSide: BorderSide(color: Colors.white.withOpacity(0.09))), contentPadding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12), ); }