import { Component, OnInit, ViewChild } from "@angular/core";
import { CommonService } from "src/app/hmg-common/services/common/common.service";
import { TranslatorService } from "src/app/hmg-common/services/translator/translator.service";
import { MenuResponse } from "src/app/hmg-common/services/menu/models/menu-response";
import { MenuService } from "src/app/hmg-common/services/menu/menuservice.service";
import { AbsenceAttahcmentResponse } from "../models/abs.attach.response";
import { AbsenceListService } from "../service/service.service";
import { IonInfiniteScroll } from "@ionic/angular";

@Component({
  selector: "app-home",
  templateUrl: "./home.component.html",
  styleUrls: ["./home.component.scss"]
})
export class HomeComponent implements OnInit {
  @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
  P_PAGE_NUM: number;
  P_PAGE_LIMIT: number;
  GetAbsenceTransactionList: any;
  IsReachEnd: boolean = false;
  selEmp: string = "";
  respID: number;
  selMenu: MenuResponse;
  constructor(
    public common: CommonService,
    public ts: TranslatorService,
    public menuService: MenuService,
    public absService: AbsenceListService
  ) {}

  ngOnInit() {
    this.P_PAGE_LIMIT = 50;
    this.P_PAGE_NUM = 1;
    this.selMenu = this.common.sharedService.getSharedData(
      MenuResponse.SHARED_DATA,
      false
    );
    this.selEmp = this.common.sharedService.getSharedData(
      MenuResponse.SHARED_SEL_EMP,
      false
    );
    this.respID = this.common.sharedService.getSharedData(
      MenuResponse.SHARED_SEL_RESP_ID,
      false
    );
    this.getAbsenceTransaction();
  }

  AccrualBalances() {
    this.common.openAccuralPage();
  }
  AttachmentDocuments(id) {
    const request = {
      P_ABSENCE_ATTENDANCE_ID: id
    };
    this.absService
      .getAbsenceAttachment(request)
      .subscribe((result: AbsenceAttahcmentResponse) => {
        this.handleAbsAttachResult(result);
      });
  }

  private handleAbsAttachResult(result) {
    if (this.common.validResponse(result)) {
      if (this.common.hasData(result.GetAbsenceAttachmentsList)) {
        this.common.sharedService.setSharedData(
          result.GetAbsenceAttachmentsList,
          AbsenceAttahcmentResponse.SHARED_DATA
        );
        this.common.openviewAbsenceAttachment();
        // this.navCtrl.push("AbsenceAttachmentPage");
      } else {
        let msg: string = "";
        msg = this.ts.trPK("general", "noFileSelect");
        this.common.presentAlert(msg);
      }
      //this.GetAbsenceTransactionList =result.GetAbsenceTransactionList;
    }
  }
  getAbsenceTransaction() {
    this.IsReachEnd = false;
    const request = {
      P_SELECTED_EMPLOYEE_NUMBER: this.selEmp,
      P_MENU_TYPE: this.selMenu.List_Menu.MENU_TYPE,
      P_SELECTED_RESP_ID: this.respID,
      P_PAGE_NUM: this.P_PAGE_NUM,
      P_PAGE_LIMIT: this.P_PAGE_LIMIT
    };
    this.absService.getAbsenceList(request).subscribe((result: any) => {
      this.handleAbsListResult(result);
    });
  }

  private handleAbsListResult(result) {
    if (this.common.validResponse(result)) {
      if (this.common.hasData(result.GetAbsenceTransactionList)) {
        this.GetAbsenceTransactionList = result.GetAbsenceTransactionList;
        this.P_PAGE_NUM++;
        let lastItemIndex = this.GetAbsenceTransactionList.length - 1;
        if (result.GetAbsenceTransactionList[lastItemIndex]) {
          let lastitem = result.GetAbsenceTransactionList[lastItemIndex];
          if (lastitem.NO_OF_ROWS == lastitem.ROW_NUM) {
            this.IsReachEnd = true;
          } else {
            this.IsReachEnd = false;
          }
        }
      }
    }
  }
  doInfinite(infiniteScroll) {
    if (!this.IsReachEnd) {
      //this.P_PAGE_NUM++;
      const request = {
        P_SELECTED_EMPLOYEE_NUMBER: this.selEmp,
        P_MENU_TYPE: this.selMenu.List_Menu.MENU_TYPE,
        P_SELECTED_RESP_ID: this.respID,
        P_PAGE_NUM: this.P_PAGE_NUM,
        P_PAGE_LIMIT: this.P_PAGE_LIMIT
      };
      this.absService.getAbsenceList(request).subscribe(
        (result: any) => {
          if (this.common.validResponse(result)) {
            if (this.common.hasData(result.GetAbsenceTransactionList)) {
              this.P_PAGE_NUM++;
              result.GetAbsenceTransactionList.forEach(vr => {
                if (vr.ROW_NUM == vr.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.GetAbsenceTransactionList.push(vr);
              });
            } else {
              this.IsReachEnd = true;
            }
          }
          if (this.infiniteScroll) {
            this.infiniteScroll.complete();
          }
        },
        Error => console.log(Error),
        () => this.infiniteScroll.complete()
      );
    } else {
      if (this.infiniteScroll) {
        this.infiniteScroll.complete();
      }
    }
  }
  createAbsence() {
    this.common.openSubmitAbsencePage();
  }
}
