import 'package:easy_localization/src/public_ext.dart';
import 'package:expandable/expandable.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/classes/colors.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_item_creation_ntf_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_mo_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_po_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_pr_notification_body_list_model.dart';
import 'package:mohem_flutter_app/ui/work_list/item_history_screen.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';
import 'package:mohem_flutter_app/widgets/item_detail_view_widget.dart';

class RequestFragment extends StatelessWidget {
  final List<GetMoNotificationBodyList> moNotificationBodyList;
  final List<ItemCreationLines> itemCreationLines;
  final List<POLines> poLinesList;
  final List<PRLines> prLinesList;

  RequestFragment({
    Key? key,
    this.moNotificationBodyList = const <GetMoNotificationBodyList>[],
    this.itemCreationLines = const <ItemCreationLines>[],
    this.poLinesList = const <POLines>[],
    this.prLinesList = const <PRLines>[],
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView(
        physics: const BouncingScrollPhysics(),
        padding: const EdgeInsets.all(21),
        children: [
          if (moNotificationBodyList.isNotEmpty) moNotificationDataView(),
          if (poLinesList.isNotEmpty) poLinesDataView(),
          if (itemCreationLines.isNotEmpty) itemCreationLinesView(),
          if (prLinesList.isNotEmpty) prLinesDataView(),
        ],
      ),
    );
  }

