import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/pending_transactions_api_client.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/models/pending_transactions/get_pending_transactions_details.dart';
import 'package:mohem_flutter_app/widgets/app_bar_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 == "") {
      final 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: "Pending Transactions",
      ),
      body: getPendingTransactionsDetails.isNotEmpty
          ? Container(
              margin: const EdgeInsets.only(top: 10.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Expanded(
                      child: ListView.separated(
                          physics: const BouncingScrollPhysics(),
                          shrinkWrap: true,
                          itemBuilder: (BuildContext context, int index) {
                            return Container(
                              width: double.infinity,
                              padding: const EdgeInsets.only(left: 12, right: 12, top: 10, bottom: 10),
                              margin: const EdgeInsets.only(left: 12, right: 12, top: 10, bottom: 10),
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(10),
                                boxShadow: [
                                  BoxShadow(
                                    color: const Color(0xff000000).withOpacity(.05),
                                    blurRadius: 26,
                                    offset: const Offset(0, -3),
                                  ),
                                ],
                              ),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Row(
                                        children: [
                                          "Created For ".toText14(color: MyColors.grey57Color),
                                          getPendingTransactionsDetails[index].tRANSACTIONCREATEDFOR!.toText14(color: MyColors.grey57Color),
                                        ],
                                      ),
                                      Column(
                                        children: [
                                          getPendingTransactionsDetails[index].cREATIONDATE!.split(" ")[0].toText12(color: MyColors.grey70Color),
                                          getPendingTransactionsDetails[index].cREATIONDATE!.split(" ")[1].toText12(color: MyColors.grey70Color),
                                        ],
                                      ),
                                    ],
                                  ),
                                  Container(
                                    child: Row(
                                      children: [
                                        "Request Name: ".toText14(color: MyColors.grey57Color),
                                        getPendingTransactionsDetails[index].uSERFUNCTIONNAME!.toText12(color: MyColors.grey57Color),
                                      ],
                                    ),
                                  ),
                                  Container(
                                    padding: const EdgeInsets.only(top: 0.0),
                                    child: Row(
                                      children: [
                                        "Request Type: ".toText14(color: MyColors.grey57Color),
                                        getPendingTransactionsDetails[index].rEQUESTTYPE!.toText14(color: MyColors.redColor),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            );
                          },
                          separatorBuilder: (BuildContext context, int index) => 12.height,
                          itemCount: getPendingTransactionsDetails.length ?? 0))
                ],
              ),
            )
          : 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);
    }
  }
}
