import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

import 'package:provider/provider.dart';
import 'package:generp/screens/screensExports.dart';
import '../../Notifiers/crmProvider/crmDashboardProvider.dart';
import '../../Utils/app_colors.dart';
import '../../Utils/commonWidgets.dart';

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

  @override
  State<CrmdashboardScreen> createState() => _CrmdashboardScreenState();
}

class _CrmdashboardScreenState extends State<CrmdashboardScreen> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var prov = Provider.of<Crmdashboardprovider>(context, listen: false);
      prov.accessPagesAPIFunction(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Crmdashboardprovider>(
      builder: (context, provider, child) {
        final gridPages =
            provider.accessPages
                .where((page) => ![8, 9, 23, 24,51,50].contains(page.id))
                .toList();

        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            backgroundColor: AppColors.scaffold_bg_color,
            appBar: appbar(context, "CRM"),
            body: SafeArea(
              child: ListView.builder(
                padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                itemCount: gridPages.length,
                itemBuilder: (context, index) {
                  // final icons = ["comm_ic_1", "comm_ic_2"];
                  Widget? SvgIcon;

                  if (gridPages[index].pageName!.contains("Lead List"))
                  {
                    SvgIcon = SvgPicture.asset("assets/svg/fin_lv1.svg");
                  }
                  else if (gridPages[index].pageName=="Nearby Leads")
                  {
                    SvgIcon =  SvgPicture.asset("assets/svg/home_icons_5.svg",height: 18,width: 18,);
                  }
                  else
                  {
                    SvgIcon = SvgPicture.asset("assets/svg/fin_ic.svg");
                  }
                  return InkResponse(
                    onTap: () {
                      print(gridPages[index].pageName!);
                      if (gridPages[index].pageName!.contains("Lead List")) {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder:
                                (context) => Leadlistbymode(
                                  mode: gridPages[index].mode,
                                  pageTitleName: gridPages[index].pageName,
                                ),
                          ),
                        );
                      } else if (gridPages[index].pageName!.contains("Prospect List")) {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder:
                                (context) => ProspectListByMode(
                              mode: gridPages[index].mode,
                              pageTitleName: gridPages[index].pageName,
                            ),
                          ),
                        );
                      }else if(gridPages[index].pageName=="Nearby Leads"){
                        Navigator.push(context, MaterialPageRoute(builder: (context) => NearbyOpenLeads(),));
                      }
                      // switch (gridPages[index].pageName!) {
                      //   case "Lead List":
                      //     Navigator.push(
                      //       context,
                      //       MaterialPageRoute(builder: (context) => Leadlistbymode()),
                      //     );
                      //     break;
                      //   case "Prospect List":
                      //     // Navigator.push(
                      //     //   context,
                      //     //   MaterialPageRoute(builder: (context) => Accountledger()),
                      //     // );
                      //     break;
                      //
                      //   default:
                      //     print("111");
                      //     break;
                      // }
                    },
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(14),
                      ),
                      margin: EdgeInsets.symmetric(horizontal: 5, vertical: 5),
                      padding: EdgeInsets.symmetric(
                        horizontal: 10,
                        vertical: 15,
                      ),
                      child: Row(
                        children: [
                          Expanded(flex: 1, child: SvgIcon),

                          Expanded(
                            flex: 5,
                            child: Text(
                              "${gridPages[index].pageName}",
                              style: TextStyle(fontSize: 14),
                            ),
                          ),
                          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 Leads and Prospect",
                  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<Crmdashboardprovider>(
                builder: (context, provider, child) {
                  final accessiblePagesList =
                      provider.accessPages
                          .where((page) => [8, 9].contains(page.id))
                          .toList();
                  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(accessiblePagesList.length, (index) {
                            print(accessiblePagesList[index].mode);

                            return ListTile(
                              onTap: () async {
                                Navigator.pop(context);
                                var res;
                              },
                              leading: SvgPicture.asset(
                                "assets/svg/fin_ic.svg",
                              ),
                              title: Text(
                                "${accessiblePagesList[index].pageName}",
                                style: TextStyle(
                                  fontSize: 14,
                                  fontFamily: "JakartaMedium",
                                ),
                              ),
                              trailing: SvgPicture.asset(
                                "assets/svg/arrow_right_new.svg",
                              ),
                            );
                          }),
                        ],
                      ),
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }
}
