import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/pending_transactions_api_client.dart';
import 'package:mohem_flutter_app/classes/utils.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/pending_transactions/get_pending_transactions_details.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/item_detail_view_widget.dart';

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

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

class _PendingTransactionsDetailsState extends State<PendingTransactionsDetails> {
  String functionID = "";
  String dateFrom = "";
  String dateTo = "";

  List<GetPendingTransactionsDetails>? getPendingTransactionsDetails;

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

  void getFunctionID() {
    if (functionID == "") {
      Map arguments = (ModalRoute.of(context)?.settings.arguments ?? <String, dynamic>{}) as Map;
      functionID = arguments["selectedFunctionID"].toString();
      dateFrom = arguments["dateFrom"];
      dateTo = arguments["dateTo"];
      getTicketTransactions();
    }
  }

  @override
  Widget build(BuildContext context) {
    getFunctionID();
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(
        context,
        title: LocaleKeys.pendingTransactions.tr(),
      ),
      body: getPendingTransactionsDetails == null
          ? const SizedBox()
          : (getPendingTransactionsDetails!.isNotEmpty
              ? ListView.separated(
                  physics: const BouncingScrollPhysics(),
                  padding: const EdgeInsets.all(21),
                  itemBuilder: (BuildContext context, int index) {
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        ItemDetailView(LocaleKeys.createdFor.tr(), getPendingTransactionsDetails![index].tRANSACTIONCREATEDFOR!),
                        ItemDetailView(LocaleKeys.creationDate.tr(), getPendingTransactionsDetails![index].cREATIONDATE!),
                        ItemDetailView(LocaleKeys.requestName.tr(), getPendingTransactionsDetails![index].uSERFUNCTIONNAME!),
                        ItemDetailView(LocaleKeys.requestType.tr(), getPendingTransactionsDetails![index].rEQUESTTYPE!),
                      ],
                    ).objectContainerView();
                  },
                  separatorBuilder: (BuildContext context, int index) => 12.height,
                  itemCount: getPendingTransactionsDetails!.length)
              : Utils.getNoDataWidget(context)),
    );
  }

  void getTicketTransactions() async {
    try {
      Utils.showLoading(context);
      getPendingTransactionsDetails = await PendingTransactionsApiClient().getPendingTransactionsDetails(functionID, dateFrom, dateTo);
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }
}
