import 'package:cheminova/controller/cart_controller.dart';
import 'package:cheminova/controller/rd_get_order_controller.dart';
import 'package:cheminova/models/rd_get_order_model.dart';
import 'package:cheminova/models/rd_order_item_model.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
class ProductCard1 extends StatefulWidget {
  final RDOrderItem productModel; // The specific product to be displayed
  int? quantity;

  ProductCard1({
    super.key,
    required this.productModel, // Pass the product model explicitly
    this.quantity = 1,
  });

  @override
  State<ProductCard1> createState() => _ProductCard1State();
}

class _ProductCard1State extends State<ProductCard1> {
  String capitalizeFirstLetter(String text) {
    if (text.isEmpty) return text;
    return text[0].toUpperCase() + text.substring(1).toLowerCase();
  }

  @override
  Widget build(BuildContext context) {
    final CartController _cartController = Get.put(CartController());

    // Current quantity the user wants to process
    int processQuantity = widget.productModel.processquantity ?? 1;

    // Total available quantity
    int availableQuantity = widget.productModel.remainingQuantity ?? 1;

    return Card(
      child: Row(
        children: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(15.0),
              child: Container(
                height: Get.height * 0.15,
                width: Get.width * 0.31,
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage("assets/images/product.png"),
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            ),
          ),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 3.0),
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // Display product name with capitalization
                    Text(
                      capitalizeFirstLetter(widget.productModel.name),
                      style: GoogleFonts.roboto(
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    // Display category name
                    Text(
                      capitalizeFirstLetter(widget.productModel.categoryName),
                      style: GoogleFonts.roboto(
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                    // Display the price of the product
                    Text(
                      "₹ ${widget.productModel.price.toString()}",
                      style: GoogleFonts.roboto(
                        fontSize: 22,
                        fontWeight: FontWeight.w700,
                      ),
                    ),
                    Row(
                      children: [
                        Text(
                          "Ordered Quantity : ",
                          style: GoogleFonts.roboto(
                            fontSize: 15,
                            fontWeight: FontWeight.w700,
                          ),
                        ),
                        Text(
                          " ${widget.productModel.remainingQuantity}",
                          style: GoogleFonts.roboto(
                            fontSize: 15,
                          ),
                        ),
                      ],
                    ),
                    // Quantity adjustment buttons
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          "Availability :",
                          style: GoogleFonts.roboto(
                            fontSize: 15,
                            fontWeight: FontWeight.w700,
                          ),
                        ),
                        Container(
                          height: Get.height * 0.04,
                          width: Get.width * 0.21,
                          decoration: BoxDecoration(
                            color: const Color(0xFF004791),
                            borderRadius: BorderRadius.circular(10),
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              // Decrease quantity button
                              SizedBox(
                                height: 24,
                                width: 24,
                                child: ElevatedButton(
                                  onPressed: () {
                                    setState(() {
                                      if (processQuantity > 1) {
                                        processQuantity--;
                                        widget.productModel.processquantity = processQuantity;
                                      }
                                    });
                                  },
                                  style: ElevatedButton.styleFrom(
                                    padding: EdgeInsets.zero,
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(8.0),
                                    ),
                                  ),
                                  child: Text(
                                    '-',
                                    style: GoogleFonts.roboto(
                                      fontSize: 16,
                                      fontWeight: FontWeight.w800,
                                    ),
                                  ),
                                ),
                              ),
                              // Display the current process quantity
                              Text(
                                "$processQuantity",
                                style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 16,
                                ),
                              ),
                              // Increase quantity button
                              SizedBox(
                                height: 22,
                                width: 22,
                                child: ElevatedButton(
                                  onPressed: () {
                                    setState(() {
                                      if (processQuantity < availableQuantity) {
                                        processQuantity++;
                                        widget.productModel.processquantity = processQuantity;
                                      }
                                    });
                                  },
                                  style: ElevatedButton.styleFrom(
                                    padding: EdgeInsets.zero,
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(8.0),
                                    ),
                                  ),
                                  child: Text(
                                    '+',
                                    style: GoogleFonts.roboto(
                                      fontSize: 16,
                                      fontWeight: FontWeight.w800,
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ]),
            ),
          )
        ],
      ),
    );
  }
}
