import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:mohem_flutter_app/api/my_team/my_team_api_client.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/dashboard/menu_entries.dart';
import 'package:mohem_flutter_app/models/dashboard/menus.dart';
import 'package:mohem_flutter_app/models/my_team/get_employee_subordinates_list.dart';
import 'package:mohem_flutter_app/ui/my_attendance/dynamic_screens/dynamic_listview_screen.dart';
import 'package:mohem_flutter_app/ui/my_attendance/services_menu_list_screen.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';

class CreateRequest extends StatefulWidget {
  const CreateRequest({Key? key}) : super(key: key);

  @override
  _CreateRequestState createState() => _CreateRequestState();
}

class _CreateRequestState extends State<CreateRequest> {
  String searchEmpEmail = "";
  String searchEmpName = "";
  String searchEmpNo = "";
  String? empId;
  List<GetEmployeeSubordinatesList> getEmployeeSubordinatesList = [];

//  late DashboardProviderModel data;
  List<GetMenuEntriesList> getMenuEntriesList = [];
  GetEmployeeSubordinatesList? getEmployeeSubordinates;
  List<Menus>? homeMenus;

  @override
  void initState() {
    super.initState();
    employeeSubRequest();
  }

  void employeeSubRequest() async {
    try {
      Utils.showLoading(context);
      getEmployeeSubordinatesList = await MyTeamApiClient().getEmployeeSubordinates(searchEmpEmail.toString(), searchEmpName.toString(), searchEmpNo.toString());
      getMenuEntriesList = await MyTeamApiClient().employeeSubordinatesRequest(getEmployeeSubordinates?.eMPLOYEENUMBER);
      homeMenus = parseMenus(getMenuEntriesList);
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  List<Menus> parseMenus(List<GetMenuEntriesList> getMenuEntriesList) {
    List<Menus> menus = [];
    for (int i = 0; i < getMenuEntriesList.length; i++) {
      if (getMenuEntriesList[i].parentMenuName!.isEmpty) {
        menus.add(Menus(getMenuEntriesList[i], getMenuEntriesList.where((element) => getMenuEntriesList[i].menuName == element.parentMenuName).toList()));
      }
    }
    return menus;
  }

  void handleOnPress(context, Menus menu) {
    if (menu.menuEntry.menuEntryType == "FUNCTION") {
      if (menu.menuEntry.requestType == "EIT") {
        Navigator.pushNamed(context, AppRoutes.dynamicScreen, arguments: DynamicListViewParams(menu.menuEntry.prompt!, menu.menuEntry.functionName!));
      } else {}
    } else {
      Navigator.pushNamed(context, AppRoutes.servicesMenuListScreen, arguments: ServicesMenuListScreenParams(menu.menuEntry.prompt!, menu.menuEntiesList));
    }
    return;
  }

  @override
  Widget build(BuildContext context) {
    getEmployeeSubordinates ??= ModalRoute.of(context)?.settings.arguments as GetEmployeeSubordinatesList;

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(
        context,
        title: LocaleKeys.createRequest.tr(),
      ),
      body: SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: homeMenus == null
            ? const SizedBox()
            : (homeMenus!.isEmpty
                ? Utils.getNoDataWidget(context)
                : ListView.separated(
                    padding: const EdgeInsets.all(21),
                    itemBuilder: (cxt, index) => itemView("assets/images/pdf.svg", homeMenus![index].menuEntry.prompt!, index).onPress(() {
                          handleOnPress(context, homeMenus![index]);
                        }),
                    separatorBuilder: (cxt, index) => 12.height,
                    itemCount: homeMenus!.length)),
      ),
    );
  }

  Widget itemView(String icon, String title, index) {
    return Row(
      children: [
        (title).toText16().expanded,
        12.width,
        SvgPicture.asset(
          "assets/images/arrow_next.svg",
          color: MyColors.darkIconColor,
        )
      ],
    ).objectContainerView();
  }
}
