import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../Models/Order.dart';
import '../controller/HistoryController.dart';

class DetailPage extends StatelessWidget {
  final Order order;
  final HistoryController historyController = Get.find<HistoryController>();

  DetailPage({super.key, required this.order}) {
    historyController.fetchOrderItems(order.id);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Détails de la commande #${order.id}'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'Total: ${order.totalPrice}',
                  style: const TextStyle(
                    fontSize: 18.0,
                    fontWeight: FontWeight.bold,
                    color: Colors.blue,
                  ),
                ),
                const SizedBox(height: 8.0),
                Text(
                  'Date: ${order.dateTime}',
                  style: const TextStyle(
                    fontSize: 16.0,
                    color: Colors.grey,
                  ),
                ),
              ],
            ),
          ),
          Divider(
            thickness: 1.0,
            color: Colors.grey[300],
          ),
          const SizedBox(height: 16),
          Obx(
            () => historyController.orderItems.isEmpty
                ? const Center(
                    child: Text(
                      'Aucun article trouvé',
                      style: TextStyle(
                        fontSize: 16.0,
                        fontWeight: FontWeight.bold,
                        color: Colors.red,
                      ),
                    ),
                  )
                : ListView.builder(
                    shrinkWrap: true,
                    itemCount: historyController.orderItems.length,
                    itemBuilder: (context, index) {
                      final item = historyController.orderItems[index];

                      return Card(
                        elevation: 2.0,
                        margin: const EdgeInsets.symmetric(
                          horizontal: 16.0,
                          vertical: 8.0,
                        ),
                        color: Colors.white,
                        child: ListTile(
                          leading: const Icon(
                            Icons.add_shopping_cart_rounded,
                            color: Colors.green,
                          ),
                          title: Text(
                            item['product_name'],
                            style: const TextStyle(
                              fontWeight: FontWeight.bold,
                              color: Colors.black,
                            ),
                          ),
                          subtitle: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                'Quantité: ${item['quantity']}',
                                style: const TextStyle(
                                  color: Colors.grey,
                                ),
                              ),
                              Text(
                                'Prix: ${item['price']}',
                                style: const TextStyle(
                                  color: Colors.grey,
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    },
                  ),
          ),
        ],
      ),
    );
  }
}
