import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/Notifiers/financeProvider/DashboardProvider.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:generp/screens/screensExports.dart';
import 'package:provider/provider.dart';

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

  @override
  State<Financedashboard> createState() => _FinancedashboardState();
}

class _FinancedashboardState extends State<Financedashboard> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var provider = Provider.of<Dashboardprovider>(context, listen: false);
      provider.DashboardPagesAPIFunction(context);
      provider.addFormfinanceFormAccessPagesAPIFunction(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Dashboardprovider>(
      builder: (context, provider, child) {
        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            backgroundColor: AppColors.scaffold_bg_color,
            appBar: appbar(context, "Finance"),
            body: SafeArea(
              child: SizedBox(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      ListView.builder(
                        itemCount: provider.accessiblePagesList.length,
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                        itemBuilder: (context, index) {
                          Widget? SvgIcon;
                          switch (provider.accessiblePagesList[index].mode) {
                            case "apr_lvl1":
                              SvgIcon = SvgPicture.asset(
                                "assets/svg/fin_lv1.svg",
                              );
                              break;
                            case "apr_lvl2":
                              SvgIcon = SvgPicture.asset(
                                "assets/svg/fin_lv2.svg",
                              );
                              break;

                            default:
                              SvgIcon = SvgPicture.asset("assets/svg/fin_ic.svg");
                              break;
                          }
                          return InkResponse(
                            onTap: () async {
                              var navigate;
                              if (provider.accessiblePagesList[index].pageName ==
                                  "Payments List") {
                                navigate = Paymentlistpaymentrequisition(
                                  pageTitleName:
                                      provider
                                          .accessiblePagesList[index]
                                          .pageName!,
                                );
                              } else if (provider
                                      .accessiblePagesList[index]
                                      .pageName ==
                                  "Payment Receipts List") {
                                navigate = Paymentreceiptlist(
                                  pageTitleName:
                                  provider
                                      .accessiblePagesList[index]
                                      .pageName!,
                                );
                              } else {
                                navigate = Allpaymentrequesitionlistsbymodes(
                                  mode: provider.accessiblePagesList[index].mode!,
                                  pageTitleName:
                                      provider
                                          .accessiblePagesList[index]
                                          .pageName!,
                                );
                              }
                              await Navigator.push(
                                context,
                                MaterialPageRoute(builder: (context) => navigate),
                              );
                            },
                            child: Container(
                              margin: EdgeInsets.symmetric(
                                horizontal: 5,
                                vertical: 5,
                              ),
                              padding: EdgeInsets.symmetric(
                                horizontal: 10,
                                vertical: 15,
                              ),
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(14),
                              ),
                              child: Row(
                                children: [
                                  Expanded(flex: 1, child: SvgIcon),
                                  Expanded(
                                    flex: 5,
                                    child: Text(
                                      "${provider.accessiblePagesList[index].pageName}",
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: SvgPicture.asset(
                                      "assets/svg/arrow_right_new.svg",
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          );
                        },
                      ),
                    ],
                  ),
                ),
              ),
            ),
            floatingActionButtonLocation:
                FloatingActionButtonLocation.centerFloat,
            floatingActionButton: InkResponse(
              onTap: () {
                _showPaymentOptionsSheet(context);
              },
              child: Container(
                height: 45,
                alignment: Alignment.center,
                margin: EdgeInsets.symmetric(horizontal: 10),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                decoration: BoxDecoration(
                  color: AppColors.app_blue,
                  borderRadius: BorderRadius.circular(15),
                ),
                child: Text(
                  "Add Payment Request",
                  style: TextStyle(
                    fontSize: 15,
                    fontFamily: "JakartaMedium",
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  Future<void> _showPaymentOptionsSheet(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<Dashboardprovider>(
                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: [
                          SizedBox(height: 15),

                          ...List.generate(provider.accessiblePagesList2.length, (
                            index,
                          ) {
                            print(provider.accessiblePagesList2[index].mode);
                            List<String> mode_lst = [
                              "self",
                              "other",
                              "process",
                              "admin",
                              "direct",
                            ];
                            final Headingnames = [
                              "Add Self Payment Requisition",
                              "Add Other Payment Requisition",
                              "Add Processed Payment Requisition",
                              "Add Admin Payment Requisition",
                              "Add Direct Payment",
                            ];
                            return ListTile(
                              onTap: () async {

                                var res;
                                if (provider
                                        .accessiblePagesList2[index]
                                        .pageName ==
                                    "Add Direct Payment") {
                                  res = await Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder:
                                          (
                                            context,
                                          ) => Directpaymentrequesitionlist(
                                            pageTitleName:
                                                provider
                                                    .accessiblePagesList2[index]
                                                    .pageName!,
                                          ),
                                    ),
                                  );
                                }else if (provider
                                    .accessiblePagesList2[index]
                                    .pageName ==
                                    "Add Payment Receipt") {
                                  res = await Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder:
                                          (
                                          context,
                                          ) => Addpaymentreceiptlist(
                                        pageTitleName:
                                        provider
                                            .accessiblePagesList2[index]
                                            .pageName!,
                                      ),
                                    ),
                                  );
                                } else {
                                  res = await Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder:
                                          (
                                            context,
                                          ) => Submitpaymentrequestionlistsbymode(
                                            mode:
                                                "${provider.accessiblePagesList2[index].mode}",
                                            pageTitleName:
                                                "${provider.accessiblePagesList2[index].pageName}",
                                          ),
                                    ),
                                  );
                                }
                                Navigator.pop(context);
                              },
                              leading: SvgPicture.asset(
                                "assets/svg/fin_ic.svg",
                              ),
                              title: Text(
                                "${provider.accessiblePagesList2[index].pageName}",
                                style: TextStyle(
                                  fontSize: 14,
                                  fontFamily: "JakartaMedium",
                                ),
                              ),
                              trailing: SvgPicture.asset(
                                "assets/svg/arrow_right_new.svg",
                              ),
                            );
                          }),
                        ],
                      ),
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }
}
