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/my_team/get_employee_subordinates_list.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:url_launcher/url_launcher.dart';

class TeamMembers extends StatefulWidget {
  final String? selectedEmp;

  const TeamMembers({this.selectedEmp, Key? key}) : super(key: key);

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

class _TeamMembersState extends State<TeamMembers> {
  String searchEmpEmail = "";
  String searchEmpName = "";
  String searchEmpNo = "";
  String? empId;

  List<GetEmployeeSubordinatesList> getEmployeeSubordinatesList = [];
  GetEmployeeSubordinatesList? getEmployeeSubordinates;

  void initState() {
    super.initState();
    // employeeSubordinates();
  }

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

  @override
  Widget build(BuildContext context) {
    if (getEmployeeSubordinates == null) {
      getEmployeeSubordinates ??= ModalRoute.of(context)?.settings.arguments as GetEmployeeSubordinatesList;
      employeeSubordinates();
    }
    return Scaffold(
      appBar: AppBarWidget(
        context,
        title: LocaleKeys.teamMembers.tr(),
      ),
      backgroundColor: MyColors.backgroundColor,
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: Column(
          children: <Widget>[
            getEmployeeSubordinatesList.isEmpty
                ? Container(margin: const EdgeInsets.only(top: 100), child: Utils.getNoDataWidget(context))
                : ListView.separated(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    padding: const EdgeInsets.only(top: 12),
                    separatorBuilder: (BuildContext cxt, int index) => 12.height,
                    itemCount: getEmployeeSubordinatesList.length,
                    itemBuilder: (BuildContext context, int index) {
                      var phoneNumber = Uri.parse('tel:${getEmployeeSubordinatesList[index].eMPLOYEEMOBILENUMBER}');
                      return InkWell(
                        onTap: () async {
                          Navigator.pushNamed(context, AppRoutes.employeeDetails, arguments: getEmployeeSubordinatesList[index]);
                        },
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            getEmployeeSubordinatesList[index].eMPLOYEEIMAGE == null
                                ? SvgPicture.asset(
                                    "assets/images/user.svg",
                                    height: 34,
                                    width: 34,
                                  ).paddingOnly(top: 4)
                                : Container(
                                    height: 34,
                                    width: 34,
                                    child: CircleAvatar(
                                      radius: 25,
                                      backgroundImage: MemoryImage(Utils.dataFromBase64String(getEmployeeSubordinatesList[index].eMPLOYEEIMAGE!)),
                                      backgroundColor: Colors.black,
                                    ).paddingOnly(top: 4),
                                  ),
                            9.width,
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                // "Present".toText13(color: MyColors.greenColor),
                                "${getEmployeeSubordinatesList[index].eMPLOYEENAME}".toText16(color: MyColors.grey3AColor),
                                "${getEmployeeSubordinatesList[index].pOSITIONNAME}".toText10(color: MyColors.grey57Color),
                              ],
                            ).expanded,
                            Column(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.end,
                              children: [
                                getEmployeeSubordinatesList[index].eMPLOYEEMOBILENUMBER == ""
                                    ? SvgPicture.asset("assets/images/call-disable.svg", width: 22, height: 22)
                                    : InkWell(
                                        onTap: () {
                                          launchUrl(phoneNumber);
                                        },
                                        child: SvgPicture.asset("assets/images/call.svg", width: 22, height: 22),
                                      ),
                                8.height,
                                const Icon(
                                  Icons.arrow_forward_outlined,
                                  color: MyColors.grey3AColor,
                                  size: 18,
                                ),
                              ],
                            ),
                          ],
                        ).paddingOnly(top: 13, bottom: 13, right: 12, left: 14).objectContainerView(radius: 10, disablePadding: true),
                      ).paddingOnly(left: 16.0, right: 16.0);
                    }),
          ],
        ),
      ),
    );
  }
}
