// ignore_for_file: unused_result, use_build_context_synchronously
import 'dart:async';
import 'dart:io';

import 'package:firebase_core/firebase_core.dart' as firebase_core;
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_feather_icons/flutter_feather_icons.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:image_picker/image_picker.dart';
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';
import 'package:intl/intl.dart';
import 'package:mobile_pos/GlobalComponents/button_global.dart';
import 'package:mobile_pos/Provider/category,brans,units_provide.dart';
import 'package:mobile_pos/Screens/Products/brands_list.dart';
import 'package:mobile_pos/Screens/Products/category_list.dart';
import 'package:mobile_pos/Screens/Products/unit_list.dart';
import 'package:mobile_pos/const_commas.dart';
import 'package:mobile_pos/generated/l10n.dart' as lang;
import 'package:mobile_pos/model/product_model.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:nb_utils/nb_utils.dart';

import '../../GlobalComponents/Model/category_model.dart';
import '../../Provider/product_provider.dart';
import '../../Services/check user subscription limit/check_subscription_limit_for_user.dart';
import '../../constant.dart';
import '../../currency.dart';
import '../../subscription.dart';
import '../Warehouse/warehouse_model.dart';
import '../tax report/tax_model.dart';
import 'excel_upload screen.dart';

class AddProduct extends StatefulWidget {
  const AddProduct(
      {super.key,
      required this.productNameList,
      required this.productCodeList,
      required this.warehouseBasedProductModel});

  final List<WarehouseBasedProductModel> warehouseBasedProductModel;
  final List<String> productNameList;
  final List<String> productCodeList;

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

class AddProductState extends State<AddProduct> {
  bool saleButtonClicked = false;
  bool isSerialNumberTaken = false;
  TextEditingController productSerialNumberController = TextEditingController(text: '');
  List<String> productSerialNumberList = [];
  GlobalKey<FormState> globalKey = GlobalKey<FormState>();
  GetCategoryAndVariationModel data =
      GetCategoryAndVariationModel(variations: [], categoryName: '');
  String productCategory = '';
  String productCategoryHint = lang.S.current.selectProductCategory;
  String brandName = '';
  String brandNameHint = lang.S.current.selectBrand;
  String productUnit = '';
  String productUnitHint = lang.S.current.selectUnit;
  late String productName, productSalePrice, productPurchasePrice, productCode;
  String productWholeSalePrice = '0';
  String productDealerPrice = '0';
  String productManufacturer = '';
  int lowerStockAlert = 5;
  String size = '';
  String color = '';
  String weight = '';
  String capacity = '';
  String type = '';
  String productPicture =
      'https://firebasestorage.googleapis.com/v0/b/maanpos.appspot.com/o/Customer%20Picture%2FNo_Image_Available.jpeg?alt=media&token=3de0d45e-0e4a-4a7b-b115-9d6722d5031f';
  String productDiscount = '';
  final ImagePicker _picker = ImagePicker();
  XFile? pickedImage;
  TextEditingController productCodeController = TextEditingController();
  File imageFile = File('No File');
  String imagePath = 'No Data';

  // Add these variables to your state class
  Timer? _calculationTimer;
  final Duration _calculationDelay = const Duration(milliseconds: 800);

  void calculatePurchaseAndMrp({String? from}) {
    double purchasePrice =
        double.tryParse(_cleanNumberInput(productPurchasePriceController.text)) ?? 0;
    double salePrice = double.tryParse(_cleanNumberInput(productSalePriceController.text)) ?? 0;
    double taxRate = selectedGroupTaxModel != null
        ? double.parse(selectedGroupTaxModel!.taxRate.toString())
        : 0.0;

    if (from == 'mrp') {
      if (salePrice > 0 && purchasePrice > 0) {
        // Calculate profit margin based on the actual sale price (excluding tax for exclusive)
        double actualSalePrice =
            selectedTaxType == 'Exclusive' ? salePrice / (1 + (taxRate / 100)) : salePrice;

        double profitMargin = ((actualSalePrice - purchasePrice) / purchasePrice) * 100;
        marginController.text = profitMargin.toStringAsFixed(2);
        adjustSalesPrices();
      }
    } else if (from == 'margin') {
      if (purchasePrice > 0) {
        adjustSalesPrices();
      }
    } else if (from == 'purchase') {
      if (purchasePrice > 0) {
        adjustSalesPrices();
      }
    }
  }

  void adjustSalesPrices() {
    double purchasePrice =
        double.tryParse(productPurchasePriceController.text.replaceAll(',', '')) ?? 0;
    double margin = double.tryParse(marginController.text) ?? 0;
    double taxRate = selectedGroupTaxModel != null
        ? double.parse(selectedGroupTaxModel!.taxRate.toString())
        : 0.0;

    if (purchasePrice <= 0) return;

    // Calculate base price without tax first
    double basePrice = purchasePrice + (purchasePrice * margin / 100);

    double taxAmount = basePrice * (taxRate / 100);

    if (selectedTaxType == 'Inclusive') {
      // For inclusive tax: Price includes tax
      double priceWithTax = basePrice;
      double priceWithoutTax = priceWithTax / (1 + (taxRate / 100));

      mrpText = priceWithTax.toStringAsFixed(2);
      incTaxAmount = priceWithTax.toStringAsFixed(2);
      excTaxAmount = priceWithoutTax.toStringAsFixed(2);

      productSalePriceController.text = mrpText;
    } else {
      // For exclusive tax: Price doesn't include tax, tax is added separately
      double priceWithoutTax = basePrice;
      double priceWithTax = basePrice + taxAmount;

      mrpText = priceWithTax.toStringAsFixed(2);
      incTaxAmount = priceWithTax.toStringAsFixed(2);
      excTaxAmount = priceWithoutTax.toStringAsFixed(2);

      // Update sale price controller with the total price (including tax)
      productSalePriceController.text = mrpText;
    }

    // Update wholesale and dealer prices to match MRP
    productWholesalePriceController.text = mrpText;
    productDealerPriceController.text = mrpText;

    // Update tax display fields
    incTaxController.text = incTaxAmount;
    excTaxController.text = excTaxAmount;

    setState(() {});
  }

  void scheduleCalculation({String? from}) {
    _calculationTimer?.cancel();
    _calculationTimer = Timer(_calculationDelay, () {
      calculatePurchaseAndMrp(from: from);
    });
  }

