import 'package:auto_size_text/auto_size_text.dart';
import 'package:cheminova/controller/get_order_placed_controller.dart';
import 'package:cheminova/models/oder_place_model.dart';
import 'package:cheminova/models/order_item_model.dart';
import 'package:cheminova/models/place_order_list_model.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';

import '../../controller/cart_controller.dart';
import '../../models/product_model1.dart';

class OrderManagementDetailScreen extends StatefulWidget {
  //final Product? productModel;
  PlacedOrderList? placedOrderList;
  PlacedOrderModel? placedOrderModel;
  OrderManagementDetailScreen({super.key,this.placedOrderList,this.placedOrderModel});

  @override
  State<OrderManagementDetailScreen> createState() =>
      _OrderManagementDetailScreenState();
}


class _OrderManagementDetailScreenState
    extends State<OrderManagementDetailScreen> {
  final CartController _cartController = Get.put(CartController());
  final GetPlacedOrderController _getPlacedOrderController = Get.put(GetPlacedOrderController());
  String formatDate(String apiDate) {

    DateTime parsedDate = DateTime.parse(apiDate);

    String formattedDate = DateFormat('dd-MMM-yyyy').format(parsedDate);

    return formattedDate;
  }
  String capitalizeFirstLetter(String text) {
    if (text.isEmpty) return text;
    return text[0].toUpperCase() + text.substring(1).toLowerCase();
  }

Future<void> adduni()async {
  final Set<String> uniqueOrderIds = {};
  final List<PlacedOrderList> uniqueOrders = [];

  for (var order in _getPlacedOrderController.placedOrders) {
    if (uniqueOrderIds.add(order.uniqueId)) {
      uniqueOrders.add(order);
    }
  }
  final order = uniqueOrders[0];

  // Combine product names into a single string
  final productNames = order.orderItem
      .map((item) => (item.name))
      .join(', ');
  final categotyName = order.orderItem
      .map((item) => (item.categoryName))
      .join(', ');
  final quantity =  order.orderItem
      .map((item) => (item.quantity))
      .join(', ');
}


      @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        leading: GestureDetector(
          onTap: () {},
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: SvgPicture.asset(
              'assets/svg/menu.svg',
            ),
          ),
        ),
        actions: [
          GestureDetector(
            onTap: () => Get.back(),
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: SvgPicture.asset(
                'assets/svg/back_arrow.svg',
              ),
            ),
          ),
        ],
        title: const Text(
          "Order Detail",
        ),
      ),
      body: Stack(
        fit: StackFit.expand,
        children: [
          Image.asset(
            'assets/images/image_1.png',
            fit: BoxFit.cover,
          ),
          SafeArea(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                SizedBox(
                  height: Get.height * 0.02,
                ),
                Card(
                  margin: const EdgeInsets.symmetric(horizontal: 18),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(19),
                    side: const BorderSide(color: Color(0xFFFDFDFD)),
                  ),
                  color: const Color(0xFFB4D1E5).withOpacity(0.9),
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Card(
                          child: Column(
                            children: [
                              SizedBox(
                                width: Get.width,
                                child: Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(8, 8, 8, 0),
                                  child: Text(
                                    "Order Summary",
                                    style: GoogleFonts.roboto(
                                      fontSize: Get.width * 0.05,
                                      fontWeight: FontWeight.bold,
                                    ),
                                  ),
                                ),
                              ),
                              SizedBox(
                                width: Get.width,
                                child: Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(8, 8, 8, 0),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                       "Order ID:",
                                        style: GoogleFonts.roboto(
                                          fontSize: Get.width * 0.04,
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                      Text(widget.placedOrderList!.uniqueId),
                                    ],
                                  ),
                                ),
                              ),
                              SizedBox(
                                width: Get.width,
                                child: Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(8, 8, 8, 0),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                        "Order Date: ",
                                        style: GoogleFonts.roboto(
                                          fontSize: Get.width * 0.04,
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                      Text(formatDate("${widget.placedOrderList!.createdAt}")),
                                    ],
                                  ),
                                ),
                              ),
                              SizedBox(
                                width: Get.width,
                                child: Padding(
                                  padding:  EdgeInsets.all(8.0),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                        "Total Amount: ",
                                        style: GoogleFonts.roboto(
                                          fontSize: Get.width * 0.04,
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                     Text("₹ ${widget.placedOrderList!.grandTotal}"),
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        const SizedBox(height: 8),
                        Card(
                          child: SizedBox(
                            height: Get.height * 0.22,
                            child: Padding(
                              padding: EdgeInsets.all(Get.width * 0.02),
                              child: ListView.builder(
                                padding: EdgeInsets.zero,
                                itemCount: widget.placedOrderList?.orderItem.length ?? 0,
                                itemBuilder: (context, index) {
                                  final orderItem = widget.placedOrderList!.orderItem[index];
                                  return orderItem != null
                                      ? Card(
                                    margin: const EdgeInsets.symmetric(vertical: 5.0),
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(10),
                                    ),
                                    child: Padding(
                                      padding: const EdgeInsets.all(8.0),
                                      child: Row(
                                        children: [
                                          Image.asset(
                                           "assets/images/product.png", // Add the image URL here
                                            height: 50,
                                            width: 50,
                                            fit: BoxFit.cover,
                                          ),
                                          const SizedBox(width: 10),
                                          Expanded(
                                            child: Column(
                                              crossAxisAlignment: CrossAxisAlignment.end,
                                              children: [
                                                Text(
                                                  capitalizeFirstLetter(orderItem.name),
                                                  style: GoogleFonts.roboto(
                                                    fontSize: Get.width * 0.04,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                                Text(
                                                  "Quantity: ${orderItem.quantity}",
                                                  style: GoogleFonts.roboto(
                                                    fontSize: Get.width * 0.03,
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  )
                                      : const SizedBox.shrink();
                                },
                              ),
                            ),
                          ),
                        ),
                        const SizedBox(height: 8),
                        Card(
                          child: Column(
                            children: [
                              SizedBox(
                                width: Get.width,
                                child: Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(8, 8, 8, 0),
                                  child: Text(
                                    "Shipping Information",
                                    style: GoogleFonts.roboto(
                                      fontSize: Get.width * 0.05,
                                      fontWeight: FontWeight.w500,
                                    ),
                                  ),
                                ),
                              ),
                              SizedBox(
                                width: Get.width,
                                height: Get.height*0.06,
                                child: Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(8, 8, 8, 0),
                                  child: Row(
                                    children: [
                                      Text(
                                        "Address: ",
                                        style: GoogleFonts.roboto(
                                          fontSize: Get.width * 0.04,
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                      AutoSizeText("${widget.placedOrderList!.shipTo}",maxLines: 4,
                                          overflow:TextOverflow.ellipsis,)
                                    ],
                                  ),
                                ),
                              ),
                              // SizedBox(
                              //   width: Get.width,
                              //   child: Padding(
                              //     padding: const EdgeInsets.all(8.0),
                              //     child: Text(
                              //       "Contact: +91 9123456789",
                              //       style: GoogleFonts.roboto(
                              //         fontSize: Get.width * 0.04,
                              //         fontWeight: FontWeight.w400,
                              //       ),
                              //     ),
                              //   ),
                              // ),
                            ],
                          ),
                        ),
                        const SizedBox(height: 8),
                        Card(
                          child: SizedBox(
                            width: Get.width,
                            child: Padding(
                              padding: const EdgeInsets.all(8),
                              child: Row(
                                children: [
                                  Text(
                                    "Status: ",
                                    style: GoogleFonts.roboto(
                                      fontSize: Get.width * 0.04,
                                      fontWeight: FontWeight.w600,
                                    ),
                                  ),
                                  Text(capitalizeFirstLetter("${widget.placedOrderList!.status}")),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                SizedBox(height: Get.height * 0.04),
                // SizedBox(
                //   width: Get.width * 0.9,
                //   height: Get.height * 0.06,
                //   child: ElevatedButton(
                //     onPressed: () => Get.to(
                //       () =>  OrderFullfilmentScreen(
                //         placedOrderList:widget.placedOrderList ,
                //       ),
                //     ),
                //     style: ElevatedButton.styleFrom(
                //       foregroundColor: Colors.white,
                //       backgroundColor: const Color(0xFF00784C),
                //       shape: RoundedRectangleBorder(
                //         borderRadius: BorderRadius.circular(10),
                //       ),
                //     ),
                //     child: Text(
                //       "Fulfill Order",
                //       style: GoogleFonts.roboto(
                //         fontSize: Get.width * 0.04,
                //         fontWeight: FontWeight.w600,
                //       ),
                //     ),
                //   ),
                // ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
