import { WorklistService } from './../service/worklist.service';
import { Component, OnInit } from '@angular/core';
import { WorkListReplacmentEmployeeRequest } from '../models/ReplacmentEmployeeReq';
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';

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

  radioButtonValue: any;
  getRepList: any;//WorKListReplacmentEmployeeResponse=new WorKListReplacmentEmployeeResponse();
  IsReachEnd: boolean = false;
  private getReplacmentEmployeeReq: WorkListReplacmentEmployeeRequest = new WorkListReplacmentEmployeeRequest();


  constructor(private cs: CommonService, private ts: TranslatorService, public modalCtrl: ModalController, public worklistService: WorklistService) {
    this.getReplacmentEmployeeReq = this.cs.sharedService.getSharedData('replacmentReqObj');
    this.getRepList = this.cs.sharedService.getSharedData('replacmentResObj');
  }

  ngOnInit() { }

  radioSelect(v) {
    this.radioButtonValue = v;
  }

  OkBtnModal() {
    if (!this.radioButtonValue) {
      let msg: string = this.ts.trPK("replacementRoll", "msg");
      this.cs.presentAlert(msg);
      return;
    } else {
      let data = this.radioButtonValue;
      this.modalCtrl.dismiss(data);
    }
  }

  closeBtnModal() {
    this.modalCtrl.dismiss("cancel");
  }

  doInfinite(infiniteScroll) {
    if (!this.IsReachEnd) {
      this.getReplacmentEmployeeReq.P_PAGE_NUM++;
      this.worklistService.getReplacmentEmployeeList(this.getReplacmentEmployeeReq).
        subscribe((result: any) => {
          if (this.cs.validResponse(result)) {
            if (result.ReplacementList != undefined) {
              (result.ReplacementList).forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.getRepList.push(element);
              });
            }// if list length >0
            else {
              this.IsReachEnd = true;
            }
          }// if response == 1
          //this.pageNum++;
          infiniteScroll.target.complete();
        });
    } else {
      if (infiniteScroll)
        infiniteScroll.target.complete();
    }
  }//end infiniteScroll

}
