import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { Component, OnInit } from '@angular/core';
import { MenuResponse } from 'src/app/hmg-common/services/menu/models/menu-response';
import { LoginRequest } from 'src/app/hmg-common/services/authentication/models/login.request';
import { MyTeamService } from 'src/app/my-team/service/my-team.service';

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

  pageNum: number = 1;
  pageLimit: number = 50;
  empSubordinate: any;
  employee: any;
  public headerTitle: string = "";
  IsReachEnd: boolean = false;
  searchKey: any;
  isSearch: boolean = false;
  public searchKeySelect: any;
  infiniteRequest: any;
  selMenu: MenuResponse = new MenuResponse();

  constructor(public cs: CommonService, public myTeamService: MyTeamService) {
    this.selMenu = this.cs.sharedService.getSharedData(MenuResponse.SHARED_DATA, true);
    this.headerTitle = this.selMenu.List_Menu.MENU_NAME;
    this.isSearch = this.cs.sharedService.getSharedData("isSearch") ? this.cs.sharedService.getSharedData("isSearch") : false;

    //this.employee
    if (!this.isSearch)
      this.getEmpSubordinate();
  }

  ngOnInit() { }

  getEmpSubordinate() {
    let selEmpNo: string = null;
    this.pageNum = 1;
    this.IsReachEnd = false;
    let searchEmpNum = "";
    let searchEmpName = "";
    let searchEmpEmail = "";
    if (this.searchKey) {
      switch (this.searchKeySelect) {
        case '1': {

          searchEmpName = this.searchKey;
          searchEmpNum = "";
          searchEmpEmail = "";

          break;
        }
        case '2': {
          searchEmpNum = this.searchKey;
          searchEmpName = "";
          searchEmpEmail = "";
          break;
        }
        case '3': {
          searchEmpEmail = this.searchKey;
          searchEmpNum = "";
          searchEmpName = "";
          break;
        }
        default: {
          searchEmpNum = "";
          searchEmpName = "";
          searchEmpEmail = "";
          break;
        }
      }
    } else {
      searchEmpNum = "";
      searchEmpName = "";
      searchEmpEmail = "";
    }
    if (this.isSearch)
      selEmpNo = null;
    else
      selEmpNo = this.cs.sharedService.getSharedData(LoginRequest.SHARED_DATA, true);
    // this.employee=this.empSubordinate[index];
    // selEmpNo=this.employee.EMPLOYEE_NUMBER;
    const body = {
      P_SELECTED_EMPLOYEE_NUMBER: selEmpNo,
      P_SEARCH_EMPLOYEE_NUMBER: searchEmpNum,
      P_SEARCH_EMPLOYEE_DISPLAY_NAME: searchEmpName,
      P_SEARCH_EMAIL_ADDRESS: searchEmpEmail,
      P_PAGE_NUM: this.pageNum,
      P_PAGE_LIMIT: this.pageLimit
    }
    this.infiniteRequest = body;
    this.myTeamService.getEmployeeSubordinates(body).
      subscribe((result: any) => {
        this.handleEmpResult(result);
      });
  }

  handleEmpResult(result) {
    //let employeeSubordinate=resFlag.GetEmployeeSubordinatesList;
    //this.nav.push("MyTeamPage",{empSubordinate:employeeSubordinate});
    if (this.cs.validResponse(result)) {
      if (this.cs.hasData(result.GetEmployeeSubordinatesList)) {
        this.empSubordinate = result.GetEmployeeSubordinatesList;
        this.infiniteRequest.P_PAGE_NUM= this.pageNum++;
        let lastItemIndex = this.empSubordinate.length - 1;
        if (result.GetEmployeeSubordinatesList[lastItemIndex]) {
          let lastitem = result.GetEmployeeSubordinatesList[lastItemIndex];
          if (lastitem.NO_OF_ROWS == lastitem.ROW_NUM) {
            this.IsReachEnd = true;
          } else {
            this.IsReachEnd = false;
          }
        }
      } else {
        this.empSubordinate = [];
      }
    }
  }
  doInfinite(infiniteScroll) {
    if (!this.IsReachEnd && this.infiniteRequest) {

      // this.infiniteRequest.P_PAGE_NUM = this.pageNum;
      this.myTeamService.getEmployeeSubordinates(this.infiniteRequest).
        subscribe((result: any) => {
          if (this.cs.validResponse(result)) {
            // this.pageNum++;
            this.infiniteRequest.P_PAGE_NUM= this.pageNum++;

            if (this.cs.hasData(result.GetEmployeeSubordinatesList)) {
              result.GetEmployeeSubordinatesList.forEach(vr => {
                if (vr.ROW_NUM == vr.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                }
                else {
                  this.IsReachEnd = false;
                }
                this.empSubordinate.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();
    }
  }
  getDetails(index) {
    // this.navCtrl.push("MyTeamPage", { employee: this.empSubordinate[index] });
    this.cs.sharedService.setSharedData({ employee: this.empSubordinate[index] }, 'MyTeamPage');
    this.cs.openMyTeamPage();
  }
  toggleSearch() {
    this.isSearch = !this.isSearch;
  }
  goToSubordinate() {
    // this.navCtrl.push("MySubordinatePage", { isSearch: true });
    this.cs.sharedService.setSharedData(true, 'isSearch');
    this.cs.openMySubordinatePage();
  }

}
