import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/my_attendance_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/get_eit_transaction_list_model.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/item_detail_view_widget.dart';

class DynamicListViewParams {
  String title;
  String dynamicId;
  String uRL;
  String requestID;
  final String selectedEmp;
  DynamicListViewParams(this.title, this.dynamicId, {this.selectedEmp ='', this.uRL = 'GET_EIT_DFF_STRUCTURE', this.requestID = ''});
}

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

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

class _DynamicListViewScreenState extends State<DynamicListViewScreen> {
  List<GetEITTransactionList>? getEITTransactionList;
  DynamicListViewParams? dynamicParams;
  @override
  void initState() {
    super.initState();
  }

  void getTransactions() async {
   try {
      Utils.showLoading(context);
      getEITTransactionList = await MyAttendanceApiClient().getEitTransaction(dynamicParams!.dynamicId, dynamicParams!.selectedEmp);
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

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

  @override
  Widget build(BuildContext context) {
    if (dynamicParams == null) {
      dynamicParams = ModalRoute.of(context)!.settings.arguments as DynamicListViewParams;
      getTransactions();
    }

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBarWidget(
        context,
        title: dynamicParams!.title,
      ),
      body: getEITTransactionList == null
          ? const SizedBox()
          : (getEITTransactionList!.isEmpty
              ? Utils.getNoDataWidget(context)
              : ListView.separated(
                  physics: const BouncingScrollPhysics(),
                  padding: EdgeInsets.all(21),
                  itemBuilder: (cxt, int parentIndex) => Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          for (int t = 0; t < (getEITTransactionList![parentIndex].collectionTransaction ?? []).length; t++)
                            if (getEITTransactionList![parentIndex].collectionTransaction![t].dISPLAYFLAG == "Y")
                              ItemDetailView(
                                  getEITTransactionList![parentIndex].collectionTransaction![t].sEGMENTPROMPT!, getEITTransactionList![parentIndex].collectionTransaction![t].sEGMENTVALUEDSP ?? ""),
                        ],
                      ).objectContainerView(),
                  separatorBuilder: (cxt, index) => 12.height,
                  itemCount: getEITTransactionList!.length)),
      floatingActionButton: Container(
        height: 50,
        width: 50,
        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.addDynamicInput, arguments: dynamicParams);
      }),
    );
  }
}
