import 'package:flutter/material.dart';
import 'package:generp/screens/finance/FileViewer.dart';
import 'package:generp/screens/notifierExports.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonServices.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:provider/provider.dart';

class GeneratorPartDetailsScreen extends StatefulWidget {
  const GeneratorPartDetailsScreen({super.key});

  @override
  State<GeneratorPartDetailsScreen> createState() =>
      _GeneratorPartDetailsScreenState();
}

class _GeneratorPartDetailsScreenState
    extends State<GeneratorPartDetailsScreen> {
  FocusNode descriptionFocusNode = FocusNode();
  FocusNode quantityFocusNode = FocusNode();

  TextEditingController descriptioncontroller = TextEditingController();
  TextEditingController quantitycontroller = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      var homeProvider = Provider.of<HomescreenNotifier>(
        context,
        listen: false,
      );
      initialiseFunction(context, homeProvider);
    });
  }

  void initialiseFunction(BuildContext context, homeProvider) async {
    var inventoryProvider = Provider.of<InventoryProvider>(
      context,
      listen: false,
    );
    inventoryProvider.LoadPartDetailsApifunction(
      homeProvider,
      context,
      "",
      inventoryProvider.partID,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer2<InventoryProvider, HomescreenNotifier>(
      builder: (context, provider, homeProvider, child) {
        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: RefreshIndicator.adaptive(
            onRefresh: () async {
              initialiseFunction(context, homeProvider);
            },
            child: Scaffold(
              resizeToAvoidBottomInset: true,
              backgroundColor: AppColors.scaffold_bg_color,
              appBar: appbar(context, "Gen Inventory"),
              body: SafeArea(
                child: Container(
                  child: SingleChildScrollView(
                    child: Column(
                      children: [
                        Container(
                          margin: EdgeInsets.only(top: 15, bottom: 15),
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(16),
                          ),
                          child: Column(
                            children: [
                              Container(
                                padding: EdgeInsets.only(
                                  left: 10,
                                  right: 10,
                                  top: 15,
                                ),
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: Text(
                                        "${provider.partData.prodName}",
                                        style: TextStyle(
                                          color: AppColors.app_blue,
                                          fontFamily: "JakartaMedium",
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 10,
                                  vertical: 15,
                                ),
                                margin: EdgeInsets.symmetric(
                                  horizontal: 10,
                                  vertical: 15,
                                ),
                                decoration: BoxDecoration(
                                  color: Color(0xFFE6F6FF),
                                  borderRadius: BorderRadius.circular(16),
                                ),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Expanded(
                                      child: SizedBox(
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.center,
                                          children: [
                                            RichText(
                                              text: TextSpan(
                                                children: [
                                                  TextSpan(
                                                    text:
                                                        "${provider.partData.remainingQuantity}",
                                                    style: TextStyle(
                                                      color: AppColors.app_blue,
                                                      fontSize: 30,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ),
                                            Text(
                                              "Remaining Quantity",
                                              style: TextStyle(
                                                color: Color(0xFF818181),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 15,
                                  vertical: 15,
                                ),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Expanded(
                                      child: InkResponse(
                                        onTap: () {
                                          _showStockIssueBottomSheet(
                                            context,
                                            "Issue",
                                          );
                                        },
                                        child: Container(
                                          height: 45,
                                          decoration: BoxDecoration(
                                            color: Color(0xFFFFEFEF),
                                            border: Border.all(
                                              color: Color(0xFFED3424),
                                              width: 0.5,
                                            ),
                                            borderRadius: BorderRadius.circular(
                                              12,
                                            ),
                                          ),
                                          child: Center(
                                            child: Text(
                                              "- Issue",
                                              style: TextStyle(
                                                color: Color(0xFFED3424),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                    ),
                                    SizedBox(width: 10),
                                    Expanded(
                                      child: InkResponse(
                                        onTap: () {
                                          _showStockIssueBottomSheet(
                                            context,
                                            "Recieve",
                                          );
                                        },
                                        child: Container(
                                          height: 45,
                                          decoration: BoxDecoration(
                                            color: Color(0xFFE7FFE5),
                                            border: Border.all(
                                              color: Color(0xFF0D9C00),
                                              width: 0.5,
                                            ),
                                            borderRadius: BorderRadius.circular(
                                              12,
                                            ),
                                          ),
                                          child: Center(
                                            child: Text(
                                              "+ Recieve",
                                              style: TextStyle(
                                                color: Color(0xFF0D9C00),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),

                        Container(
                          alignment: Alignment.topLeft,
                          padding: EdgeInsets.symmetric(horizontal: 10),
                          child: Text(
                            "Product Details",
                            style: TextStyle(
                              color: Color(0xFF818181),
                              fontFamily: "JakartaMedium",
                            ),
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(
                            horizontal: 10,
                            vertical: 10,
                          ),
                          margin: EdgeInsets.symmetric(
                            horizontal: 10,
                            vertical: 10,
                          ),
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(16),
                          ),
                          child: Column(
                            children: List.generate(2, (index) {
                              final headings = [
                                "Product Name",
                                // "Product ID",
                                "Project",
                              ];

                              final values = [
                                provider.partData.prodName,
                                // provider.partData.id,
                                provider.partData.project,
                              ];
                              return Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 10,
                                  vertical: 10,
                                ),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Expanded(
                                      flex: 1,
                                      child: Text(
                                        "${headings[index]}",
                                        style: TextStyle(
                                          fontFamily: "JakartaMedium",
                                        ),
                                      ),
                                    ),
                                    Expanded(
                                      flex: 2,
                                      child: Text(
                                        "${values[index]}",
                                        style: TextStyle(
                                          color: Color(0xFF818181),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }),
                          ),
                        ),

                        Container(
                          alignment: Alignment.topLeft,
                          padding: EdgeInsets.symmetric(horizontal: 10),
                          child: Text(
                            "Vendor Details",
                            style: TextStyle(
                              fontFamily: "JakartaMedium",
                              color: Color(0xFF818181),
                            ),
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(
                            horizontal: 10,
                            vertical: 10,
                          ),
                          margin: EdgeInsets.symmetric(
                            horizontal: 10,
                            vertical: 10,
                          ),
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(16),
                          ),
                          child: Column(
                            children: List.generate(2, (index) {
                              final headings = ["Vendor 1", "Vendor 2"];

                              final values = [
                                provider.partData.vendor1,
                                provider.partData.vendor2,
                              ];
                              return Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 10,
                                  vertical: 10,
                                ),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Expanded(
                                      flex: 1,
                                      child: Text(
                                        "${headings[index]}",
                                        style: TextStyle(
                                          fontFamily: "JakartaMedium",
                                        ),
                                      ),
                                    ),
                                    Expanded(
                                      flex: 2,
                                      child: Text(
                                        "${values[index]}",
                                        style: TextStyle(
                                          color: Color(0xFF818181),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }),
                          ),
                        ),

                        Container(
                          alignment: Alignment.topLeft,
                          padding: EdgeInsets.symmetric(horizontal: 10),
                          child: Text(
                            "Other Details",
                            style: TextStyle(
                              fontFamily: "JakartaMedium",
                              color: Color(0xFF818181),
                            ),
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(
                            horizontal: 10,
                            vertical: 10,
                          ),
                          margin: EdgeInsets.symmetric(
                            horizontal: 10,
                            vertical: 10,
                          ),
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(16),
                          ),
                          child: Column(
                            children: List.generate(5, (index) {
                              final headings = [
                                "Sub Group",
                                "Units",
                                "MSL",
                                "Description",
                                "Image",
                              ];

                              final values = [
                                provider.partData.subGroup,
                                provider.partData.units,
                                provider.partData.msl,
                                provider.partData.prodDesc,
                                "View",
                              ];
                              if(index==4&&provider
                                  .partData
                                  .imageDirFilePath==""){
                                return SizedBox.shrink();
                              }
                              return Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 10,
                                  vertical: 10,
                                ),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Expanded(
                                      flex: 1,
                                      child: Text(
                                        "${headings[index]}",
                                        style: TextStyle(
                                          fontFamily: "JakartaMedium",
                                        ),
                                      ),
                                    ),
                                    if (values[index] == "View") ...[
                                      Expanded(
                                        flex: 2,
                                        child: InkResponse(
                                          onTap: () {
                                            Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                builder:
                                                    (context) => Fileviewer(
                                                      fileName:
                                                          provider
                                                              .partData
                                                              .imageViewFileName!,
                                                      fileUrl:
                                                          provider
                                                              .partData
                                                              .imageDirFilePath!,
                                                    ),
                                              ),
                                            );
                                          },
                                          child: Text(
                                            "${values[index]}",
                                            style: TextStyle(
                                              color: AppColors.app_blue,
                                              decoration:
                                                  TextDecoration.underline,
                                              decorationColor:
                                                  AppColors.app_blue,
                                            ),
                                          ),
                                        ),
                                      ),
                                    ] else ...[
                                      Expanded(
                                        flex: 2,
                                        child: Text(
                                          "${values[index]}",
                                          style: TextStyle(
                                            color: Color(0xFF818181),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ],
                                ),
                              );
                            }),
                          ),
                        ),

                        // Container(
                        //   alignment: Alignment.topLeft,
                        //   padding: EdgeInsets.symmetric(horizontal: 10),
                        //   child: Text(
                        //     "Description",
                        //     style: TextStyle(
                        //       fontFamily: "JakartaMedium",
                        //       color: Color(0xFF818181),
                        //     ),
                        //   ),
                        // ),
                        // Container(
                        //   padding: EdgeInsets.symmetric(
                        //     horizontal: 10,
                        //     vertical: 10,
                        //   ),
                        //   margin: EdgeInsets.symmetric(
                        //     horizontal: 10,
                        //     vertical: 10,
                        //   ),
                        //   decoration: BoxDecoration(
                        //     color: Colors.white,
                        //     borderRadius: BorderRadius.circular(16),
                        //   ),
                        //   child: Container(
                        //     padding: EdgeInsets.symmetric(
                        //       horizontal: 10,
                        //       vertical: 10,
                        //     ),
                        //     child: Row(
                        //       children: [
                        //         Expanded(
                        //           child: SizedBox(
                        //             child: Column(
                        //               crossAxisAlignment: CrossAxisAlignment.start,
                        //               mainAxisAlignment: MainAxisAlignment.start,
                        //               children: [
                        //                 Text(
                        //                   "Description",
                        //                   style: TextStyle(
                        //                     fontFamily: "JakartaMedium",
                        //                   ),
                        //                 ),
                        //                 Text(
                        //
                        //                   style: TextStyle(
                        //                     color: Color(0xFF818181),
                        //                   ),
                        //                 ),
                        //               ],
                        //             ),
                        //           ),
                        //         ),
                        //       ],
                        //     ),
                        //   ),
                        // ),
                        SizedBox(height: 150),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  Future<void> _showStockIssueBottomSheet(BuildContext context, type) {
    return showModalBottomSheet(
      useSafeArea: true,
      isDismissible: true,
      isScrollControlled: true,
      showDragHandle: true,
      useRootNavigator: true,
      enableDrag: true,
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return SafeArea(
              child: Padding(
                padding: EdgeInsets.only(
                  bottom:
                      MediaQuery.of(
                        context,
                      ).viewInsets.bottom, // This handles keyboard
                ),
                child: Container(
                  margin: EdgeInsets.only(
                    bottom: 15,
                    left: 15,
                    right: 15,
                    top: 30,
                  ),

                  child: Consumer2<InventoryProvider, HomescreenNotifier>(
                    builder: (context, provider, homeProvider, child) {
                      return SingleChildScrollView(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text(
                              "Inventory ${type}",
                              style: TextStyle(
                                color: AppColors.app_blue,
                                fontWeight: FontWeight.w600,
                                fontSize: 16,
                              ),
                            ),
                            SizedBox(height: 10),
                            Container(
                              alignment: Alignment.topLeft,
                              child: Text(
                                "Quantity",
                                style: TextStyle(
                                  color: AppColors.semi_black,
                                  fontSize: 14,
                                ),
                              ),
                            ),
                            SizedBox(height: 5),
                            Container(
                              height: 48,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                color: AppColors.text_field_color,
                                borderRadius: BorderRadius.circular(20),
                                border:
                                    quantityFocusNode.hasFocus
                                        ? Border.all(
                                          color: AppColors.app_blue,
                                          width: 0.5,
                                        )
                                        : null,
                              ),
                              //  alignment: Alignment.center,
                              margin: EdgeInsets.only(left: 5.0, right: 5.0),
                              child: Padding(
                                padding: const EdgeInsets.fromLTRB(
                                  10.0,
                                  0.0,
                                  15,
                                  0,
                                ),
                                child: TextField(
                                  controller: quantitycontroller,
                                  keyboardType: TextInputType.numberWithOptions(
                                    decimal: true,
                                    signed: true,
                                  ),
                                  focusNode: quantityFocusNode,
                                  style: TextStyle(fontSize: 14),

                                  onChanged: (value) {
                                    provider.updateQuantity(
                                      quantitycontroller.text,
                                    );
                                  },
                                  onTapOutside: (event) {
                                    // Handle onTapOutside
                                    FocusScope.of(context).unfocus();
                                  },
                                  decoration: InputDecoration(
                                    isDense: true,
                                    hintStyle: TextStyle(
                                      color: Color(0xFF818181),
                                      fontSize: 14,
                                    ),

                                    //contentPadding: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 10.0),
                                    enabledBorder: InputBorder.none,
                                    focusedBorder: InputBorder.none,
                                    hintText: 'Enter Quantity',
                                  ),
                                ),
                              ),
                            ),
                            SizedBox(height: 10),
                            Container(
                              alignment: Alignment.topLeft,
                              child: Text(
                                "Description",
                                style: TextStyle(
                                  color: AppColors.semi_black,
                                  fontSize: 14,
                                ),
                              ),
                            ),
                            SizedBox(height: 5),
                            Container(
                              height: 180,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                color: AppColors.text_field_color,
                                borderRadius: BorderRadius.circular(20),
                                border:
                                    descriptionFocusNode.hasFocus
                                        ? Border.all(
                                          color: AppColors.app_blue,
                                          width: 0.5,
                                        )
                                        : null,
                              ),
                              //  alignment: Alignment.center,
                              margin: EdgeInsets.only(left: 5.0, right: 5.0),
                              child: Padding(
                                padding: const EdgeInsets.fromLTRB(
                                  10.0,
                                  0.0,
                                  15,
                                  0,
                                ),
                                child: TextField(
                                  controller: descriptioncontroller,
                                  maxLines: 100,
                                  keyboardType: TextInputType.text,
                                  focusNode: descriptionFocusNode,
                                  style: TextStyle(fontSize: 14),
                                  onChanged: (value) {
                                    provider.updateQuantity(
                                      descriptioncontroller.text,
                                    );
                                  },
                                  onTapOutside: (event) {
                                    // Handle onTapOutside
                                    FocusScope.of(context).unfocus();
                                  },
                                  decoration: InputDecoration(
                                    isDense: true,
                                    hintStyle: TextStyle(
                                      color: Color(0xFF818181),
                                      fontSize: 14,
                                    ),
                                    //contentPadding: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 10.0),
                                    enabledBorder: InputBorder.none,
                                    focusedBorder: InputBorder.none,
                                    hintText: 'Enter Description',
                                  ),
                                ),
                              ),
                            ),
                            InkWell(
                              onTap: () {
                                print(provider.isButtonEnabled);
                                if (type == "Recieve") {
                                  provider.issuetype = "Recieved";
                                } else {
                                  provider.issuetype = "Issued";
                                }

                                provider.StockRecieveIssueAPI(
                                  homeProvider,
                                  context,
                                  quantitycontroller.text,
                                  descriptioncontroller.text,
                                  provider.partID,
                                  provider.issuetype,
                                );
                                quantitycontroller.clear();
                                descriptioncontroller.clear();
                              },

                              child: Container(
                                alignment: Alignment.center,
                                height: 45,
                                margin: EdgeInsets.only(
                                  left: 5.0,
                                  right: 5.0,
                                  top: 5.0,
                                  bottom: 5.0,
                                ),
                                decoration: BoxDecoration(
                                  color:
                                      quantitycontroller.text.isEmpty ||
                                              descriptioncontroller.text.isEmpty
                                          ? AppColors.button_disabled
                                          : AppColors.app_blue, //1487C9
                                  borderRadius: BorderRadius.circular(12.0),
                                ),
                                child: Center(
                                  child: Text(
                                    "Submit",
                                    textAlign: TextAlign.center,
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      );
                    },
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }
}