  @override
  void initState() {
    super.initState();

    // Use delayed calculation instead of immediate listeners
    productPurchasePriceController.addListener(() {
      scheduleCalculation(from: 'purchase');
    });

    productSalePriceController.addListener(() {
      scheduleCalculation(from: 'mrp');
    });

    marginController.addListener(() {
      scheduleCalculation(from: 'margin');
    });

    Future.delayed(const Duration(milliseconds: 100), () {
      if (mounted) adjustSalesPrices();
    });
  }

  @override
  void dispose() {
    _calculationTimer?.cancel();
    productPurchasePriceController.dispose();
    productSalePriceController.dispose();
    totalAmountController.dispose();
    marginController.dispose();
    productDealerPriceController.dispose();
    productWholesalePriceController.dispose();
    incTaxController.dispose();
    excTaxController.dispose();
    super.dispose();
  }

  Future<void> uploadFile(String filePath) async {
    File file = File(filePath);
    try {
      EasyLoading.show(
        status: '${lang.S.of(context).Uploading}... ',
        dismissOnTap: false,
      );
      var snapshot = await FirebaseStorage.instance
          .ref('Product Picture/${DateTime.now().millisecondsSinceEpoch}')
          .putFile(file);
      var url = await snapshot.ref.getDownloadURL();

      setState(() {
        productPicture = url.toString();
      });
    } on firebase_core.FirebaseException catch (e) {
      EasyLoading.dismiss();
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(e.code.toString())));
    }
  }

  final TextEditingController productPurchasePriceController = TextEditingController();
  final TextEditingController productSalePriceController = TextEditingController();
  final TextEditingController productWholesalePriceController = TextEditingController();
  final TextEditingController productDealerPriceController = TextEditingController();
  final TextEditingController stockController = TextEditingController();
  TextEditingController totalAmountController = TextEditingController();
  TextEditingController incTaxController = TextEditingController();
  TextEditingController excTaxController = TextEditingController();
  TextEditingController marginController = TextEditingController();

  String mrpText = '';
  String purchaseText = '';
  String wholesaleText = '';
  String dealerText = '';
  TextEditingController productNameController = TextEditingController();
  TextEditingController productDescriptionController = TextEditingController();

  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    if (newValue.text.isEmpty) {
      return newValue.copyWith(text: '');
    }

    final cleanText = newValue.text.replaceAll(',', ''); // Remove commas
    final formattedText = _formatWithCommas(cleanText);

    return newValue.copyWith(
      text: formattedText,
      selection: TextSelection.collapsed(offset: formattedText.length),
    );
  }

  String _formatWithCommas(String text) {
    final intValue = int.tryParse(text);
    if (intValue == null) {
      return text; // Invalid input, don't format
    }

    return myFormat.format(intValue);
  }

  String _formatNumber(String s) => myFormat.format(int.parse(s));
  TextEditingController expireDateTextEditingController = TextEditingController();
  TextEditingController manufactureDateTextEditingController = TextEditingController();

  String? expireDate;
  String? manufactureDate;

