import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { EITTransactionsRequest } from './../models/EITTransactionsReq';
import { MenuResponse } from 'src/app/hmg-common/services/menu/models/menu-response';
import { Component, OnInit } from '@angular/core';
import { EitService } from '../services/eit.service';
import { EIT_ACTION } from '../models/submit.eit.action';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { AuthenticationService } from 'src/app/hmg-common/services/authentication/authentication.service';

@Component({
  selector: 'app-cei-list',
  templateUrl: './cei-list.component.html',
  styleUrls: ['./cei-list.component.scss'],
})
export class CeiListComponent implements OnInit {
  Sdate: any;
  emp_no: any;
  balance:any;
  P_PAGE_NUM: number;
  P_PAGE_LIMIT: number;
  IsReachEnd = false;
  userName: any;
  isDelete = false;
  getResCEITransactionList: any = [];
  eitAddedList: any = [];
  getPassMnuEntryObj: any;
  selMenu: MenuResponse = new MenuResponse();
  CEITransactionsListObj: EITTransactionsRequest = new EITTransactionsRequest();
  selEmp: string;
  respID: number;
  transactionNo = 0;
  menuType = '';
  public functionName = '';
  private pAction = 0;
  public headerTitle = '';
  data: any;
  accrualNetInfants: any;
  accrualUNetChild: any;
  accrualNetAdult: any;

  public options = {
    cutoutPercentage: 80,
    tooltips: { enabled: false },
    legend: { display: false }};
    gaugeType = 'full';
  accrualBalancesList: any;
  common: any;
  direction: string;

  constructor(
    private cs: CommonService,
    private EITService: EitService,
    public ts: TranslatorService,
    public authService:AuthenticationService,) {
    this.direction = TranslatorService.getCurrentLanguageName();
    this.selMenu = this.cs.sharedService.getSharedData(MenuResponse.SHARED_DATA, false);
    this.selEmp = this.cs.sharedService.getSharedData(MenuResponse.SHARED_SEL_EMP, false);
    this.respID = this.cs.sharedService.getSharedData(MenuResponse.SHARED_SEL_RESP_ID, false);
    this.getPassMnuEntryObj = this.selMenu.GetMenuEntriesList;
    this.menuType = this.selMenu.List_Menu.MENU_TYPE;
    this.transactionNo = 0;
    this.functionName = this.getPassMnuEntryObj.FUNCTION_NAME;
    this.headerTitle = this.selMenu.GetMenuEntriesList.PROMPT;
    this.P_PAGE_NUM = 1;
    this.P_PAGE_LIMIT = 100;
  }

  ngOnInit() {
    this.getCEITransactions();
  }


  public getCEITransactions() {
    this.IsReachEnd = false;
    this.CEITransactionsListObj.P_PAGE_LIMIT = this.P_PAGE_LIMIT;
    this.CEITransactionsListObj.P_PAGE_NUM = this.P_PAGE_NUM;
    this.CEITransactionsListObj.P_SELECTED_EMPLOYEE_NUMBER = this.selEmp;
    this.CEITransactionsListObj.P_SELECTED_RESP_ID = this.respID;
    this.CEITransactionsListObj.P_MENU_TYPE = this.selMenu.List_Menu.MENU_TYPE;
    this.CEITransactionsListObj.P_FUNCTION_NAME = this.getPassMnuEntryObj.FUNCTION_NAME;
    this.EITService.getCEITransactionsList(this.CEITransactionsListObj).
      subscribe((result: any) => {
        this.handleCEITransactionsResult(result);
      });
  }

  public handleCEITransactionsResult(result) {
    if (this.cs.validResponse(result)) {
      if (this.cs.hasData(result.GetEITTransactionList)) {
        this.getResCEITransactionList = result.GetEITTransactionList;
        this.P_PAGE_NUM++;
        this.CEITransactionsListObj.P_PAGE_NUM = this.P_PAGE_NUM;
        const lastItemIndex = this.getResCEITransactionList.length - 1;
        if (result.GetEITTransactionList[lastItemIndex] && result.GetEITTransactionList[lastItemIndex].Collection_Transaction[0]) {
          const lastitem = result.GetEITTransactionList[lastItemIndex].Collection_Transaction[0];
          // tslint:disable-next-line: triple-equals
          if (lastitem.NO_OF_ROWS == lastitem.ROW_NUM) {
            this.IsReachEnd = true;
          } else {
            this.IsReachEnd = false;
          }
        }
      }
    }
  }

  public addCEIRrq(i?) {
    this.pAction = EIT_ACTION.ADD;
    this.cs.sharedService.setSharedData(this.pAction, EITTransactionsRequest.SUBMIT_EIT_ACTION);
    this.openCEINew(i);
  }

  openCEINew(index) {
    let item: any = [];
    if (this.pAction == EIT_ACTION.UPDATE) {
      item = this.getResCEITransactionList[index].Collection_Transaction;
    }
    this.transactionNo++;
    // tslint:disable-next-line: max-line-length
    this.cs.sharedService.setSharedData({ dirfromNotificationPage: false, submitEITObjList: undefined, transNo: this.transactionNo }, 'AddEITData');
    this.cs.openAddCEIPage();
  }

}



