import 'package:flutter/material.dart'; import 'package:p_tits_pas/models/m_dashbord/assistant_model.dart'; import 'package:p_tits_pas/models/m_dashbord/child_model.dart'; import 'package:p_tits_pas/models/m_dashbord/contract_model.dart'; import 'package:p_tits_pas/models/m_dashbord/conversation_model.dart'; import 'package:p_tits_pas/models/m_dashbord/event_model.dart'; import 'package:p_tits_pas/models/m_dashbord/notification_model.dart'; import 'package:p_tits_pas/services/dashboardService.dart'; class ParentDashboardController extends ChangeNotifier { final DashboardService _dashboardService; ParentDashboardController(this._dashboardService); // État des données List _children = []; String? _selectedChildId; AssistantModel? _selectedAssistant; List _upcomingEvents = []; List _contracts = []; List _conversations = []; List _notifications = []; // État de chargement bool _isLoading = false; String? _error; // Getters List get children => _children; String? get selectedChildId => _selectedChildId; ChildModel? get selectedChild => _children.where((c) => c.id == _selectedChildId).firstOrNull; AssistantModel? get selectedAssistant => _selectedAssistant; List get upcomingEvents => _upcomingEvents; List get contracts => _contracts; List get conversations => _conversations; List get notifications => _notifications; bool get isLoading => _isLoading; String? get error => _error; // Initialisation du dashboard Future initDashboard() async { _isLoading = true; _error = null; notifyListeners(); try { await Future.wait([ _loadChildren(), _loadUpcomingEvents(), _loadContracts(), _loadConversations(), _loadNotifications(), ]); // Sélectionner le premier enfant par défaut if (_children.isNotEmpty && _selectedChildId == null) { await selectChild(_children.first.id); } } catch (e) { _error = 'Erreur lors du chargement du tableau de bord: $e'; } finally { _isLoading = false; notifyListeners(); } } // Sélection d'un enfant Future selectChild(String childId) async { _selectedChildId = childId; notifyListeners(); // Charger les données spécifiques à cet enfant await _loadChildSpecificData(childId); } // Afficher le modal d'ajout d'enfant void showAddChildModal() { // Logique pour ouvrir le modal d'ajout d'enfant // Sera implémentée dans le ticket FRONT-09 } // Méthodes privées de chargement des données Future _loadChildren() async { _children = await _dashboardService.getChildren(); notifyListeners(); } Future _loadChildSpecificData(String childId) async { try { // Charger l'assistante maternelle associée à cet enfant _selectedAssistant = await _dashboardService.getAssistantForChild(childId); // Filtrer les événements et contrats pour cet enfant _upcomingEvents = await _dashboardService.getEventsForChild(childId); _contracts = await _dashboardService.getContractsForChild(childId); notifyListeners(); } catch (e) { _error = 'Erreur lors du chargement des données pour l\'enfant: $e'; notifyListeners(); } } Future _loadUpcomingEvents() async { _upcomingEvents = await _dashboardService.getUpcomingEvents(); notifyListeners(); } Future _loadContracts() async { _contracts = await _dashboardService.getContracts(); notifyListeners(); } Future _loadConversations() async { _conversations = await _dashboardService.getConversations(); notifyListeners(); } Future _loadNotifications() async { _notifications = await _dashboardService.getNotifications(); notifyListeners(); } // Méthodes d'action Future markNotificationAsRead(String notificationId) async { try { await _dashboardService.markNotificationAsRead(notificationId); await _loadNotifications(); // Recharger les notifications } catch (e) { _error = 'Erreur lors du marquage de la notification: $e'; notifyListeners(); } } Future refreshDashboard() async { await initDashboard(); } }