  Widget poLinesDataView() {
    return ExpandableNotifier(
      child: ListView.separated(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          itemBuilder: (cxt, index) => ExpandablePanel(
                header: poLinesList[index].iTEMDESCRIPTION!.toText14(),
                collapsed: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    ItemDetailGrid(
                      ItemDetailViewCol(LocaleKeys.code.tr(), poLinesList[index].iTEMCODE ?? ""),
                      ItemDetailViewCol(LocaleKeys.mfg.tr(), poLinesList[index].uOM ?? ""),
                    ),
                    ItemDetailGrid(
                      ItemDetailViewCol(LocaleKeys.lineType.tr(), poLinesList[index].qUANTITY?.toString() ?? ""),
                      ItemDetailViewCol(LocaleKeys.unit.tr(), poLinesList[index].uOM ?? ""),
                    ),
                    ItemDetailGrid(
                      ItemDetailViewCol(LocaleKeys.price.tr(), poLinesList[index].uNITPRICE?.toString() ?? ""),
                      ItemDetailViewCol(LocaleKeys.lineAmount.tr(), poLinesList[index].lINEAMOUNT?.toString() ?? ""),
                    ),
                    ItemDetailGrid(
                      ItemDetailViewCol(LocaleKeys.quantity.tr(), poLinesList[index].qUANTITY?.toString() ?? ""),
                      ItemDetailViewCol(LocaleKeys.lineDiscount.tr(), poLinesList[index].lINEDISCPERCENTAGE?.toString() ?? ""),
                    ),
                    ItemDetailGrid(
                      ItemDetailViewCol(LocaleKeys.needByDate.tr(), poLinesList[index].nEEDBYDATE ?? ""),
                      ItemDetailViewCol(LocaleKeys.promisedDate.tr(), poLinesList[index].pROMISEDDATE ?? ""),
                    ),
                    ItemDetailGrid(
                      ItemDetailViewCol(LocaleKeys.deliverToLocation.tr(), poLinesList[index].dELIVERTOLOCATION ?? ""),
                      ItemDetailViewCol(LocaleKeys.requisitionNumber.tr(), poLinesList[index].rEQUESTOR ?? ""),
                    ),
                    ItemDetailGrid(
                      ItemDetailViewCol(LocaleKeys.requester.tr(), poLinesList[index].pRNUM ?? ""),
                      Container(),
                    ),
                    12.height,
                    Row(
                      children: [
                        DefaultButton(LocaleKeys.itemHistory.tr(), () {
                          Navigator.pushNamed(
                            cxt,
                            AppRoutes.itemHistory,
                            arguments: ItemHistoryScreenParams(title: LocaleKeys.itemHistory.tr(), isMO: false, pItemId: poLinesList[index].iTEMID),
                          );
                        }).expanded,
                        12.width,
                        DefaultButton(LocaleKeys.quotationAnalysis.tr(), () {
                          Navigator.pushNamed(
                            cxt,
                            AppRoutes.itemHistory,
                            arguments: ItemHistoryScreenParams(
                                isItemHistory: false, isMO: false, title: LocaleKeys.quotationAnalysis.tr(), pItemId: poLinesList[index].iTEMID, pPoHeaderId: poLinesList[index].pOHEADERID),
                          );
                        }).expanded,
                      ],
                    )
                  ],
                ),
                expanded: const SizedBox(),
              ).objectContainerView(),
          separatorBuilder: (cxt, index) => 12.height,
          itemCount: poLinesList.length),
    );
  }

  Widget prLinesDataView() {
    return ExpandableNotifier(
      child: ListView.separated(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          itemBuilder: (cxt, index) => ExpandablePanel(
                header: prLinesList[index].dESCRIPTION.toString().toText14(color: MyColors.textMixColor),
                collapsed: Column(
                  children: [
                    ItemDetailGrid(
                      ItemDetailViewCol("Cost Center", prLinesList[index].cOSTCENTER ?? ""),
                      ItemDetailViewCol("Code", prLinesList[index].iTEMCODE ?? ""),
                    ),
                    ItemDetailGrid(
                      ItemDetailViewCol("Unit", prLinesList[index].uOM ?? ""),
                      ItemDetailViewCol("Price (SAR)", prLinesList[index].uNITPRICE.toString() ?? ""),
                    ),
                    ItemDetailGrid(
                      ItemDetailViewCol("Amount (SAR)", prLinesList[index].lINEAMOUNT.toString() ?? ""),
                      ItemDetailViewCol("Quantity", prLinesList[index].qUANTITY.toString() ?? ""),
                    ),
                    ItemDetailGrid(
                      ItemDetailViewCol("AMU (Last 3 months)", prLinesList[index].iTEMAMU.toString() ?? ""),
                      Container(),
                      isItLast: true,
                    ),
                    // ItemDetailGrid(
                    //   ItemDetailViewCol(getPrNotificationBodyList.pRHeader![1].hDRATTRIBUTENAME!, getPrNotificationBodyList.pRHeader![1].hDRATTRIBUTEVALUE ?? ""),
                    //   Container(),
                    //   isItLast: true,
                    // ),
                  ],
                ),
                expanded: const SizedBox(),
              ).objectContainerView(),
          separatorBuilder: (cxt, index) => 12.height,
          itemCount: prLinesList.length),
    );
  }

  Widget moNotificationDataView() {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.code.tr(), moNotificationBodyList[index].iTEMCODE ?? ""),
                  ItemDetailViewCol(LocaleKeys.unit.tr(), moNotificationBodyList[index].uOM ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.quantity.tr(), moNotificationBodyList[index].qUANTITY?.toString() ?? ""),
                  ItemDetailViewCol(LocaleKeys.dateRequired.tr(), moNotificationBodyList[index].dATEREQUIRED ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.lineStatus.tr(), moNotificationBodyList[index].lINESTATUS ?? ""),
                  ItemDetailViewCol(LocaleKeys.statusDate.tr(), moNotificationBodyList[index].sTATUSDATE ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.transactionType.tr(), moNotificationBodyList[index].tRANSACTIONTYPENAME ?? ""),
                  ItemDetailViewCol(LocaleKeys.operatingUnit.tr(), moNotificationBodyList[index].oPERATINGUNIT ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.organizationCode.tr(), moNotificationBodyList[index].oRGANIZATIONCODE ?? ""),
                  ItemDetailViewCol(LocaleKeys.organization.tr(), moNotificationBodyList[index].oRGANIZATIONNAME ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.fromSubInventory.tr(), moNotificationBodyList[index].fROMSUBINVENTORY ?? ""),
                  ItemDetailViewCol(LocaleKeys.fromLocator.tr(), moNotificationBodyList[index].fROMLOCATOR ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.toSubInventory.tr(), moNotificationBodyList[index].tOSUBINVENTORY ?? ""),
                  ItemDetailViewCol(LocaleKeys.toLocator.tr(), moNotificationBodyList[index].tOLOCATOR ?? ""),
                ),
                ItemDetailGrid(ItemDetailViewCol(LocaleKeys.shipToLocator.tr(), moNotificationBodyList[index].sHIPTOLOCATION ?? ""), Container()),
                12.height,
                DefaultButton(LocaleKeys.itemHistory.tr(), () {
                  Navigator.pushNamed(
                    cxt,
                    AppRoutes.itemHistory,
                    arguments: ItemHistoryScreenParams(title: LocaleKeys.itemHistory.tr(), pItemId: moNotificationBodyList[index].iTEMID, pOrgId: moNotificationBodyList[index].oRGID),
                  );
                })
              ],
            ).objectContainerView(title: moNotificationBodyList[index].dESCRIPTION ?? ""),
        separatorBuilder: (cxt, index) => 12.height,
        itemCount: moNotificationBodyList.length);
  }

  Widget itemCreationLinesView() {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.productName.tr(), itemCreationLines[index].pRODUCTNAME ?? ""),
                  ItemDetailViewCol(LocaleKeys.productDescription.tr(), itemCreationLines[index].pRODUCTDESCRIPTION ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.unitPrice.tr(), itemCreationLines[index].uNITPRICE?.toString() ?? ""),
                  ItemDetailViewCol(LocaleKeys.manufacturerName.tr(), itemCreationLines[index].uSERMANUFACTURERNAME ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.manufacturerPartName.tr(), itemCreationLines[index].uSERMFGPARTNUM ?? ""),
                  ItemDetailViewCol(LocaleKeys.supplierName.tr(), itemCreationLines[index].sUPPLIERNAME ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.supplierContact.tr(), itemCreationLines[index].sUPPLIERCONTACT ?? ""),
                  ItemDetailViewCol(LocaleKeys.chargeToPatient.tr(), itemCreationLines[index].cHARGETOPATIENT ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.justification.tr(), itemCreationLines[index].jUSTIFICATION ?? ""),
                  ItemDetailViewCol(LocaleKeys.itemCode.tr(), itemCreationLines[index].iTEMCODE ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.itemDescription.tr(), itemCreationLines[index].iTEMDESCRIPTION ?? ""),
                  ItemDetailViewCol(LocaleKeys.groupCode.tr(), itemCreationLines[index].iTEMGROUPCODE ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.groupDescription.tr(), itemCreationLines[index].iTEMGROUP ?? ""),
                  ItemDetailViewCol(LocaleKeys.subgroupCode.tr(), itemCreationLines[index].iTEMSUBGROUPCODE ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.subgroupDescription.tr(), itemCreationLines[index].iTEMSUBGROUP ?? ""),
                  ItemDetailViewCol(LocaleKeys.primaryUOM.tr(), itemCreationLines[index].pRIMARYUOM ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.manufacturerName.tr(), itemCreationLines[index].sTANDARDMANUFACTURERNAME ?? ""),
                  ItemDetailViewCol(LocaleKeys.manufacturerPartName.tr(), itemCreationLines[index].sTANDARDMFGPARTNUM ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.templateName.tr(), itemCreationLines[index].tEMPLATENAME ?? ""),
                  ItemDetailViewCol(LocaleKeys.itemCreationStatus.tr(), itemCreationLines[index].iTEMCREATIONSTATUS ?? ""),
                ),
                ItemDetailGrid(
                  ItemDetailViewCol(LocaleKeys.standardizationApprovalStatus.tr(), itemCreationLines[index].sTANDARDSTATUS ?? ""),
                  ItemDetailViewCol(LocaleKeys.standardizationApprovalRejectionReason.tr(), itemCreationLines[index].sTANDARDREJECTREASON ?? ""),
                  isItLast: true,
                ),
              ],
            ).objectContainerView(title: itemCreationLines[index].iTEMDESCRIPTION!),
        separatorBuilder: (cxt, index) => 12.height,
        itemCount: itemCreationLines.length);
  }
}
