import { ViewNoteModalComponent } from './../view-note-modal/view-note-modal.component';
import { WorklistService } from './../service/worklist.service';
import { Component, OnInit } from '@angular/core';
import { WorkListActionHistoryRequest } from '../models/ActionHistoryReq';
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 {  POItemHistoryRequest} from '../models/POItemHistoryReq';
import { WorklistMainService } from '../service/work-list.main.service';
import { POItemHistoryRes } from '../models/POItemHistoryRes';


@Component({
  selector: 'app-item-history',
  templateUrl: './item-history.component.html',
  styleUrls: ['./item-history.component.scss'],
})
export class ItemHistoryComponent implements OnInit {
  getPassPOInfo: any;
  itemHistoryRes: any;
  IsReachEnd: boolean = false;
  P_PAGE_NUM: number = 1;
  P_PAGE_LIMIT: number = 50;
  POItemHistoryReq:any;
  getPassItemHistoreyList: any;
  constructor(
    public worklistMainService: WorklistMainService,
    private cs: CommonService,
    private ts: TranslatorService,
    private modalCtrl: ModalController,
    public worklistService: WorklistService
    ) {
    this.getPassPOInfo = this.cs.sharedService.getSharedData('passPOInfo');
    this.getPassItemHistoreyList = this.cs.sharedService.getSharedData('passItemHisList');
//itemHistoryRes
    this.POItemHistoryReq = new POItemHistoryRequest();
    this.POItemHistoryReq.P_PAGE_NUM = this.P_PAGE_NUM;
    this.POItemHistoryReq.P_PAGE_LIMIT = this.P_PAGE_LIMIT;
    this.POItemHistoryReq.P_ITEM_ID= this.getPassPOInfo;
    
  }
  ngOnInit() {

    this.getItemHistory(this.getPassPOInfo);
  }
 getItemHistory(itemID) {
    console.log(itemID);
    this.IsReachEnd = false;
    // const request = new POItemHistoryRequest();
    // request.P_ITEM_ID=parseInt(itemID);
    // request.P_PAGE_LIMIT=50;
    // request.P_PAGE_NUM=1;
    this.worklistMainService.getPOItemHistory( this.POItemHistoryReq).
      subscribe((result: POItemHistoryRes) => {

        this.handleItemHistoryResult(result);
      });
  }


  handleItemHistoryResult(result) {
    if (this.cs.validResponse(result)) {
      if (this.cs.hasData(result.GetPoItemHistoryList)) {
        this.itemHistoryRes = result.GetPoItemHistoryList;
        this.P_PAGE_NUM++;
        this.POItemHistoryReq.P_PAGE_NUM = this.P_PAGE_NUM;
        let lastItemIndex = this.itemHistoryRes.length - 1;
        if (result.GetPoItemHistoryList[lastItemIndex]) {
          let lastitem = result.GetPoItemHistoryList[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.getPOItemHistory(this.POItemHistoryReq).
        subscribe((result: any) => {
          if (this.cs.validResponse(result)) {
            if (result.GetPoItemHistoryList != undefined) {
              this.POItemHistoryReq.P_PAGE_NUM=this.P_PAGE_NUM++;
              (result.GetPoItemHistoryList).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


}
