import 'package:cheminova/models/oder_place_model.dart';
import 'package:cheminova/models/product_model.dart';
import 'package:cheminova/screens/order/checkout_screen.dart';
import 'package:cheminova/widgets/my_drawer.dart';
import 'package:cheminova/widgets/product_card.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 '../../controller/cart_controller.dart';
import '../../models/product_model1.dart';

class CartScreen extends StatefulWidget {
   Product? productModel;
  PlacedOrderModel? placedOrder;
   CartScreen({super.key,  this.productModel,this.placedOrder});

  @override
  State<CartScreen> createState() => _CartScreenState();
}

class _CartScreenState extends State<CartScreen> {
  final CartController _cartController = Get.find<CartController>();

  // @override
  void initState() {
    super.initState();
    if (widget.productModel != null) {
      _cartController.addToCart(widget.productModel!);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        leading: Builder(
          builder: (context) {
            return GestureDetector(
              onTap: () => Scaffold.of(context).openDrawer(),
              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(
          "Cart",
        ),
      ),
      drawer: const MyDrawer(),
      body: Stack(
        fit: StackFit.expand,
        children: [
          Image.asset(
            'assets/images/image_1.png',
            fit: BoxFit.cover,
          ),
          SafeArea(
            child: Column(
              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: [
                        SizedBox(
                          height: Get.height * 0.6,
                          child: Obx(() {
                            return ListView.builder(
                              padding: EdgeInsets.zero,
                              itemCount: _cartController.cartList.length,
                              itemBuilder: (context, index) => ProductCard(
                                productModel: _cartController.cartList[index],
                                isInCart: true,
                                placedOrder:widget.placedOrder,
                              ),
                            );
                          }),
                        ),
                        const SizedBox(
                          height: 10,
                        ),
                        Obx(() {
                          return Text(
                            "Subtotal Price: ₹ ${_cartController.subtotal.value}",
                            style: GoogleFonts.roboto(
                              fontSize: 15,
                              fontWeight: FontWeight.w700,
                              color: Colors.white,
                            ),
                          );

                        }),


                        // const SizedBox(
                        //   height: 16,
                        // ),
                        Obx(() {
                          return Text(
                            "gstTotal Price: ₹ ${_cartController.gstTotal.value}",
                            style: GoogleFonts.roboto(
                              fontSize: 15,
                              fontWeight: FontWeight.w700,
                              color: Colors.white,
                            ),
                          );

                        }),
                        Obx(() {
                          return Text(
                            "grandTotal Price: ₹ ${_cartController.grandTotal.value}",
                            style: GoogleFonts.roboto(
                              fontSize: 15,
                              fontWeight: FontWeight.w700,
                              color: Colors.white,
                            ),
                          );

                        }),
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  height: Get.height * 0.025,
                ),
                SizedBox(
                  width: Get.width * 0.9,
                  height: Get.height * 0.06,
                  child: ElevatedButton(
                    onPressed: () => Get.to(() =>  CheckoutScreen(

                        productModel:widget.productModel
                    )),
                    style: ElevatedButton.styleFrom(
                      foregroundColor: Colors.white,
                      backgroundColor: const Color(0xFF00784C),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10),
                      ),
                    ),
                    child: Text(
                      "Proceed to Checkout",
                      style: GoogleFonts.roboto(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
