195 lines
6.8 KiB
Dart
195 lines
6.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:p_tits_pas/services/user_service.dart';
|
|
import 'package:p_tits_pas/widgets/admin/base_user_management.dart';
|
|
|
|
class ParentManagementWidget extends StatelessWidget {
|
|
const ParentManagementWidget({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return BaseUserManagementWidget(
|
|
config: UserDisplayConfig(
|
|
title: 'Gestion des Parents',
|
|
role: 'parent',
|
|
defaultIcon: Icons.person_outline,
|
|
filterFields: [
|
|
FilterField(
|
|
label: 'Rechercher',
|
|
hint: 'Nom ou email',
|
|
type: FilterType.text,
|
|
filter: (user, query) {
|
|
final fullName = '${user['prenom'] ?? ''} ${user['nom'] ?? ''}'.toLowerCase();
|
|
final email = (user['email'] ?? '').toLowerCase();
|
|
return fullName.contains(query.toLowerCase()) ||
|
|
email.contains(query.toLowerCase());
|
|
},
|
|
),
|
|
FilterField(
|
|
label: 'Statut',
|
|
hint: 'Tous',
|
|
type: FilterType.dropdown,
|
|
options: ['actif', 'en_attente', 'inactif', 'supprimé'],
|
|
filter: (user, status) {
|
|
if (status.isEmpty) return true;
|
|
return user['statut']?.toString().toLowerCase() == status.toLowerCase();
|
|
},
|
|
),
|
|
FilterField(
|
|
label: 'Nombre d\'enfants',
|
|
hint: 'Minimum',
|
|
type: FilterType.number,
|
|
filter: (user, query) {
|
|
final nombreEnfants = int.tryParse(user['nombreEnfants']?.toString() ?? '0') ?? 0;
|
|
final minEnfants = int.tryParse(query) ?? 0;
|
|
return nombreEnfants >= minEnfants;
|
|
},
|
|
),
|
|
],
|
|
actions: [
|
|
const UserAction(
|
|
icon: Icons.edit,
|
|
color: Colors.orange,
|
|
tooltip: 'Modifier',
|
|
onPressed: _editParent,
|
|
),
|
|
const UserAction(
|
|
icon: Icons.delete,
|
|
color: Colors.red,
|
|
tooltip: 'Supprimer',
|
|
onPressed: _deleteParent,
|
|
),
|
|
const UserAction(
|
|
icon: Icons.child_care,
|
|
color: Colors.purple,
|
|
tooltip: 'Voir enfants',
|
|
onPressed: _showChildren,
|
|
),
|
|
],
|
|
getDisplayName: (user) => '${user['prenom'] ?? ''} ${user['nom'] ?? ''}',
|
|
getSubtitle: (user) {
|
|
final email = user['email'] ?? '';
|
|
final nombreEnfants = user['nombreEnfants'] ?? user['children']?.length ?? 0;
|
|
return '$email\nNombre d\'enfants: $nombreEnfants';
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
static Future<void> _editParent(BuildContext context, Map<String, dynamic> parent) async {
|
|
// TODO: Implémenter l'édition
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Fonctionnalité de modification à implémenter'),
|
|
backgroundColor: Colors.orange,
|
|
),
|
|
);
|
|
}
|
|
|
|
static Future<void> _deleteParent(BuildContext context, Map<String, dynamic> parent) async {
|
|
final confirmed = await showDialog<bool>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Confirmer la suppression'),
|
|
content: Text(
|
|
'Êtes-vous sûr de vouloir supprimer le compte de ${parent['prenom']} ${parent['nom']} ?\n\n'
|
|
'Cette action supprimera également tous les contrats et données associés.'
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(false),
|
|
child: const Text('Annuler'),
|
|
),
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(true),
|
|
style: TextButton.styleFrom(foregroundColor: Colors.red),
|
|
child: const Text('Supprimer'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
|
|
if (confirmed == true) {
|
|
try {
|
|
final userService = UserService();
|
|
final success = await userService.deleteUser(parent['id']);
|
|
|
|
if (success && context.mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('${parent['prenom']} ${parent['nom']} supprimé avec succès'),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
} else {
|
|
throw Exception('Erreur lors de la suppression');
|
|
}
|
|
} catch (e) {
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Erreur: ${e.toString()}'),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static Future<void> _showChildren(BuildContext context, Map<String, dynamic> parent) async {
|
|
final children = parent['children'] as List<dynamic>? ?? [];
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: Text('Enfants de ${parent['prenom']} ${parent['nom']}'),
|
|
content: Container(
|
|
width: double.maxFinite,
|
|
constraints: const BoxConstraints(maxHeight: 400),
|
|
child: children.isEmpty
|
|
? const Text('Aucun enfant enregistré')
|
|
: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: children.map((child) => Card(
|
|
child: ListTile(
|
|
leading: const Icon(Icons.child_care),
|
|
title: Text(child['prenom'] ?? child['firstName'] ?? 'Nom non défini'),
|
|
subtitle: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
if (child['dateNaissance'] != null || child['birthDate'] != null)
|
|
Text('Né(e) le: ${child['dateNaissance'] ?? child['birthDate']}'),
|
|
if (child['age'] != null)
|
|
Text('Âge: ${child['age']} ans'),
|
|
],
|
|
),
|
|
isThreeLine: true,
|
|
),
|
|
)).toList(),
|
|
),
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: const Text('Fermer'),
|
|
),
|
|
if (children.isNotEmpty)
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
// TODO: Naviguer vers la gestion des enfants
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Navigation vers la gestion des enfants à implémenter'),
|
|
),
|
|
);
|
|
},
|
|
child: const Text('Gérer les enfants'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
} |