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

import 'package:generp/screens/screensExports.dart';
import 'package:provider/provider.dart';
import '../../Notifiers/ordersProvider/pagesDashboardProvider.dart';
import '../../Utils/app_colors.dart';
import '../../Utils/commonWidgets.dart';
import 'addTpcAgent.dart';

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

  @override
  State<Ordermoduledashboard> createState() => _OrdermoduledashboardState();
}

class _OrdermoduledashboardState extends State<Ordermoduledashboard> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var provider = Provider.of<Pagesdashboardprovider>(context, listen: false);
      provider.ordersAccessiblePagesAPIFunction(context);
    });
  }
  @override
  Widget build(BuildContext context) {
    return Consumer<Pagesdashboardprovider>(
      builder: (context, provider, child) {
        final pages = provider.ordersAccessiblePagesList;
        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            backgroundColor: AppColors.scaffold_bg_color,
            appBar: appbar(context, "Orders"),
            body: Container(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    ListView.builder(
                      itemCount: pages.length,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemBuilder: (context, index) {
                        Widget? SvgIcon;
                        switch (pages[index].mode) {
                          case "level_one_approval":
                            SvgIcon = SvgPicture.asset(
                              "assets/svg/fin_lv1.svg",
                            );
                            break;
                          case "level_two_approval":
                            case "level_two_rejected":
                            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 (pages[index].pageName!.contains("Add Order") ) {
                              navigate = AddorderScreen(
                                mode: pages[index].mode!,
                                pageTitleName:
                                pages[index]
                                    .pageName!,
                              );
                            }if (pages[index].pageName!.contains("Add Payment") ) {
                              ("navigate to Add Payment");
                              navigate = AddpaymentScreen(
                                mode: pages[index].mode!,
                                pageTitleName:
                                pages[index]
                                    .pageName!,
                              );
                            }
                            else if (pages[index].pageName!.contains("Payments List")) {
                              navigate = Paymentlistsbymode(
                                mode: pages[index].mode!,
                                pageTitleName:
                                pages[index]
                                    .pageName!,
                              );
                            } else if (pages[index].pageName!.contains("Order List")){
                              navigate = OrderslistbyModes(
                                mode: pages[index].mode!,
                                pageTitleName:
                                pages[index]
                                    .pageName!,
                              );
                            }else if(pages[index].pageName=="Add TPC Agent"){
                              navigate = AddtpcagentScreen();
                            }else if(pages[index].pageName=="TPC Agent List (Admin)"){
                              navigate = Tpcagentlistbymode(
                                mode: pages[index].mode!,
                                pageTitleName:
                                pages[index]
                                    .pageName!,
                              );
                            }else if(pages[index].pageName=="Pending TPC Issue List"){
                              navigate = Tpcagentissuelist(
                                mode: pages[index].mode!,
                                pageTitleName:
                                pages[index]
                                    .pageName!,
                              );
                            }
                            if(navigate!=null){
                              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(
                                    "${pages[index].pageName}",
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: SvgPicture.asset(
                                    "assets/svg/arrow_right_new.svg",
                                  ),
                                ),
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
