import 'package:flutter/material.dart'; import 'package:seshat/domain/models/book.dart'; import 'package:seshat/ui/add_page/view_model/add_view_model.dart'; import 'package:seshat/utils/result.dart'; class ConfirmationPopup extends StatefulWidget { const ConfirmationPopup({ super.key, required this.exitPopup, required this.setPrice, required this.viewModel, required this.book, }); final Function(BuildContext) exitPopup; final Function(num) setPrice; final AddViewModel viewModel; final Book book; @override State createState() => _ConfirmationPopupState(); } class _ConfirmationPopupState extends State { final GlobalKey _formKey = GlobalKey(); double price = 0; @override Widget build(BuildContext context) { final theme = Theme.of(context); return AlertDialog( title: Text("Prix"), content: Form( key: _formKey, child: SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ RichText( text: TextSpan( style: theme.textTheme.bodyMedium, children: [ TextSpan( text: "Titre : ", style: TextStyle(fontWeight: FontWeight.bold), ), TextSpan(text: widget.book.title), ], ), ), RichText( text: TextSpan( style: theme.textTheme.bodyMedium, children: [ TextSpan( text: "Auteur·ice : ", style: TextStyle(fontWeight: FontWeight.bold), ), TextSpan(text: widget.book.author), ], ), ), RichText( text: TextSpan( style: theme.textTheme.bodyMedium, children: [ TextSpan( text: "Prix à neuf : ", style: TextStyle(fontWeight: FontWeight.bold), ), TextSpan(text: widget.book.priceNew.replaceAll("EUR", "€")), ], ), ), SizedBox(height: 10), (widget.viewModel.askPrice) ? TextFormField( decoration: InputDecoration( labelText: "Prix", border: OutlineInputBorder(), suffixText: "€", ), keyboardType: TextInputType.number, validator: (value) { if (value == null || value.isEmpty) { return "Indiquez un prix"; } else if (num.tryParse(value) == null) { return "Le prix doit être un nombre"; } return null; }, onSaved: (newValue) { price = double.parse(newValue!); }, ) : SizedBox(), ], ), ), ), actions: [ TextButton( onPressed: () { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text("Le livre n'a pas été enregistré"), behavior: SnackBarBehavior.floating, ), ); widget.exitPopup(context); }, child: Text("Annuler"), ), TextButton( onPressed: () async { if (widget.viewModel.askPrice && _formKey.currentState!.validate()) { _formKey.currentState!.save(); } var result = await widget.viewModel.sendBook( widget.book, widget.viewModel.currentOwner!, widget.viewModel.currentBal!, price, ); switch (result) { case Ok(): if (context.mounted) { Navigator.of(context).pop(); showDialog( context: context, builder: (context) => AlertDialog( title: Text( "ID : ${widget.viewModel.currentOwner!.firstName[0].toUpperCase()}${widget.viewModel.currentOwner!.lastName[0].toUpperCase()}${(price == 0) ? "PL" : price.toString()}", ), content: Text( (widget.viewModel.currentOwner!.id == widget.viewModel.sectionOwner!.id) ? "Ce livre appartient à la section. Vous pouvez mettre le code, ou poser une gomette, ..." : "Identifiant propriétaire de ce livre. Pensez à l'écrire pour retrouver lae propriétaire du livre lors de la vente ou du retour !", ), actions: [ TextButton( onPressed: () { widget.exitPopup(context); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( "Enregistré: ${widget.book.title}", ), behavior: SnackBarBehavior.floating, ), ); }, child: Text("Ok"), ), ], ), ); } break; case Error(): if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( "Une erreur est survenue : ${result.error}", ), ), ); } } }, child: Text("Valider"), ), ], ); } }