import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/leave_balance_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/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/leave_balance/get_absence_transaction_list_model.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/balances_dashboard_widget.dart';
import 'package:mohem_flutter_app/widgets/item_detail_view_widget.dart';

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

  @override
  _LeaveBalanceState createState() {
    return _LeaveBalanceState();
  }
}

class _LeaveBalanceState extends State<LeaveBalance> {
  List<GetAbsenceTransactionList>? absenceTransList;

  DateTime accrualDateTime = DateTime.now();

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

    getAbsenceTransactions();
  }

  @override
  void dispose() {
    super.dispose();
  }

  void getAbsenceTransactions() async {
    try {
      Utils.showLoading(context);
      absenceTransList = await LeaveBalanceApiClient().getAbsenceTransactions(-999);
      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.leaveBalance.tr(),
      ),
      body: ListView(
        physics: const BouncingScrollPhysics(),
        padding: const EdgeInsets.all(21),
        children: [
          BalancesDashboardWidget(LocaleKeys.currentLeaveBalance.tr(), true),
          12.height,
          absenceTransList == null
              ? const SizedBox()
              : (absenceTransList!.isEmpty
                  ? Utils.getNoDataWidget(context).paddingOnly(top: 50)
                  : ListView.separated(
                      physics: const NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      padding: EdgeInsets.zero,
                      itemBuilder: (cxt, int index) => Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              ItemDetailGrid(
                                ItemDetailViewCol(LocaleKeys.startDateT.tr(), absenceTransList![index].sTARTDATE ?? ""),
                                ItemDetailViewCol(LocaleKeys.endDateT.tr(), absenceTransList![index].eNDDATE ?? ""),
                              ),
                              ItemDetailGrid(
                                ItemDetailViewCol(LocaleKeys.absenceType.tr(), absenceTransList![index].aBSENCETYPE ?? ""),
                                ItemDetailViewCol(LocaleKeys.absenceCategory.tr(), absenceTransList![index].aBSENCECATEGORY ?? ""),
                              ),
                              ItemDetailGrid(
                                ItemDetailViewCol(LocaleKeys.days.tr(), absenceTransList![index].aBSENCEDAYS?.toString() ?? ""),
                                ItemDetailViewCol(LocaleKeys.hours.tr(), absenceTransList![index].aBSENCEHOURS?.toString() ?? ""),
                              ),
                              ItemDetailGrid(
                                ItemDetailViewCol(LocaleKeys.approvalStatus.tr(), absenceTransList![index].aPPROVALSTATUS ?? ""),
                                ItemDetailViewCol(LocaleKeys.absenceStatus.tr(), absenceTransList![index].aBSENCESTATUS ?? ""),
                                isItLast: true,
                              ),
                            ],
                          ).objectContainerView(),
                      separatorBuilder: (cxt, index) => 12.height,
                      itemCount: absenceTransList!.length)),
        ],
      ),
      floatingActionButton: Container(
        height: 54,
        width: 54,
        decoration: const BoxDecoration(
          shape: BoxShape.circle,
          gradient: LinearGradient(transform: GradientRotation(.83), begin: Alignment.topRight, end: Alignment.bottomLeft, colors: [
            MyColors.gradiantEndColor,
            MyColors.gradiantStartColor,
          ]),
        ),
        child: const Icon(Icons.add, color: Colors.white, size: 30),
      ).onPress(() {
        Navigator.pushNamed(context, AppRoutes.addLeaveBalance);
      }),
    );
  }
}
