import 'dart:io';

import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/Utils/dropdownTheme.dart';
import 'package:generp/screens/notifierExports.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:generp/screens/commom/accountsListDetails.dart';
import 'package:provider/provider.dart';

import 'FileViewer.dart';

class Paymentrequestionlistdetails extends StatefulWidget {
  final paymentRequestId;
  final pageName;
  final mode;

  const Paymentrequestionlistdetails({
    super.key,
    required this.mode,
    required this.paymentRequestId,
    required this.pageName,
  });

  @override
  State<Paymentrequestionlistdetails> createState() =>
      _PaymentrequestionlistdetailsState();
}

class _PaymentrequestionlistdetailsState
    extends State<Paymentrequestionlistdetails> {
  Dropdowntheme ddtheme = Dropdowntheme();
  TextEditingController approvedAmount = TextEditingController();
  TextEditingController remarks = TextEditingController();

  TextEditingController paymentReferenceNumber = TextEditingController();

  FocusNode requestNode = FocusNode();
  FocusNode approveNode = FocusNode();
  FocusNode remarkNode = FocusNode();
  FocusNode proposedPaymentAmountNode = FocusNode();
  FocusNode paymentReferenceNumberNode = FocusNode();
  FocusNode accountNameNode = FocusNode();
  FocusNode branchNode = FocusNode();
  FocusNode requestingPurposeNode = FocusNode();
  FocusNode descriptionNode = FocusNode();
  FocusNode amountNode = FocusNode();
  FocusNode paymentModeNode = FocusNode();
  FocusNode bankUpiIDNode = FocusNode();
  bool isLevel1Finalized = false;

  bool isLevel2Finalized = false;

  // Final decision on whether to show buttons
  bool shouldShowButtons = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var provider = Provider.of<Requesitionlidtdetailsprovider>(
        context,
        listen: false,
      );
      provider.checkDropDownReset();
      provider.paymentRequesitionDetails(context, widget.paymentRequestId);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Requesitionlidtdetailsprovider>(
      builder: (context, provider, child) {
        var req_det = provider.requestsDetails;
        isLevel1Finalized = [
          "Level 1 Approved",
          "Level 1 Rejected",
          "Level 1 approved",
          "Level 1 rejected",
        ].contains(req_det.status);

        isLevel2Finalized = [
          "Level 2 Approved",
          "Level 2 Rejected",
          "Level 2 approved",
          "Level 2 rejected",
        ].contains(req_det.status);
        if (widget.mode == "apr_lvl1") {
          shouldShowButtons = !isLevel1Finalized;
        } else if (widget.mode == "apr_lvl2") {
          shouldShowButtons =
              !isLevel2Finalized &&
              [
                "Requested",
                "Level 1 Approved",
                "Level 1 approved",
              ].contains(req_det.status);
        } else if (widget.mode == "process") {
          shouldShowButtons = [
            "Level 2 Approved",
            "Level 2 approved",
          ].contains(req_det.status); // Show buttons if Level 2 is approved
        }
        return WillPopScope(
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: appbar2(
              context,
              widget.pageName,
              provider.resetAll,
              SizedBox(width: 0),
            ),
            backgroundColor: AppColors.scaffold_bg_color,
            body: SafeArea(
              child: Container(
                child: SingleChildScrollView(
                  child: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(16),
                    ),
                    margin: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                    padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            Expanded(
                              flex: 1,
                              child: Container(
                                height: 50,
                                width: 35,
                                padding: EdgeInsets.all(8.0),
                                decoration: BoxDecoration(
                                  color: Color(0xFFFFF3CE),
                                  borderRadius: BorderRadius.circular(8),
                                ),
                                child: SvgPicture.asset(
                                  "assets/svg/fin_ic.svg",
                                ),
                              ),
                            ),
                            SizedBox(width: 10),
                            Expanded(
                              flex: 4,
                              child: SizedBox(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      req_det.accountName == ""
                                          ? "-"
                                          : req_det.accountName ?? "-",
                                      style: TextStyle(
                                        fontFamily: "JakartaMedium",
                                        fontSize: 14,
                                        color: AppColors.semi_black,
                                      ),
                                    ),
                                    Text(
                                      req_det.amount == ""
                                          ? "-"
                                          : "₹${req_det.amount}",
                                      style: TextStyle(
                                        fontFamily: "JakartaMedium",
                                        fontSize: 14,
                                        color: AppColors.app_blue,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 2,
                              child: Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 5,
                                  vertical: 10,
                                ),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(8),
                                  color: Color(0xFFE3FFE0),
                                ),
                                child: Center(
                                  child: Text(
                                    req_det.status ?? "-",
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                      fontFamily: "JakartaMedium",
                                      fontSize: 14,
                                      color: Color(0xFF0D9C00),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                        Divider(thickness: 0.5, color: Color(0xFFD7D7D7)),
                        ...List.generate(provider.subHeadings.length, (j) {
                          if (provider.Headings[j] == "Attachment" &&
                              req_det.attachmentViewFileName == "") {
                            return SizedBox.shrink();
                          }
                          return Container(
                            padding: EdgeInsets.symmetric(vertical: 7),
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Expanded(
                                  child: Text(
                                    provider.Headings[j],
                                    style: TextStyle(
                                      fontFamily: "JakartaMedium",
                                      fontSize: 14,
                                      color: AppColors.semi_black,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  child: InkResponse(
                                    onTap: () {
                                      if (provider.Headings[j] ==
                                          "Attachment") {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder:
                                                (context) => Fileviewer(
                                                  fileName:
                                                      req_det
                                                          .attachmentViewFileName ??
                                                      "",
                                                  fileUrl:
                                                      req_det
                                                          .attachmentDirFilePath ??
                                                      "",
                                                ),
                                          ),
                                        );
                                      } else if (provider.Headings[j] ==
                                          "Requested Account") {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder:
                                                (context) =>
                                                    Accountslistdetails(
                                                      accountID:
                                                          provider
                                                              .requestsDetails
                                                              .accountId,
                                                    ),
                                          ),
                                        );
                                      }
                                    },
                                    child: Text(
                                      [
                                            "Attachment",
                                          ].contains(provider.Headings[j])
                                          ? "View"
                                          : provider.subHeadings[j] == ""
                                          ? "-"
                                          : provider.subHeadings[j],
                                      style: TextStyle(
                                        fontSize: 14,
                                        color:
                                            [
                                                  "Attachment",
                                                  "Requested Account",
                                                ].contains(provider.Headings[j])
                                                ? AppColors.app_blue
                                                : Color(0xFF818181),
                                        decoration:
                                            [
                                                  "Attachment",
                                                  "Requested Account",
                                                ].contains(provider.Headings[j])
                                                ? TextDecoration.underline
                                                : TextDecoration.none,
                                        decorationColor:
                                            [
                                                  "Attachment",
                                                  "Requested Account",
                                                ].contains(provider.Headings[j])
                                                ? AppColors.app_blue
                                                : AppColors.white,
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          );
                        }),
                      ],
                    ),
                  ),
                ),
              ),
            ),
            floatingActionButton:
                (["admin", "self"].contains(widget.mode) || !shouldShowButtons)
                    ? Container(height: 0)
                    : Container(
                      margin: EdgeInsets.symmetric(horizontal: 10),
                      alignment: Alignment.bottomCenter,
                      height: 60,
                      child: Container(
                        margin: EdgeInsets.only(bottom: 10),
                        alignment: Alignment.center,
                        height: 45,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Expanded(
                              child: InkResponse(
                                onTap: () {
                                  print(provider.requestsDetails.id);
                                  _showLevelRejectionSheet(
                                    context,
                                    provider.requestsDetails.id,
                                  );
                                },
                                child: Container(
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(8),
                                    color: Color(0xFFFFEFEF),
                                    border: Border.all(
                                      color: Color(0xFFED3424),
                                      width: 0.5,
                                    ),
                                  ),
                                  child: Center(
                                    child: Text(
                                      "Reject",
                                      style: TextStyle(
                                        color: Color(0xFFED3424),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            SizedBox(width: 10),
                            if ([
                              "apr_lvl1",
                              "apr_lvl2",
                            ].contains(widget.mode)) ...[
                              Expanded(
                                child: InkResponse(
                                  onTap: () {
                                    provider
                                        .approveRejectPaymentRequestAPIFunction(
                                          context,
                                          provider.requestsDetails.id,
                                        );
                                    _showLevelApprovalSheet(
                                      context,
                                      provider.requestsDetails.id,
                                    );
                                  },
                                  child: Container(
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(8),
                                      color: Color(0xFFE7FFE5),
                                      border: Border.all(
                                        color: Color(0xFF0D9C00),
                                        width: 0.5,
                                      ),
                                    ),
                                    child: Center(
                                      child: Text(
                                        "Approve",
                                        style: TextStyle(
                                          color: Color(0xFF0D9C00),
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ] else if (widget.mode == "process") ...[
                              Expanded(
                                child: InkResponse(
                                  onTap: () {
                                    provider
                                        .approveRejectPaymentRequestAPIFunction(
                                          context,
                                          provider.requestsDetails.id,
                                        );
                                    provider.preValues();
                                    _showAddPaymentSheet(
                                      context,
                                      provider.requestsDetails.id,
                                    );
                                  },
                                  child: Container(
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(8),
                                      color: Color(0xFFE7FFE5),
                                      border: Border.all(
                                        color: Color(0xFF0D9C00),
                                        width: 0.5,
                                      ),
                                    ),
                                    child: Center(
                                      child: Text(
                                        "Add Payment",
                                        style: TextStyle(
                                          color: Color(0xFF0D9C00),
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ],
                        ),
                      ),
                    ),
            floatingActionButtonLocation:
                FloatingActionButtonLocation.centerFloat,
            // bottomNavigationBar:
            //     (["admin", "self"].contains(widget.mode))
            //         ? Container(height: 0)
            //         : ([
            //           "Level 1 Approved",
            //           "Level 1 Rejected",
            //           "Level 1 approved",
            //           "Level 1 rejected",
            //           "Level 2 Approved",
            //           "Level 2 Rejected",
            //           "Level 2 approved",
            //           "Level 2 rejected",
            //         ]).contains(req_det.status)
            //         ? SizedBox(height: 0)
            //         : Container(
            //           margin: EdgeInsets.symmetric(horizontal: 10),
            //           alignment: Alignment.bottomCenter,
            //           height: 60,
            //           child: Container(
            //             margin: EdgeInsets.only(bottom: 10),
            //             alignment: Alignment.center,
            //             height: 45,
            //             child: Row(
            //               mainAxisAlignment: MainAxisAlignment.center,
            //               children: [
            //                 Expanded(
            //                   child: InkResponse(
            //                     onTap: () {
            //                       _showLevelRejectionSheet(context);
            //                     },
            //                     child: Container(
            //                       decoration: BoxDecoration(
            //                         borderRadius: BorderRadius.circular(8),
            //                         color: Color(0xFFFFEFEF),
            //                         border: Border.all(
            //                           color: Color(0xFFED3424),
            //                           width: 0.5,
            //                         ),
            //                       ),
            //                       child: Center(
            //                         child: Text(
            //                           "Reject",
            //                           style: TextStyle(
            //                             color: Color(0xFFED3424),
            //                           ),
            //                         ),
            //                       ),
            //                     ),
            //                   ),
            //                 ),
            //                 SizedBox(width: 10),
            //                 if ([
            //                   "apr_lvl1",
            //                   "apr_lvl2",
            //                 ].contains(widget.mode)) ...[
            //                   Expanded(
            //                     child: InkResponse(
            //                       onTap: () {
            //                         provider
            //                             .approveRejectPaymentRequestAPIFunction(
            //                               context,
            //                               provider.requestsDetails.id,
            //                             );
            //                         _showLevelApprovalSheet(context);
            //                       },
            //                       child: Container(
            //                         decoration: BoxDecoration(
            //                           borderRadius: BorderRadius.circular(8),
            //                           color: Color(0xFFE7FFE5),
            //                           border: Border.all(
            //                             color: Color(0xFF0D9C00),
            //                             width: 0.5,
            //                           ),
            //                         ),
            //                         child: Center(
            //                           child: Text(
            //                             "Approve",
            //                             style: TextStyle(
            //                               color: Color(0xFF0D9C00),
            //                             ),
            //                           ),
            //                         ),
            //                       ),
            //                     ),
            //                   ),
            //                 ] else if (widget.mode == "process") ...[
            //                   Expanded(
            //                     child: InkResponse(
            //                       onTap: () {
            //                         provider
            //                             .approveRejectPaymentRequestAPIFunction(
            //                               context,
            //                               provider.requestsDetails.id,
            //                             );
            //                         _showAddPaymentSheet(context);
            //                       },
            //                       child: Container(
            //                         decoration: BoxDecoration(
            //                           borderRadius: BorderRadius.circular(8),
            //                           color: Color(0xFFE7FFE5),
            //                           border: Border.all(
            //                             color: Color(0xFF0D9C00),
            //                             width: 0.5,
            //                           ),
            //                         ),
            //                         child: Center(
            //                           child: Text(
            //                             "Add Payment",
            //                             style: TextStyle(
            //                               color: Color(0xFF0D9C00),
            //                             ),
            //                           ),
            //                         ),
            //                       ),
            //                     ),
            //                   ),
            //                 ],
            //               ],
            //             ),
            //           ),
            //         ),
          ),
          onWillPop: () {
            provider.resetAll();
            return onBackPressed(context);
          },
        );
      },
    );
  }

  Future<void> _showLevelApprovalSheet(BuildContext context, paymentID) {
    return showModalBottomSheet(
      useSafeArea: true,
      isDismissible: true,
      isScrollControlled: true,
      showDragHandle: true,
      backgroundColor: Colors.white,
      enableDrag: true,
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return SafeArea(
              child: Consumer<Requesitionlidtdetailsprovider>(
                builder: (context, provider, child) {
                  return Container(
                    margin: EdgeInsets.only(
                      bottom: 15,
                      left: 15,
                      right: 15,
                      top: 10,
                    ),
                    padding: EdgeInsets.only(
                      bottom: MediaQuery.of(context).viewInsets.bottom,
                    ),

                    child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          if (widget.mode == "apr_lvl1") ...[
                            Align(
                              alignment: Alignment.topLeft,
                              child: Text(
                                "Level 1 Approval",
                                style: TextStyle(
                                  color: AppColors.app_blue,
                                  fontSize: 16,
                                ),
                              ),
                            ),
                          ] else if (widget.mode == "apr_lvl2") ...[
                            Align(
                              alignment: Alignment.topLeft,
                              child: Text(
                                "Level 2 Approval",
                                style: TextStyle(
                                  color: AppColors.app_blue,
                                  fontSize: 16,
                                ),
                              ),
                            ),
                          ],

                          textControllerReadonlyWidget(
                            context,
                            provider.requestedAmount,
                            "Enter Requested Amount",
                            (p0) {},
                          ),
                          textControllerWidget(
                            context,
                            approvedAmount,
                            "Enter Approved Amount",
                            (p0) {},
                            TextInputType.number,
                            false,
                            FilteringTextInputFormatter.digitsOnly,
                          ),
                          textControllerWidget(
                            context,
                            remarks,
                            "Enter Remarks",
                            (p0) {},
                            TextInputType.text,
                            false,
                            null,
                          ),
                          TextWidget(context, "Proposed Payment Account"),
                          DropdownButtonHideUnderline(
                            child: Row(
                              children: [
                                Expanded(
                                  child: DropdownButton2<PaymentAccounts>(
                                    isExpanded: true,
                                    hint: Text(
                                      'Select Payment Account',
                                      style: TextStyle(fontSize: 14),
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                    items:
                                        provider.paymentsAccounts
                                            .map(
                                              (paymenents) => DropdownMenuItem<
                                                PaymentAccounts
                                              >(
                                                value: paymenents,
                                                child: Text(
                                                  paymenents.name ?? '',
                                                  style: const TextStyle(
                                                    fontSize: 14,
                                                  ),
                                                  overflow:
                                                      TextOverflow.ellipsis,
                                                ),
                                              ),
                                            )
                                            .toList(),
                                    value: provider.selectedPaymentAccounts,
                                    onChanged: (PaymentAccounts? value) {
                                      if (value != null) {
                                        if (provider
                                            .paymentsAccounts
                                            .isNotEmpty) {
                                          provider.selectedPaymentAccounts =
                                              value;
                                          print(
                                            "Selected Complaint Type: ${value.name}, ID: ${value.id}",
                                          );
                                          provider.selectedID = value.id!;
                                          provider.selectedValue = value.name!;
                                          print(
                                            "hfjkshfg" +
                                                provider.selectedID.toString(),
                                          );
                                        }
                                      }
                                    },
                                    dropdownSearchData: DropdownSearchData(
                                      searchInnerWidgetHeight: 50,
                                      searchController:
                                          provider
                                              .paymentAccountSearchController,
                                      searchInnerWidget: Padding(
                                        padding: const EdgeInsets.all(8),
                                        child: TextFormField(
                                          controller:
                                              provider
                                                  .paymentAccountSearchController,
                                          decoration: InputDecoration(
                                            isDense: true,
                                            contentPadding:
                                                const EdgeInsets.symmetric(
                                                  horizontal: 10,
                                                  vertical: 8,
                                                ),
                                            hintText: 'Search account...',
                                            border: OutlineInputBorder(
                                              borderRadius:
                                                  BorderRadius.circular(8),
                                            ),
                                          ),
                                        ),
                                      ),
                                      searchMatchFn: (item, searchValue) {
                                        return item.value?.name
                                                ?.toLowerCase()
                                                .contains(
                                                  searchValue.toLowerCase(),
                                                ) ??
                                            false;
                                      },

                                      // Optional: clear search text when dropdown closes
                                    ),
                                    onMenuStateChange: (isOpen) {
                                      if (!isOpen) {
                                        provider.paymentAccountSearchController
                                            .clear();
                                      }
                                    },
                                    buttonStyleData: ddtheme.buttonStyleData,
                                    iconStyleData: ddtheme.iconStyleData,
                                    menuItemStyleData:
                                        ddtheme.menuItemStyleData,
                                    dropdownStyleData:
                                        ddtheme.dropdownStyleData,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          InkWell(
                            onTap: () {
                              provider
                                  .paymentrequisitionApproveSubmitAPIFunction(
                                    context,
                                    widget.mode,
                                    paymentID,
                                    approvedAmount.text,
                                    remarks.text,
                                    provider.selectedID,
                                  );
                            },
                            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: AppColors.app_blue, //1487C9
                                borderRadius: BorderRadius.circular(14.0),
                              ),
                              child: Center(
                                child: Text(
                                  "Submit",

                                  ///approve
                                  textAlign: TextAlign.center,
                                  style: TextStyle(color: Colors.white),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }

  Future<void> _showLevelRejectionSheet(BuildContext context, paymentID) {
    return showModalBottomSheet(
      useSafeArea: true,
      isDismissible: true,
      isScrollControlled: true,
      showDragHandle: true,
      backgroundColor: Colors.white,
      enableDrag: true,
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return SafeArea(
              child: Consumer<Requesitionlidtdetailsprovider>(
                builder: (context, provider, child) {
                  return Container(
                    margin: EdgeInsets.only(
                      bottom: 15,
                      left: 15,
                      right: 15,
                      top: 10,
                    ),
                    padding: EdgeInsets.only(
                      bottom: MediaQuery.of(context).viewInsets.bottom,
                    ),
                    child: SingleChildScrollView(
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          if (widget.mode == "apr_lvl1") ...[
                            Align(
                              alignment: Alignment.topLeft,
                              child: Text(
                                "Level 1 Rejection",
                                style: TextStyle(
                                  color: AppColors.app_blue,
                                  fontSize: 16,
                                ),
                              ),
                            ),
                          ] else if (widget.mode == "apr_lvl2") ...[
                            Align(
                              alignment: Alignment.topLeft,
                              child: Text(
                                "Level 2 Rejection",
                                style: TextStyle(
                                  color: AppColors.app_blue,
                                  fontSize: 16,
                                ),
                              ),
                            ),
                          ],

                          textControllerReadonlyWidget(
                            context,
                            provider.requestedAmount,
                            "Enter Requested Amount",
                            (p0) {},
                          ),

                          textControllerWidget(
                            context,
                            remarks,
                            "Enter Remarks",
                            (p0) {},
                            TextInputType.text,
                            false,
                            null,
                          ),
                          InkWell(
                            onTap: () {
                              provider
                                  .paymentrequisitionRejectSubmitAPIFunction(
                                    context,
                                    widget.mode,
                                    paymentID,
                                    remarks.text,
                                  );
                            },
                            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: AppColors.app_blue, //1487C9
                                borderRadius: BorderRadius.circular(14.0),
                              ),
                              child: Center(
                                child: Text(
                                  "Submit",

                                  ///reject
                                  textAlign: TextAlign.center,
                                  style: TextStyle(color: Colors.white),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }

  Future<void> _showAddPaymentSheet(BuildContext context, paymentID) {
    return showModalBottomSheet(
      useSafeArea: true,
      isDismissible: true,
      isScrollControlled: true,
      showDragHandle: true,
      backgroundColor: Colors.white,
      enableDrag: true,
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return SafeArea(
              child: Consumer<Requesitionlidtdetailsprovider>(
                builder: (context, provider, child) {
                  return Container(
                    margin: EdgeInsets.only(
                      bottom: 15,
                      left: 15,
                      right: 15,
                      top: 10,
                    ),
                    padding: EdgeInsets.only(
                      bottom: MediaQuery.of(context).viewInsets.bottom,
                    ),

                    child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Align(
                            alignment: Alignment.topLeft,
                            child: Text(
                              "Add Approval",
                              style: TextStyle(
                                color: AppColors.app_blue,
                                fontSize: 16,
                              ),
                            ),
                          ),

                          textControllerReadonlyWidget(
                            context,
                            provider.proposedPaymentAccount,
                            "Enter Proposed Payment Account",
                            (p0) {},
                          ),

                          TextWidget(context, "Payment Account"),
                          DropdownButtonHideUnderline(
                            child: Row(
                              children: [
                                Expanded(
                                  child: DropdownButton2<PaymentAccounts>(
                                    isExpanded: true,
                                    hint: Text(
                                      'Select Account',
                                      style: TextStyle(fontSize: 14),
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                    items:
                                        provider.paymentsAccounts
                                            .map(
                                              (paymenents) => DropdownMenuItem<
                                                PaymentAccounts
                                              >(
                                                value: paymenents,
                                                child: Text(
                                                  paymenents.name ?? '',
                                                  style: const TextStyle(
                                                    fontSize: 14,
                                                  ),
                                                  overflow:
                                                      TextOverflow.ellipsis,
                                                ),
                                              ),
                                            )
                                            .toList(),
                                    value: provider.selectedPaymentAccounts,
                                    onChanged: (PaymentAccounts? value) {
                                      if (value != null) {
                                        if (provider
                                            .paymentsAccounts
                                            .isNotEmpty) {
                                          provider.selectedPaymentAccounts =
                                              value;
                                          print(
                                            "Selected Complaint Type: ${value.name}, ID: ${value.id}",
                                          );
                                          provider.selectedID = value.id!;
                                          provider.selectedValue = value.name!;
                                          print(
                                            "hfjkshfg" +
                                                provider.selectedID.toString(),
                                          );
                                        }
                                      }
                                    },
                                    dropdownSearchData: DropdownSearchData(
                                      searchInnerWidgetHeight: 50,
                                      searchController:
                                      provider
                                          .paymentAccountSearchController,
                                      searchInnerWidget: Padding(
                                        padding: const EdgeInsets.all(8),
                                        child: TextFormField(
                                          controller:
                                          provider
                                              .paymentAccountSearchController,
                                          decoration: InputDecoration(
                                            isDense: true,
                                            contentPadding:
                                            const EdgeInsets.symmetric(
                                              horizontal: 10,
                                              vertical: 8,
                                            ),
                                            hintText: 'Search account...',
                                            border: OutlineInputBorder(
                                              borderRadius:
                                              BorderRadius.circular(8),
                                            ),
                                          ),
                                        ),
                                      ),
                                      searchMatchFn: (item, searchValue) {
                                        return item.value?.name
                                            ?.toLowerCase()
                                            .contains(
                                          searchValue.toLowerCase(),
                                        ) ??
                                            false;
                                      },

                                      // Optional: clear search text when dropdown closes
                                    ),
                                    onMenuStateChange: (isOpen) {
                                      if (!isOpen) {
                                        provider.paymentAccountSearchController
                                            .clear();
                                      }
                                    },
                                    buttonStyleData: ddtheme.buttonStyleData,
                                    iconStyleData: ddtheme.iconStyleData,
                                    menuItemStyleData:
                                        ddtheme.menuItemStyleData,
                                    dropdownStyleData:
                                        ddtheme.dropdownStyleData,
                                  ),
                                ),
                              ],
                            ),
                          ),

                          textControllerWidget(
                            context,
                            paymentReferenceNumber,
                            "Enter Payment Reference Number",
                            (p0) {},
                            TextInputType.text,
                            false,
                            null,
                          ),

                          textControllerWidget(
                            context,
                            remarks,
                            "Enter Remarks",
                            (p0) {},
                            TextInputType.text,
                            false,
                            null,
                          ),

                          InkResponse(
                            onTap: () {
                              _showAttachmentSheet(context);
                            },
                            child: Container(
                              margin: EdgeInsets.symmetric(vertical: 10),
                              height: 45,
                              width: MediaQuery.of(context).size.width,
                              decoration: BoxDecoration(
                                color: Color(0xFFE6F6FF),
                                borderRadius: BorderRadius.circular(12),
                                border: Border.all(
                                  color: AppColors.app_blue,
                                  width: 0.5,
                                ),
                              ),
                              child: Center(
                                child: Text(
                                  "Add Attachment",
                                  style: TextStyle(
                                    fontFamily: "JakartaMedium",
                                    color: AppColors.app_blue,
                                  ),
                                ),
                              ),
                            ),
                          ),
                          if (provider.imagePicked == 1 &&
                              provider.imagePath != null) ...[
                            Padding(
                              padding: const EdgeInsets.symmetric(
                                vertical: 4.0,
                              ),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    "${provider.imagePath}",
                                    style: TextStyle(
                                      color: AppColors.semi_black,
                                      fontSize: 11,
                                      fontWeight: FontWeight.w600,
                                    ),
                                  ),
                                  InkResponse(
                                    onTap: () {
                                      provider.imagePicked = 0;
                                      provider.imagePath = null;
                                      provider.imageFilePath = null;
                                    },
                                    child: SvgPicture.asset(
                                      "assets/svg/ic_close.svg",
                                      width: 15,
                                      height: 15,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ],
                          textControllerReadonlyWidget(
                            context,
                            provider.accountName,
                            "Enter Account Name",
                            (p0) {},
                          ),
                          textControllerReadonlyWidget(
                            context,
                            provider.branch,
                            "Enter Branch Name",
                            (p0) {},
                          ),
                          textControllerReadonlyWidget(
                            context,
                            provider.requestingPurpose,
                            "Enter Requesting Purpose",
                            (p0) {},
                          ),

                          textControllerReadonlyWidget(
                            context,
                            provider.description,
                            "Enter Description",
                            (p0) {},
                          ),

                          textControllerReadonlyWidget(
                            context,
                            provider.amount,
                            "Enter Amount",
                            (p0) {},
                          ),
                          textControllerReadonlyWidget(
                            context,
                            provider.paymentMode,
                            "Enter Payment Mode",
                            (p0) {},
                          ),
                          textControllerReadonlyWidget(
                            context,
                            provider.bankHolderName,
                            "Bank Account Holder Name",
                            (p0) {},
                          ),
                          textControllerReadonlyWidget(
                            context,
                            provider.bankAccountNumber,
                            "Bank Account Number",
                            (p0) {},
                          ),
                          textControllerReadonlyWidget(
                            context,
                            provider.bankName,
                            "Bank Name",
                            (p0) {},
                          ),
                          textControllerReadonlyWidget(
                            context,
                            provider.bankBranchName,
                            "Bank Branch Name",
                            (p0) {},
                          ),

                          textControllerReadonlyWidget(
                            context,
                            provider.bankIfscCode,
                            "Bank IFSC Code",
                            (p0) {},
                          ),
                          textControllerReadonlyWidget(
                            context,
                            provider.bankUpiID,
                            "Bank UPI ID",
                            (p0) {},
                          ),

                          InkWell(
                            onTap: () {
                              provider
                                  .paymentrequisitionProcessSubmitAPIFunction(
                                    context,
                                    widget.mode,
                                    paymentReferenceNumber.text,
                                    paymentID,
                                    provider.selectedID,
                                    remarks.text,
                                    provider.imagePath,
                                  );
                            },
                            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: AppColors.app_blue, //1487C9
                                borderRadius: BorderRadius.circular(14.0),
                              ),
                              child: Center(
                                child: Text(
                                  "Submit",

                                  ///add payment
                                  textAlign: TextAlign.center,
                                  style: TextStyle(color: Colors.white),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }

  Future<void> _showAttachmentSheet(BuildContext context) {
    return showModalBottomSheet(
      useSafeArea: true,
      isDismissible: true,
      isScrollControlled: true,
      showDragHandle: true,
      backgroundColor: Colors.white,
      enableDrag: true,
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return SafeArea(
              child: Consumer<Requesitionlidtdetailsprovider>(
                builder: (context, provider, child) {
                  return 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: 10,
                      ),

                      child: SingleChildScrollView(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Align(
                              alignment: Alignment.center,
                              child: Text(
                                "Select Source",
                                style: TextStyle(
                                  color: AppColors.app_blue,
                                  fontSize: 16,
                                ),
                              ),
                            ),
                            SizedBox(height: 15),
                            InkWell(
                              onTap: () {
                                Navigator.of(context).pop(false);
                                provider.imgFromGallery(context);
                              },
                              child: Container(
                                height: 35,
                                child: Text("Select photo from gallery"),
                              ),
                            ),
                            SizedBox(height: 10),
                            InkWell(
                              onTap: () {
                                Navigator.of(context).pop(false);
                                provider.imgFromCamera(context);
                              },
                              child: Container(
                                height: 35,
                                child: Text("Capture photo from camera"),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }
}
