import { AddEitComponent } from './../add-eit/add-eit.component';
import { ModalController } from '@ionic/angular';
import { EitRequest } from './../models/eit.request';
import { Component, OnInit } from '@angular/core';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { EITNotificatonBodyResponse } from '../models/EITNotificationBodyRes';
import { EitService } from '../services/eit.service';

@Component({
  selector: 'app-eit-update-list',
  templateUrl: './eit-update-list.component.html',
  styleUrls: ['./eit-update-list.component.scss'],
})
export class EitUpdateListComponent implements OnInit {

  public notificationBodyRes: any = {};
  private updatedData: any = [];
  private updatedItem: any;
  private itemKey: string = "";
  private notificationId: number;
  private selEmp: string;
  private functionName: string;
  private eitTransactionTbl: any = [];
  private eitRequest: EitRequest;
  private descFlex: string;

  constructor(public cs: CommonService, public eitService: EitService, public modalController: ModalController) {
    this.notificationBodyRes = this.cs.sharedService.getSharedData(EITNotificatonBodyResponse.SHARED_DATA, false);
    let notification = this.cs.sharedService.getSharedData(EITNotificatonBodyResponse.NOT_WORKLIST, false);
    this.itemKey = notification.ITEM_KEY;
    this.notificationId = notification.NOTIFICATION_ID;
    this.functionName = notification.FUNCTION_NAME;
    this.selEmp = notification.SELECTED_EMPLOYEE_NUMBER;
    this.descFlex = notification.DESC_FLEX_CONTEXT_CODE;
    this.fillEitTransactionTable();
  }

  ngOnInit() { console.log("ngOnInit");}

  async updateEitNot(index) {
    let item = this.notificationBodyRes[index].Collection_Notification;
    this.cs.sharedService.setSharedData({ dirfromNotificationPage: true, submitEITObjList: item }, 'AddEITData')
    const modal = await this.modalController.create({
      component: AddEitComponent
    });

    modal.onDidDismiss()
      .then((data: any) => {
        console.log("data" + data);
        console.log("data" + data.data.updated);
        console.log("data" + data.data.eitRequest.EITTransactionTBL);


        if (data) {
          this.updatedData = this.updatedData ? this.updatedData.concat(data.data.updated) : data.data.updated;
          this.notificationBodyRes[index].Collection_Notification = data.data.updated;
          this.updateTransactionList(data.data.eitRequest.EITTransactionTBL);
        }
      });

    return await modal.present();
  }

  updateTransactionList(list) {
    list.forEach(element => {
      const index = this.eitTransactionTbl.findIndex(x => x.TRANSACTION_NUMBER === element.TRANSACTION_NUMBER);
      if (index != -1)
        this.eitTransactionTbl.splice(index, 1);
    });
    this.eitTransactionTbl = this.eitTransactionTbl ? this.eitTransactionTbl.concat(list) : list;
  }

  resubmitEit() {
    this.eitRequest = new EitRequest();
    this.eitRequest.EITTransactionTBL = this.eitTransactionTbl;
    this.eitRequest.P_SELECTED_EMPLOYEE_NUMBER = this.selEmp;
    this.eitRequest.P_MENU_TYPE = "E";
    this.eitRequest.P_SELECTED_RESP_ID = -999;
    this.eitRequest.P_FUNCTION_NAME = this.functionName;
    this.eitRequest.P_DESC_FLEX_CONTEXT_CODE = this.descFlex;
    let body = {
      P_NOTIFICATION_ID: this.notificationId,
      P_ITEM_KEY: this.itemKey,
      EITTransactionTBL: this.eitTransactionTbl
    }
    this.eitService.reSubmitEit(body).subscribe((result: any) => {
      this.handleResubmitEit(result);
    });
  }

  handleResubmitEit(result) {
    if (this.cs.validResponse(result)) {
      this.cs.sharedService.setSharedData(result.ResubmitEITTransactionList.P_TRANSACTION_ID, "TransactionIDResubmit");
      this.cs.sharedService.setSharedData(this.eitRequest, EitRequest.SHARED_DATA);
      this.cs.sharedService.setSharedData({ isResubmit: true }, 'confirmAddEITData');
      this.cs.openConfirmEitPage();
    }
  }

  fillEitTransactionTable() {
    let valuseArr: any = [];
    let varcharValue: any = null;
    let numbervalue: any = 0;
    let dateValue: any = null;
    let transNo: number = 1;
    let textValue = "";
    // if(this.ExtraObj.transactionNo)
    // transNo=this.ExtraObj.transactionNo;
    for (let j = 0; j < this.notificationBodyRes.length; j++) {
      let list = this.notificationBodyRes[j].Collection_Notification;
      for (let i = 0; i < list.length; i++) {
        let item = list[i];
        varcharValue = null;
        numbervalue = null;
        dateValue = null;
        valuseArr.push(
          {
            TRANSACTION_NUMBER: item.TRANSACTION_NUMBER,
            NAME: item.APPLICATION_COLUMN_NAME,
            VARCHAR2_VALUE: item.VARCHAR2_VALUE,
            NUMBER_VALUE: item.NUMBER_VALUE,
            DATE_VALUE: item.DATE_VALUE,
          }
        )
      }// end for 
    }
    this.eitTransactionTbl = valuseArr;
  }

}
