import { Component, OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { ReplacementServiceRequest } from 'src/app/vacation-rule/model/replacement-Service.request';
import { VacationRuleServiceService } from 'src/app/vacation-rule/service/vacation-rule-service.service';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';


@Component({
  selector: 'app-work-list-replacement-itg',
  templateUrl: './work-list-replacement-itg.component.html',
  styleUrls: ['./work-list-replacement-itg.component.scss'],
})
export class WorkListReplacementItgComponent implements OnInit {
  P_SEARCH_USER_NAME: string = "";
  P_SEARCH_EMPLOYEE_DISPLAY_NAME: string = "";
  P_SEARCH_EMAIL_ADDRESS: string = "";
  P_PAGE_NUM: number = 1;
  P_PAGE_LIMIT: number = 50;
  ReplacementList: any = [];
  IsReachEnd: boolean = false;
  selEmp: string = null;
  callback: any;
  searchKey: any = "";
  searchKeySelect: string = "";
  replacmentRequest: any = "";
  isAbs: boolean = false;
  isSave: boolean = false;

  constructor(public vacationRuleService: VacationRuleServiceService, 
    public ts: TranslatorService, 
    public cs: CommonService, 
    public modalController: ModalController) { }

  ngOnInit() {}

  SearchReplacementList() {
    this.ReplacementList = [];
    this.IsReachEnd = false;
    this.P_PAGE_NUM = 1;
    if (this.searchKey) {
      switch (this.searchKeySelect) {
        case '1': {

          this.P_SEARCH_EMPLOYEE_DISPLAY_NAME = this.searchKey;
          this.P_SEARCH_USER_NAME = "";
          this.P_SEARCH_EMAIL_ADDRESS = "";

          break;
        }
        case '2': {
          this.P_SEARCH_USER_NAME = this.searchKey;
          this.P_SEARCH_EMPLOYEE_DISPLAY_NAME = "";
          this.P_SEARCH_EMAIL_ADDRESS = "";
          break;
        }
        case '3': {
          this.P_SEARCH_EMAIL_ADDRESS = this.searchKey;
          this.P_SEARCH_USER_NAME = "";
          this.P_SEARCH_EMPLOYEE_DISPLAY_NAME = "";
          break;
        }
        default: {
          this.P_SEARCH_USER_NAME = "";
          this.P_SEARCH_EMPLOYEE_DISPLAY_NAME = "";
          this.P_SEARCH_EMAIL_ADDRESS = "";
          break;
        }
      }
    } else {
      this.P_SEARCH_USER_NAME = "";
      this.P_SEARCH_EMPLOYEE_DISPLAY_NAME = "";
      this.P_SEARCH_EMAIL_ADDRESS = "";
    }

    const request = new ReplacementServiceRequest();
    request.P_SEARCH_USER_NAME = this.P_SEARCH_USER_NAME;
    request.P_SEARCH_EMPLOYEE_DISPLAY_NAME = this.P_SEARCH_EMPLOYEE_DISPLAY_NAME;
    request.P_SEARCH_EMAIL_ADDRESS = this.P_SEARCH_EMAIL_ADDRESS;
    request.P_PAGE_NUM = this.P_PAGE_NUM;
    request.P_PAGE_LIMIT = this.P_PAGE_LIMIT;
    request.P_SELECTED_EMPLOYEE_NUMBER = this.selEmp;

    this.vacationRuleService.getReplacementList(request).
      subscribe((result: any) => {
        this.handleReplacment(result);
      });
  }

  handleReplacment(result) {
    if (this.cs.validResponse(result)) {
      if (this.cs.hasData(result.ReplacementList)) {
        this.ReplacementList = result.ReplacementList;
        this.P_PAGE_NUM++;
        let lastItemIndex = this.ReplacementList.length - 1;
        if (result.ReplacementList[lastItemIndex]) {
          let lastitem = result.ReplacementList[lastItemIndex];
          if (lastitem.NO_OF_ROWS == lastitem.ROW_NUM) {
            this.IsReachEnd = true;
          } else {
            this.IsReachEnd = false;
          }
        }
      }
    }
  }

  doInfinite(infiniteScroll) {

  }

  closePage() {
    this.isSave = true;
    let data: any = null;
    if (this.isSave == true) {
      if (this.selEmp) {
        data = this.ReplacementList[this.selEmp];
      } else {
        data = null;
      }
      if (typeof this.callback == 'function') {
        this.callback(data);
      }

      this.modalController.dismiss({
        'dismissed': true,
        empData: data
      });
    }
  }
}
