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.orderDashboardAPIFunction(context, "executive", "", "", "");
      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: [
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 10,vertical: 5),
                      margin: EdgeInsets.symmetric(horizontal: 0,vertical: 10),
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(16)
                      ),
                      height: MediaQuery.of(context).size.height * 0.28,
                      child: GridView.builder(
                        padding: EdgeInsets.symmetric(horizontal: 0,vertical: 5),
                        itemCount: provider.ordersgain.length,
                        shrinkWrap: true,
                        scrollDirection: Axis.horizontal,
                        physics: AlwaysScrollableScrollPhysics(),
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 2,
                            crossAxisSpacing: 10,
                            mainAxisSpacing: 10,
                            childAspectRatio: 0.65
                        ),
                        itemBuilder: (context, jndex) {
                          final icons = ["comm_ic_1", "comm_ic_2"];
                          final leadTitles = [
                            'Order Gain',
                            'Dispatched',
                            'Pending Tasks',
                            'Quote',
                          ];
                          final colors = [
                            0xFFE7FFE5,
                            0xFFF3EDFF,
                            0xFFFFFCD5,
                            0xFFFFF6F0,
                          ];
                          final textcolors = [
                            0xFF0D9C00,
                            0xFF493272,
                            0xFF605C00,
                            0xFF91481B,
                          ];
                          return InkResponse(
                            onTap: () async {
                              if (provider.ordersgain[jndex].filter!.pageName !=
                                  "") {
                                if (provider.ordersgain[jndex].filter!.pageName!
                                    .contains("Order List")) {
                                  // await Navigator.push(
                                  //   context,
                                  //   MaterialPageRoute(
                                  //     builder:
                                  //         (context) => OrderslistbyModes(
                                  //       pageTitleName:
                                  //       provider
                                  //           .ordersgain[jndex]
                                  //           .filter!
                                  //           .pageName!,
                                  //       mode:
                                  //       provider.ordersgain[jndex].filter!.mode,
                                  //       filter: provider.ordersgain[jndex].filter!,
                                  //     ),
                                  //   ),
                                  // );
                                }
                              }
                            },
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 13),
                              decoration: BoxDecoration(
                                color: Color(colors[jndex]),
                                borderRadius: BorderRadius.circular(12),
                              ),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    provider.ordersgain[jndex].count!,
                                    style: TextStyle(
                                      fontSize: 30,
                                      fontFamily: "JakartaMedium",
                                      color: Color(textcolors[jndex]),
                                    ),
                                  ),
                                  Text(
                                    leadTitles[jndex],
                                    style: TextStyle(color: Color(0xFF818181)),
                                  ),
                                ],
                              ),
                            ),
                          );
                        },
                      ),
                    ),

                    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",
                                  ),
                                ),
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