//_____________________Warehouse_list____________________________________________________________________
  late WareHouseModel? selectedWareHouse;

  int i = 0;

  DropdownButton<WareHouseModel> getName({required List<WareHouseModel> list}) {
    List<DropdownMenuItem<WareHouseModel>> dropDownItems = [
      // DropdownMenuItem(
      //   enabled: false,
      //   value: ar,
      //   child: Text(ar.warehouseName),
      // )
    ];
    for (var element in list) {
      dropDownItems.add(DropdownMenuItem(
        value: element,
        child: SizedBox(
          width: 110,
          child: Text(
            element.warehouseName,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ));
      if (i == 0) {
        selectedWareHouse = element;
      }
      i++;
    }
    return DropdownButton(
      isExpanded: true,
      items: dropDownItems,
      value: selectedWareHouse,
      onChanged: (value) {
        setState(() {
          selectedWareHouse = value!;
        });
      },
    );
  }

  bool checkProductName({required String name, required String id}) {
    for (var element in widget.warehouseBasedProductModel) {
      print('name: ${element.productName}, id: ${element.productID}');
      if (element.productName.toLowerCase() == name.toLowerCase() && element.productID == id) {
        return false;
      }
    }

    return true;
  }

  bool isIncludeTax = true;

  GroupTaxModel? selectedGroupTaxModel;

  //___________________________________tax_type____________________________________
  List<String> get status => [
        // lang.S.current.inclusive,
        'Inclusive',
        // lang.S.current.exclusive,
        'Exclusive',
      ];

  late String selectedTaxType = status.first;

  DropdownButton<String> getTaxType() {
    List<DropdownMenuItem<String>> dropDownItems = [];
    for (String des in status) {
      var item = DropdownMenuItem(
        value: des,
        child: Text(des),
      );
      dropDownItems.add(item);
    }
    return DropdownButton(
        isExpanded: true,
        hint: const Text('Select Tax type'),
        //hint:  Text(lang.S.of(context).selectTaxType),
        items: dropDownItems,
        value: selectedTaxType,
        onChanged: (value) {
          setState(() {
            selectedTaxType = value!;
          });
          // Immediately recalculate prices when tax type changes
          adjustSalesPrices();
        });
  }

  //___________________________________calculate_total_with_tax____________________
  double totalAmount = 0.0;

  void calculateTotal() {
    String purchaseAmountText = productPurchasePriceController.text;
    double purchaseAmount = double.tryParse(purchaseAmountText) ?? 0.0;

    if (selectedGroupTaxModel != null) {
      double taxRate = double.parse(selectedGroupTaxModel!.taxRate.toString());

      if (selectedTaxType == 'Inclusive') {
        // For inclusive tax, total amount is the purchase price
        totalAmountController.text = purchaseAmount.toStringAsFixed(2);
      } else {
        // For exclusive tax, add tax to purchase price
        double taxAmount = purchaseAmount * (taxRate / 100);
        double totalAmount = purchaseAmount + taxAmount;
        totalAmountController.text = totalAmount.toStringAsFixed(2);
      }

      totalAmount = double.parse(totalAmountController.text);
    }
  }

  double calculateTotalAmount(double saleAmount, double taxRate) {
    double taxDecimal = taxRate / 100;
    double totalAmount = saleAmount + (saleAmount * taxDecimal);
    return totalAmount;
  }

  double calculateAmountFromPercentage(double percentage, double price) {
    return price * (percentage / 100);
  }

  String excTaxAmount = '';
  String incTaxAmount = '';

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Scaffold(
      backgroundColor: kMainColor,
      appBar: AppBar(
        backgroundColor: kMainColor,
        iconTheme: const IconThemeData(color: Colors.white),
        title: Text(
          lang.S.of(context).addNewProduct,
          style: GoogleFonts.poppins(
            color: Colors.white,
            fontWeight: FontWeight.bold,
          ),
        ),
        actions: [
          IconButton(
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => ExcelUploader(
                              previousProductCode: widget.productCodeList,
                              previousProductName: widget.productNameList,
                            )));
              },
              style: ButtonStyle(
                  backgroundColor: MaterialStatePropertyAll(kMainColor.withValues(alpha: 0.2))),
              icon: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    lang.S.of(context).bulkUpload,
                    //'Bulk\nUpload',
                    textAlign: TextAlign.center,
                    style: const TextStyle(fontSize: 8, color: Colors.white),
                  ),
                  const SizedBox(
                    width: 3,
                  ),
                  const Image(
                    height: 25,
                    width: 25,
                    image: AssetImage('images/excel-file.png'),
                  ),
                  // Icon(Icons.upload,),
                ],
              ))
        ],
        centerTitle: true,
        elevation: 0,
      ),
      body: Container(
        alignment: Alignment.topCenter,
        decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius:
                BorderRadius.only(topRight: Radius.circular(30), topLeft: Radius.circular(30))),
        child: Consumer(builder: (context, ref, __) {
          final wareHouseList = ref.watch(warehouseProvider);
          final groupTax = ref.watch(groupTaxProvider);
          return SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.only(left: 10.0, right: 10.0, top: 10),
              child: Form(
                key: globalKey,
                child: Column(
                  children: [
                    ///________Name__________________________________________
                    Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: TextFormField(
                        decoration: InputDecoration(
                          floatingLabelBehavior: FloatingLabelBehavior.always,
                          border: const OutlineInputBorder(),
                          labelText: lang.S.of(context).productName,
                          hintText: lang.S.of(context).enterProductName,
                        ),
                        validator: (value) {
                          if (value?.removeAllWhiteSpace().toLowerCase().isEmptyOrNull ?? true) {
                            //return 'Product name is required.';
                            return '${lang.S.of(context).productNameIsRequired}.';
                          } else if (!checkProductName(name: value!, id: selectedWareHouse!.id)) {
                            //return 'Product Name already exists in this warehouse.';
                            return '${lang.S.of(context).productNameAlreadyExistsInThisWarehouse}.';
                          } else {
                            return null; // Validation passes
                          }
                        },
                        // validator: (value) {
                        //   if (value.isEmptyOrNull) {
                        //     return 'Product name is required.';
                        //   } else if (widget.productNameList.contains(value?.toLowerCase().removeAllWhiteSpace())) {
                        //     return 'Product name is already added.';
                        //   }
                        //   return null;
                        // },
                        controller: productNameController,
                        onSaved: (value) {
                          productNameController.text = value!;
                        },
                        // onSaved: (value) {
                        //   productName = value!;
                        // },
                      ),
                    ),

                    ///______category___________________________
                    Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: TextFormField(
                        readOnly: true,
                        onTap: () async {
                          data = await const CategoryList().launch(context);
                          setState(() {
                            productCategory = data.categoryName;
                            productCategoryHint = data.categoryName;
                          });
                        },
                        decoration: InputDecoration(
                          floatingLabelBehavior: FloatingLabelBehavior.always,
                          hintText: productCategoryHint,
                          labelText: lang.S.of(context).category,
                          border: const OutlineInputBorder(),
                          suffixIcon: const Icon(Icons.keyboard_arrow_down),
                        ),
                      ),
                    ),

                    ///_____SIZE & Color__________________________
                    Row(
                      children: [
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              onSaved: (value) {
                                size = value!;
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).size,
                                hintText: lang.S.of(context).enterSize,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ).visible(data.variations.contains('Size')),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              onSaved: (value) {
                                color = value!;
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).color,
                                hintText: lang.S.of(context).enterColor,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ).visible(data.variations.contains('Color')),
                      ],
                    ),

                    ///_______Weight & Capacity & Type_____________________________
                    Row(
                      children: [
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              onSaved: (value) {
                                weight = value!;
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).weight,
                                hintText: lang.S.of(context).enterWeight,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ).visible(data.variations.contains('Weight')),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              onSaved: (value) {
                                capacity = value!;
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).capacity,
                                hintText: lang.S.of(context).enterCapacity,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ).visible(data.variations.contains('Capacity')),
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: TextFormField(
                        onSaved: (value) {
                          type = value!;
                        },
                        decoration: InputDecoration(
                          floatingLabelBehavior: FloatingLabelBehavior.always,
                          labelText: lang.S.of(context).type,
                          hintText: lang.S.of(context).enterType,
                          border: const OutlineInputBorder(),
                        ),
                      ),
                    ).visible(data.variations.contains('Type')),

                    ///___________Brand___________________________________
                    Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: TextFormField(
                        readOnly: true,
                        onTap: () async {
                          String data = await const BrandsList().launch(context);
                          setState(() {
                            brandName = data;
                            brandNameHint = data;
                          });
                        },
                        decoration: InputDecoration(
                          floatingLabelBehavior: FloatingLabelBehavior.always,
                          hintText: brandNameHint,
                          labelText: lang.S.of(context).brand,
                          border: const OutlineInputBorder(),
                          suffixIcon: const Icon(Icons.keyboard_arrow_down),
                        ),
                      ),
                    ),

                    ///_________product_code_______________________________
                    Row(
                      children: [
                        Expanded(
                          flex: 3,
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              controller: productCodeController,
                              validator: (value) {
                                if (value.isEmptyOrNull) {
                                  //return 'Product Code is Required';
                                  return lang.S.of(context).productCodeIsRequired;
                                } else if (widget.productCodeList
                                    .contains(value?.toLowerCase().removeAllWhiteSpace())) {
                                  ///return 'This Product Already added!';
                                  return '${lang.S.of(context).thisProductAlreadyAdded}!';
                                }
                                return null;
                              },
                              onSaved: (value) {
                                productCode = value!;
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).productCode,
                                hintText: lang.S.of(context).enterProductCodeOrScan,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          flex: 1,
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: GestureDetector(
                              onTap: () async {
                                await showDialog(
                                  context: context,
                                  useSafeArea: true,
                                  builder: (context1) {
                                    MobileScannerController controller = MobileScannerController(
                                      torchEnabled: false,
                                      returnImage: false,
                                    );
                                    return Container(
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadiusDirectional.circular(6.0),
                                      ),
                                      child: Column(
                                        children: [
                                          AppBar(
                                            backgroundColor: Colors.transparent,
                                            iconTheme: const IconThemeData(color: Colors.white),
                                            leading: IconButton(
                                              icon: const Icon(Icons.arrow_back),
                                              onPressed: () {
                                                Navigator.pop(context1);
                                              },
                                            ),
                                          ),
                                          Expanded(
                                            child: MobileScanner(
                                              fit: BoxFit.contain,
                                              controller: controller,
                                              onDetect: (capture) {
                                                final List<Barcode> barcodes = capture.barcodes;

                                                if (barcodes.isNotEmpty) {
                                                  final Barcode barcode = barcodes.first;
                                                  debugPrint('Barcode found! ${barcode.rawValue}');
                                                  productCode = barcode.rawValue!;
                                                  productCodeController.text = productCode;
                                                  globalKey.currentState!.save();
                                                  Navigator.pop(context1);
                                                }
                                              },
                                            ),
                                          ),
                                        ],
                                      ),
                                    );
                                  },
                                );

                                // await showDialog(
                                //   context: context,
                                //   useSafeArea: true,
                                //   builder: (context1) {
                                //     MobileScannerController controller = MobileScannerController(
                                //       torchEnabled: false,
                                //       returnImage: false,
                                //     );
                                //     return Container(
                                //       decoration: BoxDecoration(borderRadius: BorderRadiusDirectional.circular(6.0)),
                                //       child: MobileScanner(
                                //         fit: BoxFit.contain,
                                //         controller: controller,
                                //         onDetect: (capture) {
                                //           final List<Barcode> barcodes = capture.barcodes;
                                //
                                //           if (barcodes.isNotEmpty) {
                                //             final Barcode barcode = barcodes.first;
                                //             debugPrint('Barcode found! ${barcode.rawValue}');
                                //             productCode = barcode.rawValue!;
                                //             productCodeController.text = productCode;
                                //             globalKey.currentState!.save();
                                //             Navigator.pop(context1);
                                //           }
                                //         },
                                //       ),
                                //     );
                                //   },
                                // );
                              },
                              child: Container(
                                height: 60.0,
                                width: 100.0,
                                padding: const EdgeInsets.all(5.0),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(8.0),
                                  border: Border.all(color: kGreyTextColor),
                                ),
                                child: const Image(
                                  image: AssetImage('images/barcode.png'),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),

                    ///_______stock & unit______________________
                    Row(
                      children: [
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              inputFormatters: [
                                FilteringTextInputFormatter.allow(RegExp(r'^[1-9][0-9]*$')),
                                // Custom formatter for Indian
                                // number format
                              ],
                              controller: stockController,
                              keyboardType: TextInputType.number,
                              validator: (value) {
                                if (value.isEmptyOrNull) {
                                  return lang.S.of(context).stockIsRequired;
                                }
                                return null;
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).stocks,
                                hintText: lang.S.of(context).enterStocks,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              readOnly: true,
                              onTap: () async {
                                String data = await const UnitList().launch(context);
                                setState(() {
                                  productUnit = data;
                                  productUnitHint = data;
                                });
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                hintText: productUnitHint,
                                labelText: lang.S.of(context).units,
                                border: const OutlineInputBorder(),
                                suffixIcon: const Icon(Icons.keyboard_arrow_down),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                    //---------------serial number--------------------
                    Column(
                      children: [
                        ///_________product_serial____________________________________________
                        Row(
                          children: [
                            Theme(
                              data: ThemeData(
                                  checkboxTheme: const CheckboxThemeData(
                                      side: BorderSide(
                                color: kBorderColor,
                              ))),
                              child: Checkbox(
                                  value: isSerialNumberTaken,
                                  onChanged: (value) {
                                    setState(() {
                                      isSerialNumberTaken = value!;
                                    });
                                  }),
                            ),
                            Flexible(
                              child: Text.rich(TextSpan(
                                  text: 'Do you want to add a ',
                                  style: theme.textTheme.bodyLarge,
                                  children: [
                                    TextSpan(
                                        text: 'serial number?',
                                        style: theme.textTheme.titleMedium?.copyWith(
                                          color: kMainColor,
                                        ))
                                  ])),
                            ),
                          ],
                        ),

                        ///____________serial_add_system_____________________________________________
                        Column(children: [
                          Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              validator: (value) {
                                return null;
                              },
                              controller: productSerialNumberController,
                              showCursor: true,
                              cursorColor: kTitleColor,
                              onFieldSubmitted: (value) {
                                if (isSerialNumberUnique(
                                    allList: productSerialNumberList, newSerial: value)) {
                                  setState(() {
                                    productSerialNumberList.add(value);
                                  });
                                  productSerialNumberController.clear();
                                } else {
                                  EasyLoading.showError('Serial number already added!');
                                }
                              },
                              keyboardType: TextInputType.name,
                              decoration: kInputDecoration.copyWith(
                                labelText: 'Serial Number',
                                hintText: 'Enter Serial Number',
                                suffixIcon: Container(
                                  width: 60,
                                  height: 20,
                                  margin: EdgeInsets.only(top: 4, bottom: 4, right: 8),
                                  child: ElevatedButton(
                                    style: ElevatedButton.styleFrom(
                                      padding: EdgeInsets.zero,
                                      backgroundColor: kMainColor.withValues(alpha: 0.2),
                                      minimumSize: Size(40, 20),
                                      shape: RoundedRectangleBorder(
                                        borderRadius: BorderRadius.circular(8),
                                      ),
                                      elevation: 0,
                                    ),
                                    onPressed: () {
                                      if (productSerialNumberController.text.isEmpty ||
                                          productSerialNumberController.text.trim().isEmpty) {
                                        EasyLoading.showError('Please enter a serial number!');
                                        return;
                                      }

                                      if (isSerialNumberUnique(
                                          allList: productSerialNumberList,
                                          newSerial: productSerialNumberController.text)) {
                                        setState(() {
                                          productSerialNumberList
                                              .add(productSerialNumberController.text);
                                        });
                                        productSerialNumberController.clear();
                                      } else {
                                        EasyLoading.showError('Serial number already added!');
                                      }
                                    },
                                    child: Text(
                                      lang.S.of(context).add,
                                      style: theme.textTheme.titleSmall?.copyWith(
                                        fontWeight: FontWeight.w600,
                                        color: kMainColor,
                                      ),
                                    ),
                                  ).visible(isSerialNumberTaken),
                                ),
                              ),
                            ),
                          ).visible(isSerialNumberTaken),
                          Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: Container(
                              // width: context.width() < 1280 ? 200 : 400,
                              // width: 400,
                              //height: 48,
                              decoration: BoxDecoration(
                                border: Border.all(width: 1, color: kBorderColor),
                                borderRadius: const BorderRadius.all(Radius.circular(8)),
                              ),
                              child: GridView.builder(
                                shrinkWrap: true,
                                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                                  crossAxisCount: 2,
                                  childAspectRatio: 3.5,
                                  crossAxisSpacing: .5,
                                  mainAxisSpacing: 0.5,
                                ),
                                physics: NeverScrollableScrollPhysics(),
                                padding: EdgeInsets.zero,
                                itemCount: productSerialNumberList.length,
                                itemBuilder: (BuildContext context, int index) {
                                  if (productSerialNumberList.isNotEmpty) {
                                    return Padding(
                                      padding: const EdgeInsets.all(5.0),
                                      child: Container(
                                        padding: const EdgeInsets.all(8),
                                        color: kBorderColorTextField,
                                        height: 80,
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: [
                                            SizedBox(
                                              width: 100,
                                              height: 40,
                                              child: Text(
                                                productSerialNumberList[index],
                                                maxLines: 2,
                                                overflow: TextOverflow.ellipsis,
                                              ),
                                            ),
                                            GestureDetector(
                                              onTap: () {
                                                setState(() {
                                                  productSerialNumberList.removeAt(index);
                                                });
                                              },
                                              child: const Icon(
                                                Icons.close,
                                                color: Colors.red,
                                                size: 15,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    );
                                  } else {
                                    return const Text('No Serial Number Found');
                                  }
                                },
                              ),
                            ),
                          ).visible(isSerialNumberTaken),
                        ]),
                      ],
                    ),

                    Row(
                      children: [
                        Expanded(
                            child: Padding(
                          padding: const EdgeInsets.all(10.0),
                          child: TextFormField(
                            keyboardType: TextInputType.number,
                            onSaved: (value) {
                              productDiscount = value!;
                            },
                            decoration: InputDecoration(
                              floatingLabelBehavior: FloatingLabelBehavior.always,
                              labelText: lang.S.of(context).discount,
                              hintText: lang.S.of(context).enterDiscount,
                              border: const OutlineInputBorder(),
                            ),
                          ),
                        )).visible(false),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              onSaved: (value) {
                                productManufacturer = value!;
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).menufeturer,
                                hintText: lang.S.of(context).enterManufacturer,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ),
                        wareHouseList.when(
                          data: (warehouse) {
                            List<WareHouseModel> wareHouseList = warehouse;
                            // List<WareHouseModel> wareHouseList = [];
                            return Expanded(
                              child: Padding(
                                padding: const EdgeInsets.all(10.0),
                                child: FormField(
                                  builder: (FormFieldState<dynamic> field) {
                                    return InputDecorator(
                                      decoration: InputDecoration(
                                          enabledBorder: const OutlineInputBorder(
                                            borderRadius: BorderRadius.all(Radius.circular(8.0)),
                                            borderSide:
                                                BorderSide(color: kBorderColorTextField, width: 2),
                                          ),
                                          contentPadding: const EdgeInsets.all(8.0),
                                          floatingLabelBehavior: FloatingLabelBehavior.always,
                                          //labelText: 'Warehouse'
                                          labelText: lang.S.of(context).warehouse),
                                      child: DropdownButtonHideUnderline(
                                        child: getName(list: warehouse ?? []),
                                      ),
                                    );
                                  },
                                ),
                              ),
                            );
                          },
                          error: (e, stack) {
                            return Center(
                              child: Text(
                                e.toString(),
                              ),
                            );
                          },
                          loading: () {
                            return const Center(
                              child: CircularProgressIndicator(),
                            );
                          },
                        ),
                      ],
                    ),
                    //______________________________________________________________Tax________________
                    Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: Row(
                        children: [
                          groupTax.when(
                            data: (groupTax) {
                              // List<WareHouseModel> wareHouseList = [];
                              return Expanded(
                                child: FormField(
                                  builder: (FormFieldState<dynamic> field) {
                                    return InputDecorator(
                                      decoration: InputDecoration(
                                        enabledBorder: const OutlineInputBorder(
                                          borderRadius: BorderRadius.all(Radius.circular(8.0)),
                                          borderSide:
                                              BorderSide(color: kBorderColorTextField, width: 2),
                                        ),
                                        contentPadding: const EdgeInsets.all(8.0),
                                        floatingLabelBehavior: FloatingLabelBehavior.always,
                                        //labelText: 'Applicable Tax'
                                        labelText: lang.S.of(context).applicableTax,
                                      ),
                                      child: DropdownButtonHideUnderline(
                                        child: DropdownButton<GroupTaxModel>(
                                            //hint: const Text('Select Tax'),
                                            hint: Text(lang.S.of(context).selectTax),
                                            items: groupTax.map((e) {
                                              return DropdownMenuItem<GroupTaxModel>(
                                                value: e,
                                                child: Text(e.name),
                                              );
                                            }).toList(),
                                            value: selectedGroupTaxModel,
                                            onChanged: (value) {
                                              setState(() {
                                                selectedGroupTaxModel = value;
                                              });
                                              // Immediately recalculate prices when tax changes
                                              adjustSalesPrices();
                                            }),
                                      ),
                                    );
                                  },
                                ),
                              );
                            },
                            error: (e, stack) {
                              return Center(
                                child: Text(
                                  e.toString(),
                                ),
                              );
                            },
                            loading: () {
                              return const Center(
                                child: CircularProgressIndicator(),
                              );
                            },
                          ),
                          const SizedBox(
                            width: 20.0,
                          ),
                          Expanded(
                            child: FormField(
                              builder: (FormFieldState<dynamic> field) {
                                return InputDecorator(
                                  decoration: InputDecoration(
                                      enabledBorder: const OutlineInputBorder(
                                        borderRadius: BorderRadius.all(Radius.circular(8.0)),
                                        borderSide:
                                            BorderSide(color: kBorderColorTextField, width: 2),
                                      ),
                                      contentPadding: const EdgeInsets.all(8.0),
                                      floatingLabelBehavior: FloatingLabelBehavior.always,
                                      // labelText: 'Tax Type'
                                      labelText: lang.S.of(context).taxType),
                                  child: DropdownButtonHideUnderline(
                                    child: getTaxType(),
                                  ),
                                );
                              },
                            ),
                          )
                          // Checkbox(
                          //   value: isIncludeTax,
                          //   onChanged: (value) {
                          //     setState(() {
                          //       isIncludeTax = value!;
                          //     });
                          //   },
                          // ),
                          // Text('Included Tax'),
                        ],
                      ),
                    ),

                    //______________________________________________________________Tax_Amount________________
                    Row(
                      children: [
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              controller: marginController,
                              keyboardType: TextInputType.numberWithOptions(decimal: true),
                              onEditingComplete: () {
                                final value =
                                    double.tryParse(productPurchasePriceController.text) ?? 0;
                                productPurchasePriceController.text =
                                    value == 0 ? '' : value.toStringAsFixed(2);
                                calculatePurchaseAndMrp(from: 'margin');
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).margin,
                                hintText: 'Enter your margin',
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ),
                        Visibility(
                          visible: selectedTaxType == 'Inclusive',
                          child: Expanded(
                            child: Padding(
                              padding: const EdgeInsets.all(10.0),
                              child: TextFormField(
                                readOnly: true,
                                controller: incTaxController,
                                keyboardType: TextInputType.number,
                                decoration: InputDecoration(
                                  floatingLabelBehavior: FloatingLabelBehavior.always,
                                  //labelText: 'Inc. tax:',
                                  labelText: '${lang.S.of(context).incTax}:',
                                  hintText: '0',
                                  border: const OutlineInputBorder(),
                                ),
                              ),
                            ),
                          ),
                        ),
                        Visibility(
                          visible: selectedTaxType == 'Exclusive',
                          child: Expanded(
                            child: Padding(
                              padding: const EdgeInsets.all(10.0),
                              child: TextFormField(
                                controller: excTaxController,
                                readOnly: true,
                                keyboardType: TextInputType.number,
                                decoration: InputDecoration(
                                  floatingLabelBehavior: FloatingLabelBehavior.always,
                                  //labelText: 'Exc. Tax',
                                  labelText: " ${lang.S.of(context).excTax}:",
                                  hintText: '0',
                                  border: OutlineInputBorder(),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),

                    ///__________purchase & sale price_______________________________
                    Row(
                      children: [
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              controller: productPurchasePriceController,
                              keyboardType: TextInputType.numberWithOptions(decimal: true),
                              onSaved: (value) {
                                purchaseText = value?.replaceAll(',', '') ?? '0';
                              },
                              onEditingComplete: () {
                                final value =
                                    double.tryParse(productPurchasePriceController.text) ?? 0;
                                productPurchasePriceController.text =
                                    value == 0 ? '' : value.toStringAsFixed(2);
                                calculatePurchaseAndMrp(from: 'purchase');
                              },
                              onChanged: (value) {
                                purchaseText = value.replaceAll(',', '');
                                adjustSalesPrices();
                                var formattedText = myFormat.format(int.parse(purchaseText));
                                productPurchasePriceController.value =
                                    productPurchasePriceController.value.copyWith(
                                  text: formattedText,
                                  selection: TextSelection.collapsed(offset: formattedText.length),
                                );
                              },
                              validator: (value) {
                                if (value.isEmptyOrNull) {
                                  //return 'Purchase Price is required';
                                  return lang.S.of(context).purchasePriceIsRequired;
                                }
                                return null;
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).purchasePrice,
                                hintText: lang.S.of(context).enterPurchasePrice,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              controller: productSalePriceController,
                              keyboardType: TextInputType.numberWithOptions(decimal: true),
                              onSaved: (value) {
                                mrpText = value?.replaceAll(',', '') ?? '0';
                              },
                              onEditingComplete: () {
                                final value =
                                    double.tryParse(productSalePriceController.text) ?? 0;
                                productSalePriceController.text =
                                    value == 0 ? '' : value.toStringAsFixed(2);
                                calculatePurchaseAndMrp(from: 'mrp');
                              },
                              validator: (value) {
                                if (value.isEmptyOrNull) {
                                  //return 'MRP is required';
                                  return lang.S.of(context).MRPIsRequired;
                                }
                                return null;
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).MRP,
                                hintText: lang.S.of(context).enterPurchasePrice,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),

                    ///___________wholeSale_DealerPrice____________________________
                    Row(
                      children: [
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              controller: productWholesalePriceController,
                              keyboardType: TextInputType.number,
                              onSaved: (value) {
                                wholesaleText = value?.replaceAll(',', '') ?? '0';
                              },
                              onChanged: (value) {
                                wholesaleText = value.replaceAll(',', '');
                                var formattedText = myFormat.format(int.parse(wholesaleText));
                                productWholesalePriceController.value =
                                    productWholesalePriceController.value.copyWith(
                                  text: formattedText,
                                  selection: TextSelection.collapsed(offset: formattedText.length),
                                );
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).wholeSalePrice,
                                hintText: lang.S.of(context).enterWholeSalePrice,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: TextFormField(
                              controller: productDealerPriceController,
                              keyboardType: TextInputType.number,
                              onChanged: (value) {
                                dealerText = value.replaceAll(',', '');
                                var formattedText = myFormat.format(int.parse(dealerText));
                                productDealerPriceController.value =
                                    productDealerPriceController.value.copyWith(
                                  text: formattedText,
                                  selection: TextSelection.collapsed(offset: formattedText.length),
                                );
                              },
                              onSaved: (value) {
                                dealerText = value?.replaceAll(',', '') ?? '0';
                              },
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                labelText: lang.S.of(context).dealerPrice,
                                hintText: lang.S.of(context).enterDealerPrice,
                                border: const OutlineInputBorder(),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),

                    ///______________ExpireDate______________________
                    Row(
                      children: [
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: AppTextField(
                              textFieldType: TextFieldType.NAME,
                              readOnly: true,
                              validator: (value) {
                                return null;
                              },
                              controller: manufactureDateTextEditingController,
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                //labelText: "Manufacture Date",
                                labelText: lang.S.of(context).manufactureDate,
                                border: const OutlineInputBorder(),
                                suffixIcon: IconButton(
                                  onPressed: () async {
                                    final DateTime? picked = await showDatePicker(
                                      initialDate: DateTime.now(),
                                      firstDate: DateTime(2015, 8),
                                      lastDate: DateTime(2101),
                                      context: context,
                                    );
                                    setState(() {
                                      picked != null
                                          ? manufactureDateTextEditingController.text =
                                              DateFormat.yMMMd().format(picked)
                                          : null;
                                      picked != null ? manufactureDate = picked.toString() : null;
                                    });
                                  },
                                  icon: const Icon(FeatherIcons.calendar),
                                ),
                              ),
                            ),
                          ),
                        ),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(10.0),
                            child: AppTextField(
                              textFieldType: TextFieldType.NAME,
                              readOnly: true,
                              validator: (value) {
                                return null;
                              },
                              controller: expireDateTextEditingController,
                              decoration: InputDecoration(
                                floatingLabelBehavior: FloatingLabelBehavior.always,
                                // labelText: 'Expire Date',
                                labelText: lang.S.of(context).expireDate,
                                border: const OutlineInputBorder(),
                                suffixIcon: IconButton(
                                  onPressed: () async {
                                    final DateTime? picked = await showDatePicker(
                                      initialDate: DateTime.now(),
                                      firstDate: DateTime(2015, 8),
                                      lastDate: DateTime(2101),
                                      context: context,
                                    );
                                    setState(() {
                                      picked != null
                                          ? expireDateTextEditingController.text =
                                              DateFormat.yMMMd().format(picked)
                                          : null;
                                      picked != null ? expireDate = picked.toString() : null;
                                    });
                                  },
                                  icon: const Icon(FeatherIcons.calendar),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),

                    ///_______Lower_stock___________________________
                    Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: TextFormField(
                        initialValue: lowerStockAlert.toString(),
                        onSaved: (value) {
                          lowerStockAlert = int.tryParse(value ?? '') ?? 5;
                        },
                        decoration: InputDecoration(
                          floatingLabelBehavior: FloatingLabelBehavior.always,
                          //labelText: 'Low Stock Alert',
                          labelText: lang.S.of(context).lowStockAlert,
                          //hintText: 'Enter Low Stock Alert Quantity',
                          hintText: lang.S.of(context).enterLowStockAlertQuantity,
                          border: const OutlineInputBorder(),
                        ),
                        inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                      ),
                    ),

                    ///---------product description--------------------
                    Padding(
                      padding: const EdgeInsets.all(10.0),
                      child: TextFormField(
                        maxLines: 3,
                        keyboardType: TextInputType.multiline,
                        decoration: InputDecoration(
                          floatingLabelBehavior: FloatingLabelBehavior.always,
                          border: const OutlineInputBorder(),
                          //labelText: 'Description',
                          labelText: lang.S.of(context).describtion,
                          // hintText: 'Enter Description',
                          hintText: lang.S.of(context).enterDescription,
                        ),
                        // validator: (value) {
                        //   if (value?.removeAllWhiteSpace().toLowerCase().isEmptyOrNull ?? true) {
                        //     return 'Product name is required.';
                        //   } else if (!checkProductName(name: value!, id: selectedWareHouse!.id)) {
                        //     return 'Product Name already exists in this warehouse.';
                        //   } else {
                        //     return null; // Validation passes
                        //   }
                        // },
                        controller: productDescriptionController,
                        onSaved: (value) {
                          productDescriptionController.text = value!;
                        },
                        // onSaved: (value) {
                        //   productName = value!;
                        // },
                      ),
                    ),

                    Column(
                      children: [
                        const SizedBox(height: 10),
                        GestureDetector(
                          onTap: () {
                            showDialog(
                                context: context,
                                builder: (BuildContext context) {
                                  return Dialog(
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(12.0),
                                    ),
                                    // ignore: sized_box_for_whitespace
                                    child: Container(
                                      height: 200.0,
                                      width: MediaQuery.of(context).size.width - 80,
                                      child: Center(
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.center,
                                          children: [
                                            GestureDetector(
                                              onTap: () async {
                                                pickedImage = await _picker.pickImage(
                                                    source: ImageSource.gallery);

                                                setState(() {
                                                  imageFile = File(pickedImage!.path);
                                                  imagePath = pickedImage!.path;
                                                });

                                                Future.delayed(const Duration(milliseconds: 100),
                                                    () {
                                                  Navigator.pop(context);
                                                });
                                              },
                                              child: Column(
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                children: [
                                                  const Icon(
                                                    Icons.photo_library_rounded,
                                                    size: 60.0,
                                                    color: kMainColor,
                                                  ),
                                                  Text(
                                                    lang.S.of(context).gallary,
                                                    style: GoogleFonts.poppins(
                                                      fontSize: 20.0,
                                                      color: kMainColor,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ),
                                            const SizedBox(
                                              width: 40.0,
                                            ),
                                            GestureDetector(
                                              onTap: () async {
                                                pickedImage = await _picker.pickImage(
                                                    source: ImageSource.camera);
                                                setState(() {
                                                  imageFile = File(pickedImage!.path);
                                                  imagePath = pickedImage!.path;
                                                });
                                                Future.delayed(const Duration(milliseconds: 100),
                                                    () {
                                                  Navigator.pop(context);
                                                });
                                              },
                                              child: Column(
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                children: [
                                                  const Icon(
                                                    Icons.camera,
                                                    size: 60.0,
                                                    color: kGreyTextColor,
                                                  ),
                                                  Text(
                                                    lang.S.of(context).camera,
                                                    style: GoogleFonts.poppins(
                                                      fontSize: 20.0,
                                                      color: kGreyTextColor,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  );
                                });
                          },
                          child: Stack(
                            children: [
                              Container(
                                height: 120,
                                width: 120,
                                decoration: BoxDecoration(
                                  border: Border.all(color: Colors.black54, width: 1),
                                  borderRadius: const BorderRadius.all(Radius.circular(120)),
                                  image: imagePath == 'No Data'
                                      ? DecorationImage(
                                          image: NetworkImage(productPicture),
                                          fit: BoxFit.cover,
                                        )
                                      : DecorationImage(
                                          image: FileImage(imageFile),
                                          fit: BoxFit.cover,
                                        ),
                                ),
                              ),
                              Container(
                                height: 120,
                                width: 120,
                                decoration: BoxDecoration(
                                  border: Border.all(color: Colors.black54, width: 1),
                                  borderRadius: const BorderRadius.all(Radius.circular(120)),
                                  image: DecorationImage(
                                    image: FileImage(imageFile),
                                    fit: BoxFit.cover,
                                  ),
                                ),
                                // child: imageFile.path == 'No File' ? null : Image.file(imageFile),
                              ),
                              Positioned(
                                bottom: 0,
                                right: 0,
                                child: Container(
                                  height: 35,
                                  width: 35,
                                  decoration: BoxDecoration(
                                    border: Border.all(color: Colors.white, width: 2),
                                    borderRadius: const BorderRadius.all(Radius.circular(120)),
                                    color: kMainColor,
                                  ),
                                  child: const Icon(
                                    Icons.camera_alt_outlined,
                                    size: 20,
                                    color: Colors.white,
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                        const SizedBox(height: 10),
                      ],
                    ),
                    ButtonGlobal(
                      buttontext: lang.S.of(context).saveAndPublish,
                      buttonDecoration: kButtonDecoration.copyWith(
                          color: kMainColor,
                          borderRadius: const BorderRadius.all(Radius.circular(30))),
                      onPressed: saleButtonClicked
                          ? () {}
                          : () async {
                              if (!isDemo) {
                                bool allowed = await SubscriptionService.checkLimit(
                                  type: SubscriptionLimitType.products,
                                  context: context,
                                );
                                if (allowed) {
                                  if (validateAndSave()) {
                                    try {
                                      setState(() {
                                        saleButtonClicked = true;
                                      });
                                      EasyLoading.show(
                                          status: '${lang.S.of(context).loading}...',
                                          dismissOnTap: false);
                                      bool result = await InternetConnection().hasInternetAccess;

                                      result
                                          ? imagePath == 'No Data'
                                              ? null
                                              : await uploadFile(imagePath)
                                          : null;
                                      final DatabaseReference productInformationRef =
                                          FirebaseDatabase.instance
                                              .ref()
                                              .child(constUserId)
                                              .child('Products');
                                      productInformationRef.keepSynced(true);
                                      ProductModel productModel = ProductModel(
                                        productName: productNameController.text,
                                        productCategory: productCategory,
                                        productDescription: productDescriptionController.text,
                                        size: size,
                                        color: color,
                                        weight: weight,
                                        capacity: capacity,
                                        type: type,
                                        brandName: brandName,
                                        productCode: productCode,
                                        productStock: stockController.text,
                                        productUnit: productUnit,
                                        productSalePrice: mrpText,
                                        // Use the saved variable
                                        productPurchasePrice: purchaseText,
                                        // Use the saved variable
                                        productDiscount: productDiscount,
                                        productWholeSalePrice: wholesaleText,
                                        // Use the saved variable
                                        productDealerPrice: dealerText,
                                        // Use the saved variable
                                        productManufacturer: productManufacturer,
                                        warehouseName: selectedWareHouse!.warehouseName,
                                        warehouseId: selectedWareHouse!.id,
                                        productPicture: productPicture,
                                        expiringDate: expireDate,
                                        manufacturingDate: manufactureDate,
                                        lowerStockAlert: lowerStockAlert,
                                        serialNumber: productSerialNumberList,
                                        taxType: selectedTaxType,
                                        margin: num.tryParse(
                                                marginController.text.replaceAll(',', '')) ??
                                            0,
                                        excTax:
                                            num.tryParse(excTaxAmount.replaceAll(',', '')) ?? 0,
                                        incTax:
                                            num.tryParse(incTaxAmount.replaceAll(',', '')) ?? 0,
                                        groupTaxName: selectedGroupTaxModel?.name ?? '',
                                        groupTaxRate: selectedGroupTaxModel?.taxRate ?? 0,
                                        subTaxes: selectedGroupTaxModel?.subTaxes ?? [],
                                      );
                                      print(productModel.toJson());
                                      productInformationRef.push().set(productModel.toJson());
                                      Subscription.decreaseSubscriptionLimits(
                                          itemType: 'products', context: context);
                                      EasyLoading.dismiss();
                                      ref.refresh(categoryProvider);
                                      ref.refresh(brandsProvider);
                                      ref.refresh(productProvider);
                                      Navigator.pop(context, true);

                                      // Future.delayed(const Duration(milliseconds: 100), () {
                                      //   const ProductList().launch(context, isNewTask: true);
                                      // });
                                    } catch (e) {
                                      setState(() {
                                        saleButtonClicked = false;
                                      });
                                      EasyLoading.dismiss();
                                      ScaffoldMessenger.of(context)
                                          .showSnackBar(SnackBar(content: Text(e.toString())));
                                    }
                                  }
                                }
                              } else {
                                EasyLoading.showError(demoText);
                              }
                            },
                      iconWidget: null,
                      iconColor: Colors.white,
                    ),
                    const SizedBox(height: 20),
                  ],
                ),
              ),
            ),
          );
        }),
      ),
    );
  }

  String _cleanNumberInput(String input) {
    return input.replaceAll(',', '').replaceAll(RegExp(r'[^\d.]'), '');
  }

  bool validateAndSave() {
    final form = globalKey.currentState;
    if (form!.validate()) {
      form.save();
      return true;
    }
    return false;
  }
}

bool isSerialNumberUnique({required List<String> allList, required String newSerial}) {
  for (var element in allList) {
    if (element.toLowerCase().removeAllWhiteSpace() ==
        newSerial.toLowerCase().removeAllWhiteSpace()) {
      return false;
    }
  }
  return true;
}
