import { WorklistMainService } from './../service/work-list.main.service';
import { Component, OnInit } from '@angular/core';
import { WorkListActionHistoryRequest } from '../models/ActionHistoryReq';
import { WorkListActionRequest } from '../models/NotificationActionReq';
import { WorKListRFCEmployeeResponse } from '../models/RFC-EmployeeRes';
import { WorklistService } from '../service/worklist.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';

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

  private WorkListActionHistoryObj: WorkListActionHistoryRequest;
  private WorkListActionObj: WorkListActionRequest;
  messageSuccess: boolean = false;

  getPassNotificationDetails: any;
  RFCEmployeeListListRes: any;
  radioButtonValue: any;
  pageNum: number = 1;
  seqNo: any;
  //forwordTo:any;
  IsReachEnd: boolean = false;
  comments: any;
  userNote: any;
  getpassResAttr: any = [];
  P_PAGE_NUM: number = 1;
  P_PAGE_LIMIT: number = 100;
  active: any;

  constructor(
    public worklistService: WorklistService,
    private cs: CommonService,
    private ts: TranslatorService,
    public workListMainService: WorklistMainService,
    public modalCtrl: ModalController
    ) {
    this.getPassNotificationDetails = this.cs.sharedService.getSharedData('passNotificationInfo');
    this.getpassResAttr = this.cs.sharedService.getSharedData('passResAttr');

    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;

    this.WorkListActionObj = new WorkListActionRequest();
    this.WorkListActionObj.P_NOTIFICATION_ID = this.getPassNotificationDetails.NOTIFICATION_ID;
    this.WorkListActionObj.P_FORWARD_TO_USER_NAME = "";
    this.WorkListActionObj.P_ACTION_MODE = "";
    this.WorkListActionObj.P_COMMENTS = "";
    this.WorkListActionObj.P_APPROVER_INDEX = null;
    this.WorkListActionObj.RespondAttributeList = this.getpassResAttr;
  }

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

  getRFCEmployee(WorkListActionHistoryObj) {
    this.IsReachEnd = false;
    this.worklistService.getRFCEmployeeList(WorkListActionHistoryObj).
      subscribe((result: WorKListRFCEmployeeResponse) => {
        this.handleWorkListRFCEmployeeResult(result);
      });
  }

  handleWorkListRFCEmployeeResult(result) {

    if (this.cs.validResponse(result)) {
      if (this.cs.hasData(result.GetRFCEmployeeListList)) {
        this.RFCEmployeeListListRes = result.GetRFCEmployeeListList;
        this.P_PAGE_NUM++;
        let lastItemIndex = this.RFCEmployeeListListRes.length - 1;
        if (result.GetRFCEmployeeListList[lastItemIndex]) {
          let lastitem = result.GetRFCEmployeeListList[lastItemIndex];
          if (lastitem.NO_OF_ROWS == lastitem.ROW_NUM) {
            this.IsReachEnd = true;
          } else {
            this.IsReachEnd = false;
          }
        }
      }
    }
  }
  radioChecked(v) {
    let emp: any = this.RFCEmployeeListListRes[v];
    this.seqNo = emp.SEQ;
    this.radioButtonValue = emp.USER_NAME;

  }

  submitAction() {
    if (this.radioButtonValue) {
      if (this.userNote) {
        this.WorkListActionObj.P_FORWARD_TO_USER_NAME = this.radioButtonValue;
        this.WorkListActionObj.P_ACTION_MODE = "RFC";
        this.WorkListActionObj.P_COMMENTS = this.userNote;// response Attr
        this.WorkListActionObj.P_APPROVER_INDEX = this.seqNo;

        this.workListMainService.actionButton(this.WorkListActionObj).
          subscribe((result: any) => {
            this.handleApplayActionResult(result);
          });
      } else {
        let msg: string = this.ts.trPK("replacementRoll", "enterNote");
        this.cs.showErrorMessageDialog(() => {
        }, this.ts.trPK('general', 'ok'), msg);

      }
    } else {
      let msg: string = this.ts.trPK("replacementRoll", "msgRFC");
      this.cs.showErrorMessageDialog(() => {
      }, this.ts.trPK('general', 'ok'), msg);

    }
  }

  handleApplayActionResult(result) {
    if (this.cs.validResponse(result)) {
      if (result.MessageStatus == 1) {
        //  let msg:string="";
        //  msg=this.translate.translate("general.success");
        // this.common.showAlert(msg);
        // this.navCtrl.push("HomePage");
       // this.cs.openHome();
        // here show succsfull msg
        this.messageSuccess = true;

        setTimeout(() => {
          this.messageSuccess = false;
          //this.cs.openNotificationPage();
          // this.modalCtrl.dismiss();
          this.modalCtrl.dismiss({
            'dismissed': true,
            data: "Success"
          });

        }, 5000);

      }
    } // valid it
  }

  doInfinite(infiniteScroll) {
    if (!this.IsReachEnd) {
      this.WorkListActionHistoryObj.P_PAGE_NUM = this.P_PAGE_NUM;
      this.worklistService.getRFCEmployeeList(this.WorkListActionHistoryObj).
        subscribe((result) => {
          if (this.cs.validResponse(result)) {
            if (result.RFCEmployeeList != undefined) {
              this.P_PAGE_NUM++
              (result.RFCEmployeeList).forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                }
                else {
                  this.IsReachEnd = false;
                }
                //this.WorkListResObj.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
  closeModal() {
    this.modalCtrl.dismiss();
  }

  select(selectEmp,index){
    console.log(selectEmp.USER_NAME);
    console.log(selectEmp.SEQ);

    this.radioButtonValue=selectEmp.USER_NAME
    this.seqNo = selectEmp.SEQ;
//////////////////////////////////////////////////////////////
    // let emp: any = this.RFCEmployeeListListRes[v];
    // this.seqNo = emp.SEQ;
    // this.radioButtonValue = emp.USER_NAME;


     this.active=index;
  }
}
