import { Component, OnInit } from '@angular/core';
import { WorklistMainService } from '../service/work-list.main.service';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { ModalController } from '@ionic/angular';
import { WorklistService } from '../service/worklist.service';
import { MOItemHistoryReq } from '../models/MOItemHistoryReq';
import { MOItemHistoryRes } from '../models/MOItemHistoryRes';

@Component({
  selector: 'app-mo-item-history',
  templateUrl: './mo-item-history.component.html',
  styleUrls: ['./mo-item-history.component.scss'],
})
export class MoItemHistoryComponent implements OnInit {
  direction:string;
  getPassMOInfo: any;
  itemHistoryRes: any;
  IsReachEnd: boolean = false;
  P_PAGE_NUM: number = 1;
  P_PAGE_LIMIT: number = 50;
  MOItemHistoryReq: any;

  constructor(
    public worklistMainService: WorklistMainService,
    private cs: CommonService,
    public ts: TranslatorService,
    public worklistService: WorklistService
  ) {
    this.direction = TranslatorService.getCurrentLanguageName();

    this.getPassMOInfo = this.cs.sharedService.getSharedData('passMOReq');

    this.MOItemHistoryReq = new MOItemHistoryReq();
    this.MOItemHistoryReq.P_PAGE_NUM = this.P_PAGE_NUM;
    this.MOItemHistoryReq.P_PAGE_LIMIT = this.P_PAGE_LIMIT;
    this.MOItemHistoryReq.P_ITEM_ID= this.getPassMOInfo.P_ITEM_ID;
    this.MOItemHistoryReq.P_ORG_ID= this.getPassMOInfo.P_ORG_ID;

  }

  ngOnInit() {

    this.getMOItemHistory(this.getPassMOInfo.P_ITEM_ID,this.getPassMOInfo.P_ORG_ID);



  }

  getMOItemHistory(itemID ,orgID) {
    console.log(itemID);
    this.IsReachEnd = false;
    const request = new MOItemHistoryReq();
    request.P_ITEM_ID = parseInt(itemID);
    request.P_PAGE_LIMIT = 100;
    request.P_PAGE_NUM = 1;
    request.P_ORG_ID= parseInt(orgID);
    

    
    this.worklistMainService.getMOItemHistory(request).
      subscribe((result: MOItemHistoryRes) => {
     
        this.handleItemHistoryResult(result);
      });
  }


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

  }

}


doInfinite(infiniteScroll) {
  if (!this.IsReachEnd) {
   // this.POItemHistoryReq.P_PAGE_NUM = this.P_PAGE_NUM;
    this.worklistMainService.getMOItemHistory(this.MOItemHistoryReq).
      subscribe((result: any) => {
        if (this.cs.validResponse(result)) {
          if (result.GetMoItemHistoryList != undefined) {
            this.MOItemHistoryReq.P_PAGE_NUM=this.P_PAGE_NUM++;
            (result.GetMoItemHistoryList).forEach(element => {
              if (element.ROW_NUM == element.NO_OF_ROWS) {
                this.IsReachEnd = true;
              } else {
                this.IsReachEnd = false;
              }
              this.itemHistoryRes.push(element);
            }, (Error) => console.log(Error), () => infiniteScroll.target.complete());
          }// if list length >0
          else {
            this.IsReachEnd = true;
          }
        }// if response == 1
        //this.pageNum++;
        infiniteScroll.target.complete();

      });
  } else {
    if (infiniteScroll)
      infiniteScroll.target.complete();
  }
}//end infiniteScroll




}
