import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:mohem_flutter_app/api/mowadhafhi/mowadhafhi_api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.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/mowadhafhi/get_tickets_list.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';

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

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

class _MowadhafhiHomeState extends State<MowadhafhiHome> {
  List<GetTicketsByEmployeeList>? getTicketsByEmployeeList;

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

  void getOpenTickets() async {
    try {
      Utils.showLoading(context);
      getTicketsByEmployeeList?.clear();
      getTicketsByEmployeeList = await MowadhafhiApiClient().getTicketsByEmployee();
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(
        context,
        title: LocaleKeys.mowadhafhiRequest.tr(),
      ),
      body: Column(
        children: [
          (getTicketsByEmployeeList == null
                  ? const SizedBox()
                  : (getTicketsByEmployeeList!.isEmpty)
                      ? Utils.getNoDataWidget(context)
                      : ListView.separated(
                          physics: const BouncingScrollPhysics(),
                          padding: const EdgeInsets.all(21),
                          itemBuilder: (BuildContext context, int index) {
                            return InkWell(
                              onTap: () {
                                Navigator.pushNamed(context, AppRoutes.mowadhafhiDetails, arguments: getTicketsByEmployeeList![index].ticketId);
                              },
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      getTicketsByEmployeeList![index].ticketTypeName!.toText14(color: MyColors.darkTextColor).expanded,
                                      getTicketsByEmployeeList![index].created!.split(" ")[0].toText12(color: MyColors.grey70Color),
                                    ],
                                  ),
                                  getTicketsByEmployeeList![index].description!.toText12(color: MyColors.grey57Color),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      getTicketsByEmployeeList![index].ticketStatusInternalName!.toText14(color: MyColors.textMixColor),
                                      RotatedBox(
                                        quarterTurns: AppState().isArabic(context) ? 2 : 4,
                                        child: SvgPicture.asset("assets/images/arrow_next.svg", color: MyColors.darkIconColor),
                                      ),
                                    ],
                                  ),
                                ],
                              ).objectContainerView(),
                            );
                          },
                          separatorBuilder: (BuildContext context, int index) => 12.height,
                          itemCount: getTicketsByEmployeeList!.length))
              .expanded,
          DefaultButton(LocaleKeys.createRequest.tr(), () async {
            await Navigator.pushNamed(context, AppRoutes.mowadhafhiHRRequest);
            getOpenTickets();
          }).insideContainer
        ],
      ),
    );
  }
}
