import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:youmazgestion/Models/client.dart';


// Dialog pour la remise
class DiscountDialog extends StatefulWidget {
  final Commande commande;

  const DiscountDialog({super.key, required this.commande});

  @override
  _DiscountDialogState createState() => _DiscountDialogState();
}

class _DiscountDialogState extends State<DiscountDialog> {
  final _pourcentageController = TextEditingController();
  final _montantController = TextEditingController();
  bool _isPercentage = true;
  double _montantFinal = 0;

  @override
  void initState() {
    super.initState();
    _montantFinal = widget.commande.montantTotal;
  }

  void _calculateDiscount() {
    double discount = 0;
    
    if (_isPercentage) {
      final percentage = double.tryParse(_pourcentageController.text) ?? 0;
      discount = (widget.commande.montantTotal * percentage) / 100;
    } else {
      discount = double.tryParse(_montantController.text) ?? 0;
    }
    
    setState(() {
      _montantFinal = widget.commande.montantTotal - discount;
      if (_montantFinal < 0) _montantFinal = 0;
    });
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text('Appliquer une remise'),
      content: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text('Montant original: ${NumberFormat('#,##0', 'fr_FR').format(widget.commande.montantTotal)} MGA'),
            const SizedBox(height: 16),
            
            // Choix du type de remise
            Row(
              children: [
                Expanded(
                  child: RadioListTile<bool>(
                    title: const Text('Pourcentage'),
                    value: true,
                    groupValue: _isPercentage,
                    onChanged: (value) {
                      setState(() {
                        _isPercentage = value!;
                        _calculateDiscount();
                      });
                    },
                  ),
                ),
                Expanded(
                  child: RadioListTile<bool>(
                    title: const Text('Montant fixe'),
                    value: false,
                    groupValue: _isPercentage,
                    onChanged: (value) {
                      setState(() {
                        _isPercentage = value!;
                        _calculateDiscount();
                      });
                    },
                  ),
                ),
              ],
            ),
            
            const SizedBox(height: 16),
            
            if (_isPercentage)
              TextField(
                controller: _pourcentageController,
                decoration: const InputDecoration(
                  labelText: 'Pourcentage de remise',
                  suffixText: '%',
                  border: OutlineInputBorder(),
                ),
                keyboardType: TextInputType.number,
                onChanged: (value) => _calculateDiscount(),
              )
            else
              TextField(
                controller: _montantController,
                decoration: const InputDecoration(
                  labelText: 'Montant de remise',
                  suffixText: 'MGA',
                  border: OutlineInputBorder(),
                ),
                keyboardType: TextInputType.number,
                onChanged: (value) => _calculateDiscount(),
              ),
            
            const SizedBox(height: 16),
            
            Container(
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: Colors.blue.shade50,
                borderRadius: BorderRadius.circular(8),
                border: Border.all(color: Colors.blue.shade200),
              ),
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text('Montant final:'),
                      Text(
                        '${NumberFormat('#,##0', 'fr_FR').format(_montantFinal)} MGA',
                        style: const TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 16,
                        ),
                      ),
                    ],
                  ),
                  if (_montantFinal < widget.commande.montantTotal)
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text('Économie:'),
                        Text(
                          '${NumberFormat('#,##0', 'fr_FR').format(widget.commande.montantTotal - _montantFinal)} MGA',
                          style: const TextStyle(
                            color: Colors.green,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                ],
              ),
            ),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('Annuler'),
        ),
        ElevatedButton(
          onPressed: _montantFinal < widget.commande.montantTotal
              ? () {
                  final pourcentage = _isPercentage 
                      ? double.tryParse(_pourcentageController.text) 
                      : null;
                  final montant = !_isPercentage 
                      ? double.tryParse(_montantController.text) 
                      : null;
                  
                  Navigator.pop(context, {
                    'pourcentage': pourcentage,
                    'montant': montant,
                    'montantFinal': _montantFinal,
                  });
                }
              : null,
          child: const Text('Appliquer'),
        ),
      ],
    );
  }

  @override
  void dispose() {
    _pourcentageController.dispose();
    _montantController.dispose();
    super.dispose();
  }
}
