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';
import { QuotationAnalysisRequest } from '../models/quotationAnalysisReq';
import { QuotationAnalysisResponse } from '../models/quotationAnalysisRes';

@Component({
  selector: 'app-qutation-analysis',
  templateUrl: './qutation-analysis.component.html',
  styleUrls: ['./qutation-analysis.component.scss'],
})
export class QutationAnalysisComponent implements OnInit {
  IsReachEnd: boolean = false;
  P_PAGE_NUM: number = 1;
  P_PAGE_LIMIT: number = 100;
  POQuotationHISReq: QuotationAnalysisRequest;
  getPassPOItemID: any;
  getPassPOHeaderID: any;
  getPassItemHistoreyList: any;
  getQutationAnalysis:any;
  doInfiniteReq: any;
  constructor(public worklistMainService: WorklistMainService,
    private cs: CommonService,
    private ts: TranslatorService,
    private modalCtrl: ModalController,
    public worklistService: WorklistService) { 
    this.getPassPOItemID = this.cs.sharedService.getSharedData('passPOInfo',false);
    this.getPassPOHeaderID = this.cs.sharedService.getSharedData('passPOHeader',false);
    this.getPassItemHistoreyList = this.cs.sharedService.getSharedData('passItemHisList',false);
//itemHistoryRes
console.log("getPassPOItemID:  "+this.getPassPOItemID);
console.log("getPassPOItemID:  "+this.getPassPOHeaderID);

    this.POQuotationHISReq = new QuotationAnalysisRequest();
    this.POQuotationHISReq.P_PAGE_NUM = this.P_PAGE_NUM;
    this.POQuotationHISReq.P_PAGE_LIMIT = this.P_PAGE_LIMIT;
    this.POQuotationHISReq.P_ITEM_ID= this.getPassPOItemID;
    this.POQuotationHISReq.P_PO_HEADER_ID = this.getPassPOHeaderID;
    }

  ngOnInit() {
    



  this.getQutationAnalysisFun( this.getPassPOHeaderID,this.getPassPOItemID);


  }
  public getQutationAnalysisFun(headerID,itemID) {
    console.log("itemID"+ itemID+" headerID "+ headerID);
    this.IsReachEnd = false;
    const request = new QuotationAnalysisRequest();
    request.P_ITEM_ID=parseInt(itemID);
    request.P_PO_HEADER_ID=headerID;
    request.P_PAGE_LIMIT=100;
    request.P_PAGE_NUM=1;
    this.doInfiniteReq=request;
    this.worklistMainService.getQutationAnalysis(request).
      subscribe((result: QuotationAnalysisResponse) => {
        this.handleQutationAnalysisResult(result);
      });
  }
  handleQutationAnalysisResult(result) {
    if (this.cs.validResponse(result)) {

      if (this.cs.hasData(result.GetQuotationAnalysisList)) {

        //open page
        //set sheard data
        //
        this.getQutationAnalysis = result.GetQuotationAnalysisList;
        this.doInfiniteReq.P_PAGE_NUM++;
        let lastItemIndex = this.getQutationAnalysis.length - 1;
        if (result.GetQuotationAnalysisList[lastItemIndex]) {
          let lastitem = result.GetQuotationAnalysisList[lastItemIndex];
          if (lastitem.NO_OF_ROWS == lastitem.ROW_NUM) {
            this.IsReachEnd = true;
          } else {
            this.IsReachEnd = false;
          }
        }
      }
    }
  }



  doInfinite(infiniteScroll) {
    if (!this.IsReachEnd) {
      // this.getQutationAnalysis.P_PAGE_NUM = this.P_PAGE_NUM;
      this.worklistMainService.getQutationAnalysis(this.doInfiniteReq).
        subscribe((result: any) => {
          if (this.cs.validResponse(result)) {
            if (result.GetQuotationAnalysisList != undefined) {
              this.P_PAGE_NUM++;
              this.doInfiniteReq.P_PAGE_NUM = this.P_PAGE_NUM;
              (result.GetQuotationAnalysisList).forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.getQutationAnalysis.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
}
