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 { WorkListActionHistoryResponse } from '../models/ActionHistoryRes';

@Component({
  selector: 'app-work-list-action-history',
  templateUrl: './work-list-action-history.component.html',
  styleUrls: ['./work-list-action-history.component.scss'],
})
export class WorkListActionHistoryComponent implements OnInit {

  private WorkListActionHistoryObj: WorkListActionHistoryRequest;
  getPassNotificationDetails: any;
  actionHistoryRes: any;
  IsReachEnd: boolean = false;
  P_PAGE_NUM: number = 1;
  P_PAGE_LIMIT: number = 100;

  constructor(private cs: CommonService, private ts: TranslatorService, private modalCtrl: ModalController, public worklistService: WorklistService) {
    this.getPassNotificationDetails = this.cs.sharedService.getSharedData('passNotificationInfo');
    this.WorkListActionHistoryObj = new WorkListActionHistoryRequest();
    this.WorkListActionHistoryObj.P_PAGE_NUM = this.P_PAGE_NUM;
    this.WorkListActionHistoryObj.P_PAGE_LIMIT = this.P_PAGE_LIMIT;
    this.WorkListActionHistoryObj.P_NOTIFICATION_ID = this.getPassNotificationDetails.NOTIFICATION_ID;
  }

  ngOnInit() {
    this.getActionHistory(this.WorkListActionHistoryObj);
  }

  getActionHistory(WorkListActionHistoryObj) {
    this.IsReachEnd = false;
    this.worklistService.getActionHistory(WorkListActionHistoryObj).
      subscribe((result: WorkListActionHistoryResponse) => {
        this.handleWorkListActionHistoryResult(result);
      });


  }
  handleWorkListActionHistoryResult(result) {
    if (this.cs.validResponse(result)) {
      if (this.cs.hasData(result.GetActionHistoryList)) {
        this.actionHistoryRes = result.GetActionHistoryList;
        // this.P_PAGE_NUM++;
        this.WorkListActionHistoryObj.P_PAGE_NUM++;
        let lastItemIndex = this.actionHistoryRes.length - 1;
        if (result.GetActionHistoryList[lastItemIndex]) {
          let lastitem = result.GetActionHistoryList[lastItemIndex];
          if (lastitem.NO_OF_ROWS == lastitem.ROW_NUM) {
            this.IsReachEnd = true;
          } else {
            this.IsReachEnd = false;
          }
        }
      }
    }
  }

  async openNoteDetail(note) {
    // let modalPage = this.modalCtrl.create('ViewNoteModalPage', { textNote: note });
    // modalPage.present();

    this.cs.sharedService.setSharedData(note, 'ViewNoteModalPage')

    const modal = await this.modalCtrl.create({
      component: ViewNoteModalComponent
    });

    return await modal.present();
  }

  doInfinite(infiniteScroll) {
    if (!this.IsReachEnd) {
      // this.WorkListActionHistoryObj.P_PAGE_NUM = this.P_PAGE_NUM;
      this.worklistService.getActionHistory(this.WorkListActionHistoryObj).
        subscribe((result: any) => {
          if (this.cs.validResponse(result)) {
            if (result.GetActionHistoryList != undefined) {
              this.P_PAGE_NUM++;
              this.WorkListActionHistoryObj.P_PAGE_NUM = this.P_PAGE_NUM;

              (result.GetActionHistoryList).forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.actionHistoryRes.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

}
