import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/worklist/worklist_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/get_mo_Item_history_list_model.dart';
import 'package:mohem_flutter_app/models/get_po_Item_history_list_model.dart';
import 'package:mohem_flutter_app/models/get_quotation_analysis_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 ItemHistoryScreenParams {
  String? title;
  bool isItemHistory;
  bool isMO;
  int? pItemId;
  int? pPoHeaderId;
  int? pOrgId;

  ItemHistoryScreenParams({@required this.title, this.isItemHistory = true, this.isMO = true, this.pItemId, this.pPoHeaderId, this.pOrgId});
}

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

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

class _ItemHistoryScreenState extends State<ItemHistoryScreen> {
  ItemHistoryScreenParams? _screenParams;

  List<GetMoItemHistoryList> moItemHistoryList = [];
  List<GetPoItemHistoryList> poItemHistoryList = [];
  List<GetQuotationAnalysisList> quotationAnalysisList = [];

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

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

  void loadData() {
    if (_screenParams == null) {
      _screenParams = ModalRoute.of(context)!.settings.arguments as ItemHistoryScreenParams;
      getDataFromApi();
    }
  }

  void getDataFromApi() async {
    try {
      Utils.showLoading(context);
      if (_screenParams!.isItemHistory) {
        if (_screenParams!.isMO) {
          moItemHistoryList = await WorkListApiClient().getMoItemHistory(_screenParams!.pItemId!, _screenParams!.pOrgId!);
        } else {
          poItemHistoryList = await WorkListApiClient().getPoItemHistory(_screenParams!.pItemId!);
        }
      } else {
        quotationAnalysisList = await WorkListApiClient().getQuotationAnalysis(_screenParams!.pItemId!, _screenParams!.pPoHeaderId!);
      }

      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  @override
  Widget build(BuildContext context) {
    loadData();
    return Scaffold(
      appBar: AppBarWidget(context, title: _screenParams?.title ?? ""),
      backgroundColor: Colors.white,
      body: ListView(
        padding: const EdgeInsets.all(21),
        physics: const BouncingScrollPhysics(),
        children: [
          if (moItemHistoryList.isNotEmpty) loadMoItemHistoryData(),
          if (poItemHistoryList.isNotEmpty) loadPoItemHistoryData(),
          if (quotationAnalysisList.isNotEmpty) loadQuotationAnalysisData()
        ],
      ),
    );
  }

  Widget loadMoItemHistoryData() {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.requestNumber.tr(), moItemHistoryList[index].rEQUESTNUMBER ?? ""),
                  ItemDetailViewCol(LocaleKeys.uom.tr(), moItemHistoryList[index].uNITOFMEASURE ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.quantity.tr(), moItemHistoryList[index].qUANTITY?.toString() ?? ""),
                  ItemDetailViewCol(LocaleKeys.dateRequired.tr(), moItemHistoryList[index].dATEREQUIRED ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.lineStatus.tr(), moItemHistoryList[index].lINESTATUSDIS ?? ""),
                  ItemDetailViewCol(LocaleKeys.statusDate.tr(), moItemHistoryList[index].sTATUSDATE ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.transactionType.tr(), moItemHistoryList[index].tRANSACTIONTYPENAME ?? ""),
                  ItemDetailViewCol(LocaleKeys.organization.tr(), moItemHistoryList[index].oRGANIZATIONNAME ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.operatingCode.tr(), moItemHistoryList[index].oRGANIZATIONCODE ?? ""),
                  ItemDetailViewCol(LocaleKeys.operatingUnit.tr(), moItemHistoryList[index].oPERATINGUNITNAME ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.fromSubInventory.tr(), moItemHistoryList[index].fROMSUBINVENTORYCODE ?? ""),
                  ItemDetailViewCol(LocaleKeys.fromLocator.tr(), moItemHistoryList[index].fROMLOCATOR ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.toSubInventory.tr(), moItemHistoryList[index].tOSUBINVENTORYCODE ?? ""),
                  ItemDetailViewCol(LocaleKeys.toLocator.tr(), moItemHistoryList[index].tOLOCATOR ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.shipToLocation.tr(), moItemHistoryList[index].sHIPTOLOCATION ?? ""),
                  Container(),
                  isItLast: true,
                ),
              ],
            ).objectContainerView(),
        separatorBuilder: (cxt, index) => 12.height,
        itemCount: moItemHistoryList.length);
  }

  Widget loadPoItemHistoryData() {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.operatingUnit.tr(), poItemHistoryList[index].oUNAME ?? ""),
                  ItemDetailViewCol(LocaleKeys.poNumber.tr(), poItemHistoryList[index].pONUMBER ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.revision.tr(), poItemHistoryList[index].rEVISIONNUM?.toString() ?? ""),
                  ItemDetailViewCol(LocaleKeys.creationDate.tr(), poItemHistoryList[index].cREATIONDATE ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.supplier.tr(), poItemHistoryList[index].sUPPLIER ?? ""),
                  ItemDetailViewCol(LocaleKeys.buyer.tr(), poItemHistoryList[index].bUYER ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.uom.tr(), poItemHistoryList[index].uOM ?? ""),
                  ItemDetailViewCol(LocaleKeys.quantityOrdered.tr(), poItemHistoryList[index].qUANTITYORDERED?.toString() ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.quantityReceived.tr(), poItemHistoryList[index].qUANTITYRECEIVED?.toString() ?? ""),
                  ItemDetailViewCol(LocaleKeys.bonusQuantity.tr(), poItemHistoryList[index].bONUSQUANTITY?.toString() ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.purchasePrice.tr(), poItemHistoryList[index].pURCHASEPRICE?.toString() ?? ""),
                  ItemDetailViewCol(LocaleKeys.discountPer.tr(), poItemHistoryList[index].dISCOUNTPERCENTAGE?.toString() ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.balanceQuantity.tr(), poItemHistoryList[index].bALANCEQUANTITY?.toString() ?? ""),
                  ItemDetailViewCol(LocaleKeys.netPrice.tr(), poItemHistoryList[index].nETPRICE?.toString() ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.closureStatus.tr(), poItemHistoryList[index].cLOSEDCODE ?? ""),
                  Container(),
                  isItLast: true,
                )
              ],
            ).objectContainerView(),
        separatorBuilder: (cxt, index) => 12.height,
        itemCount: poItemHistoryList.length);
  }

  Widget loadQuotationAnalysisData() {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.quotationNumber.tr(), quotationAnalysisList[index].qUOTNUM ?? ""),
                  ItemDetailViewCol(LocaleKeys.vendorName.tr(), quotationAnalysisList[index].vENDORNAME ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.itemCode.tr(), quotationAnalysisList[index].iTEMCODE ?? ""),
                  ItemDetailViewCol(LocaleKeys.description.tr(), quotationAnalysisList[index].iTEMDESC ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.quotationQty.tr(), quotationAnalysisList[index].qUOTQTY?.toString() ?? ""),
                  ItemDetailViewCol(LocaleKeys.quotationUOM.tr(), quotationAnalysisList[index].qUOTUOM ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.quotationNetPrice.tr(), quotationAnalysisList[index].qUOTUNITPRICE?.toString() ?? ""),
                  ItemDetailViewCol(LocaleKeys.quotationLineTotal.tr(), quotationAnalysisList[index].qUOTLINETOTAL?.toString() ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.quotationBonusQuantity.tr(), quotationAnalysisList[index].qUOTBONUSQTY ?? ""),
                  ItemDetailViewCol(LocaleKeys.quotationDeliveryDate.tr(), quotationAnalysisList[index].qUOTDELIVERYDATE ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.quotationMFGPartNumber.tr(), quotationAnalysisList[index].qUOTMFGPARTNUM ?? ""),
                  ItemDetailViewCol(LocaleKeys.rfqNumber.tr(), quotationAnalysisList[index].rFQNUM ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.rfqQty.tr(), quotationAnalysisList[index].rFQQTY?.toString() ?? ""),
                  ItemDetailViewCol(LocaleKeys.rfqUOM.tr(), quotationAnalysisList[index].rFQUOM ?? ""),
                  isItLast: true,
                )
              ],
            ).objectContainerView(title: "${quotationAnalysisList[index].iTEMCODE}-${quotationAnalysisList[index].iTEMDESC}"),
        separatorBuilder: (cxt, index) => 12.height,
        itemCount: quotationAnalysisList.length);
  }
}
