import 'package:flutter/material.dart'; import 'package:geosector_app/core/data/models/amicale_model.dart'; import 'package:geosector_app/core/repositories/amicale_repository.dart'; import 'package:geosector_app/core/repositories/user_repository.dart'; import 'package:geosector_app/presentation/widgets/entite_form.dart'; import 'package:provider/provider.dart'; /// Exemple d'utilisation du widget EntiteForm /// /// Ce widget montre comment intégrer le formulaire d'entité dans une page /// et comment gérer les événements de soumission. class EntiteFormExample extends StatefulWidget { final int? amicaleId; final bool readOnly; const EntiteFormExample({ Key? key, this.amicaleId, this.readOnly = false, }) : super(key: key); @override State createState() => _EntiteFormExampleState(); } class _EntiteFormExampleState extends State { AmicaleModel? _amicale; bool _isLoading = true; @override void initState() { super.initState(); _loadAmicale(); } Future _loadAmicale() async { setState(() { _isLoading = true; }); try { if (widget.amicaleId != null) { // Récupérer l'amicale depuis le repository final amicaleRepository = Provider.of(context, listen: false); final amicale = amicaleRepository.getAmicaleById(widget.amicaleId!); setState(() { _amicale = amicale; _isLoading = false; }); } else { // Création d'une nouvelle amicale setState(() { _amicale = null; _isLoading = false; }); } } catch (e) { debugPrint('Erreur lors du chargement de l\'amicale: $e'); setState(() { _isLoading = false; }); } } void _handleSubmit(AmicaleModel amicale) async { try { final amicaleRepository = Provider.of(context, listen: false); // Sauvegarder l'amicale final savedAmicale = await amicaleRepository.saveAmicale(amicale); // Afficher un message de confirmation if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Amicale ${savedAmicale.name} sauvegardée avec succès'), backgroundColor: Colors.green, ), ); // Retourner à la page précédente Navigator.of(context).pop(); } } catch (e) { debugPrint('Erreur lors de la sauvegarde de l\'amicale: $e'); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Erreur lors de la sauvegarde: $e'), backgroundColor: Colors.red, ), ); } } } @override Widget build(BuildContext context) { final userRepository = Provider.of(context, listen: false); final userRole = userRepository.getUserRole(); final bool canCreate = userRole > 1; // Seuls les utilisateurs avec rôle > 1 peuvent créer/modifier return Scaffold( appBar: AppBar( title: Text(widget.amicaleId != null ? (widget.readOnly ? 'Détails de l\'amicale' : 'Modifier l\'amicale') : 'Nouvelle amicale'), actions: [ if (!widget.readOnly && _amicale != null) IconButton( icon: const Icon(Icons.delete), onPressed: () => _showDeleteConfirmation(context), tooltip: 'Supprimer', ), ], ), body: _isLoading ? const Center(child: CircularProgressIndicator()) : !canCreate && _amicale == null ? const Center( child: Text( 'Vous n\'avez pas les droits pour créer une amicale'), ) : SingleChildScrollView( padding: const EdgeInsets.all(16.0), child: EntiteForm( amicale: _amicale, onSubmit: widget.readOnly ? null : _handleSubmit, readOnly: widget.readOnly, ), ), ); } void _showDeleteConfirmation(BuildContext context) { if (_amicale == null) return; showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Confirmation de suppression'), content: Text( 'Êtes-vous sûr de vouloir supprimer l\'amicale ${_amicale!.name} ?'), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text('Annuler'), ), TextButton( onPressed: () async { Navigator.of(context).pop(); try { final amicaleRepository = Provider.of(context, listen: false); await amicaleRepository.deleteAmicale(_amicale!.id); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Amicale ${_amicale!.name} supprimée'), backgroundColor: Colors.green, ), ); // Retourner à la page précédente Navigator.of(context).pop(); } } catch (e) { debugPrint('Erreur lors de la suppression de l\'amicale: $e'); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Erreur lors de la suppression: $e'), backgroundColor: Colors.red, ), ); } } }, child: const Text('Supprimer'), style: TextButton.styleFrom(foregroundColor: Colors.red), ), ], ), ); } }