import { ReplacementServiceRequest } from './../model/replacement-Service.request';
import { Component, OnInit } from '@angular/core';
import { VacationRuleServiceService } from '../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';
import { ModalController } from '@ionic/angular';



@Component({
  selector: 'app-replacement-list',
  templateUrl: './replacement-list.component.html',
  styleUrls: ['./replacement-list.component.scss'],
})
export class ReplacementListComponent 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;
  active: any;
  public direction: string;
  isSelect: boolean =false;
  searchKeyEnter: boolean = false;
  request: ReplacementServiceRequest;

  constructor(public vacationRuleService: VacationRuleServiceService, public ts: TranslatorService, public cs: CommonService, public modalController: ModalController) {
    this.direction = TranslatorService.getCurrentLanguageName();
   }

  ngOnInit() { 
   this.request = new ReplacementServiceRequest();

  }

  SearchReplacementList() {
    console.log("this.searchKeySelect" + this.searchKeySelect);

    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();
    this.request.P_SEARCH_USER_NAME = this.P_SEARCH_USER_NAME;
    this.request.P_SEARCH_EMPLOYEE_DISPLAY_NAME = this.P_SEARCH_EMPLOYEE_DISPLAY_NAME;
    this.request.P_SEARCH_EMAIL_ADDRESS = this.P_SEARCH_EMAIL_ADDRESS;
    this.request.P_PAGE_NUM = this.P_PAGE_NUM;
    this.request.P_PAGE_LIMIT = this.P_PAGE_LIMIT;
    this.request.P_SELECTED_EMPLOYEE_NUMBER = this.selEmp;

    this.vacationRuleService.getReplacementList(this.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;
          }
        }
      }
    }
  }

 

  closePage() {
    this.isSave = true;
    let data: any = null;
    if (this.isSave == true) {
      if (this.selEmp) {
        // data = this.ReplacementList[this.selEmp];
        data=this.selEmp;
      } else {
        let msg: string = this.ts.trPK("replacementRoll", "msg");
        this.cs.presentAlert(msg);
         return;
      }
      // if (typeof this.callback == 'function') {
      //   this.callback(data);
      // }

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

      this.modalController.dismiss(data);

    // }else{
     
    //     let msg: string = this.ts.trPK("replacementRoll", "msg");
    //     this.cs.presentAlert(msg);
    //     return;
     

    // }

      }
    
  }
  closeModal() {
    this.modalController.dismiss();
  }

  select(selectEmp,index){
    console.log(selectEmp.EMPLOYEE_DISPLAY_NAME);
    this.selEmp=selectEmp
    console.log("this.selEmp" + this.selEmp);

     this.active=index;
  }


  doInfinite(infiniteScroll) {
    if (!this.IsReachEnd) {
  

      ///const request = new ReplacementServiceRequest();
      console.log(this.request.P_SEARCH_USER_NAME + "-" +  this.request.P_SEARCH_EMAIL_ADDRESS + "-" + this.request.P_SEARCH_EMPLOYEE_DISPLAY_NAME )
      this.request.P_PAGE_NUM = this.P_PAGE_NUM;
      this.request.P_PAGE_LIMIT = this.P_PAGE_LIMIT;
     
      //  request.P_SELECTED_EMPLOYEE_NUMBER=this.selEmp;

      this.vacationRuleService.getReplacementList(this.request).
        subscribe((result: any) => {
          if (this.cs.validResponse(result)) {
            if (this.cs.hasData(result.ReplacementList)) {
              this.P_PAGE_NUM++;
              result.ReplacementList.forEach(vr => {
                if (vr.ROW_NUM == vr.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                }
                else {
                  this.IsReachEnd = false;
                }
                this.ReplacementList.push(vr);
                // this.pro.GetVacationRulesList.push(vr);   
              });
            } else {
              this.IsReachEnd = true;
            }
          }
          //this.P_PAGE_NUM++;
          if (infiniteScroll)
            infiniteScroll.target.complete();


          // console.log(resFlag);
        }, (Error) => console.log(Error), () => infiniteScroll.target.complete());
    } else {
      if (infiniteScroll)
        infiniteScroll.target.complete();
    }
  }

  onChangeSelect(select){
    console.log(select.detail.value);
    let selectValue= select.detail.value;

     if(selectValue == "1" || selectValue == "2" ||selectValue == "3"){
    this.isSelect=true;
     }
     else{
      this.isSelect=false;

     }
  }

  onChangeInput(){
    if (this.searchKey.length >0) {
      this.searchKeyEnter=true;
       
   }else{
     this.searchKeyEnter=false;
   
   }
  }
}
