import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:youmazgestion/Components/app_bar.dart';
import 'package:youmazgestion/controller/HistoryController.dart';
import 'package:intl/intl.dart';

import 'bilanDesJourne.dart';

class BilanMois extends StatefulWidget {
  const BilanMois({super.key});

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

class _BilanMoisState extends State<BilanMois> {
  final HistoryController controller = Get.put(HistoryController());
  DateTime selectedDate = DateTime.now();

  void refreshData(DateTime selectedDate) {
    setState(() {
      this.selectedDate = selectedDate;
      controller.refreshOrders();
      controller.getTotalSumOrdersByMonth(selectedDate);
      controller.getOrderCountByMonth(selectedDate);
      controller.getProductQuantitiesByMonth(selectedDate);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:  CustomAppBar(title: 'Bilan du mois'),
      body: Column(
        children: [
          // Les 3 cartes en haut
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              _buildInfoCard(
                title: 'Chiffre réalisé',
                value: '${NumberFormat('#,##0', 'fr_FR').format(controller.totalSum.value)} MGA',
                color: Colors.green,
                icon: Icons.monetization_on,
              ),
              _buildInfoCard(
                title: 'Total de commandes',
                value: controller.orderQuantity.value.toString(),
                color: Colors.orange,
                icon: Icons.shopping_cart,
              ),
              ElevatedButton(
                onPressed: () {
                  // Redirect to BilanDesJourne page
                  Get.to(BilanDesJourne(selectedDate: selectedDate));
                },
                child: const Text('Voir les jours'),
              ),
            ],
          ),
          // La zone déroulante
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    const Text(
                      'Detail produit :',
                      style:
                          TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
                    ),
                    const SizedBox(height: 10),
                    SizedBox(
                      height: MediaQuery.of(context).size.height * 0.5,
                      width: MediaQuery.of(context).size.width * 0.9,
                      child: FutureBuilder<Map<String, int>>(
                        future: controller
                            .getProductQuantitiesByMonth(selectedDate),
                        builder: (context, snapshot) {
                          if (snapshot.connectionState ==
                              ConnectionState.waiting) {
                            return const Center(
                                child: CircularProgressIndicator());
                          } else if (snapshot.hasError) {
                            return const Text(
                                'Erreur lors de la récupération des quantités de produits');
                          } else {
                            final quantities = snapshot.data!;
                            return Column(
                              children: [
                                const SizedBox(height: 20),
                                Expanded(
                                  child: ListView.builder(
                                    itemCount: quantities.length,
                                    itemBuilder: (context, index) {
                                      final entry =
                                          quantities.entries.elementAt(index);
                                      return ListTile(
                                        leading:
                                            const Icon(Icons.shopping_cart),
                                        title: Text(
                                          entry.key,
                                          style: const TextStyle(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 18),
                                        ),
                                        subtitle: Text(
                                            'Quantité : ${entry.value}',
                                            style:
                                                const TextStyle(fontSize: 16)),
                                      );
                                    },
                                  ),
                                ),
                              ],
                            );
                          }
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => refreshData(selectedDate),
        backgroundColor: Colors.deepOrange,
        focusColor: Colors.deepOrangeAccent,
        hoverColor: Colors.deepOrangeAccent,
        child: const Icon(Icons.refresh),
      ),
      persistentFooterButtons: [
        TextButton(
          onPressed: () async {
            final DateTime? pickedDate = await showDatePicker(
              context: context,
              initialDate: selectedDate,
              firstDate: DateTime(DateTime.now().year - 5),
              lastDate: DateTime(DateTime.now().year + 5),
            );
            if (pickedDate != null) {
              refreshData(pickedDate);
            }
          },
          child: const Text(
            'Changer la date',
            style: TextStyle(fontSize: 16),
          ),
        ),
      ],
    );
  }

  Widget _buildInfoCard({
    required String title,
    required String value,
    required Color color,
    required IconData icon,
  }) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            Icon(
              icon,
              size: 40,
              color: color,
            ),
            const SizedBox(height: 10),
            Text(
              title,
              style: const TextStyle(fontSize: 18),
            ),
            const SizedBox(height: 10),
            Text(
              value,
              style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
          ],
        ),
      ),
    );
  }
}
