import { Component, OnInit, ElementRef } from "@angular/core";
import { CommonService } from "src/app/hmg-common/services/common/common.service";
import { TranslatorService } from "src/app/hmg-common/services/translator/translator.service";
import { WorkListBodyRequest } from "../models/NotificationBodyReq";
import { EITNotificatonBodyResponse } from "../models/EITNotificationBodyRes";
import { AbsenceNotificatonBodyResponse } from "../models/AbsenceNotificationBodyRes";
import { NotificatonButtonResponse } from "../models/NotificationButtonRes";
import { WorkListActionRequest } from "../models/NotificationActionReq";
import { WorkListButtonRequest } from "../models/NotificationButtonReq";
// import { isDate } from 'angular6-json-schema-form';
import { MenuResponse } from "src/app/hmg-common/services/menu/models/menu-response";
import { LoginRequest } from "src/app/hmg-common/services/authentication/models/login.request";
import { WorklistMainService } from "../service/work-list.main.service";
import { HomeComponent } from "src/app/notification/home/home.component";
import { TextInput } from 'src/app/uI-elements/text.input';
import { TextAreaInput } from 'src/app/uI-elements/text-area.input';
import { MenuService } from 'src/app/hmg-common/services/menu/menuservice.service';
import { Events, ModalController } from '@ionic/angular';
import { PONotificatonBodyResponse } from '../models/PONotificationBodyRes';
import { MONotificatonBodyResponse } from '../models/MONotificationBodyRes';
import { PRNotificatonBodyResponse } from '../models/PRNotificationBodyRes';
import { WorklistAttachService } from 'src/app/absence/service/work-list-attach.service';
import { NotificationGetAttachResponse } from 'src/app/eit/models/NotificationGetAttachRes';
import { PRNotificatonBodyList } from '../models/PRNotificationBodyList';
import { WorkListActionHistoryRequest } from '../models/ActionHistoryReq';
import { WorklistService } from '../service/worklist.service';
import { ViewNoteModalComponent } from '../view-note-modal/view-note-modal.component';
import { WorkListActionHistoryResponse } from '../models/ActionHistoryRes';
import { WorkListAttachViewComponent } from '../work-list-attach-view/work-list-attach-view.component';
import { ApplyActionModalComponent } from '../apply-action-modal/apply-action-modal.component';

@Component({
  selector: 'app-worklist-main-mr',
  templateUrl: './worklist-main-mr.component.html',
  styleUrls: ['./worklist-main-mr.component.scss'],
})
export class WorklistMainMRComponent implements OnInit {

  private WorkListBodyObj: WorkListBodyRequest;
  private WorkListButtonsObj: WorkListButtonRequest;
  private WorkListActionObj: WorkListActionRequest;
  private WorkListActionHistoryObj: WorkListActionHistoryRequest;
  private WorkListAttachObj: WorkListButtonRequest;
  private PRList:PRNotificatonBodyList
  public static PASS_NOTIFICATION_INFO = "passNotificationInfo";
  public static PASS_ACTION_MODE = "passActionMode";
  getPassNotificationDetails: any;
  public static PASS_RES_ATTR = "passResAttr";
  TransactionID: number = -999;
  notificationBodyRes: any;
  actionHistoryRes: any;
  notificationButtonRes: any;
  actionMode: any;
  pInformation: string = "";
  pQuestion: string = "";
  RespondAttributesListRes: any;
  schemaNotific: any;
  NotRespondAttributeList: any;
  Resp2_val: any;
  notExampleJsonObject: any;
  hideForwordEmployee: any;
  exampleJsonObject: any;
  dataOfAttr: any;
  P_RESPOND_ATTRIBUTES_TBL: any;
  selEmployeeID: any;
  actionType: string = "";
  notificationArray: any;
  notificationDynamicAttributeArr:any;
  private textInput: TextInput;
  private textArea: TextAreaInput;
  notificationCount: any;
  attachmentRes:any;
  PRHeader:any;//change the tpye later
  PRLines:any;//change the tpye later
  IsReachEnd: boolean = false;
  P_PAGE_NUM: number = 1;
  P_PAGE_LIMIT: number = 100;
  headerTotal: any;
  totalH: any;
  valueH: any;
  taxH: any;
  taxvalueH: any;
  NotificationGetRespondAttributesList: any;
  confirmMsg: string;
  delMsg: string;
  closeMsg: string;


  constructor(
    public common: CommonService,
    public ts: TranslatorService,
    public worklistMainService: WorklistMainService,
    public elementRef: ElementRef,
    public menuService: MenuService,
    public events: Events,
    public worklistAttachService: WorklistAttachService,
    private modalCtrl: ModalController,
    public worklistService: WorklistService
  ) {

  //this.getPassNotificationDetails = this.common.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.WorkListAttachObj = new WorkListButtonRequest();

    this.intializeNotificationDetail();

    let segment = document.querySelector('ion-segment');
    let slides = document.querySelector('ion-slides');
    
    segment.addEventListener('ionChange', (ev) => onSegmentChange(ev));
    slides.addEventListener('ionSlideDidChange', (ev) => onSlideDidChange(ev));
    
    // On Segment change slide to the matching slide
    function onSegmentChange(ev) {
      console.log("ev.detail.value"+ev.detail.value);
      slideTo(ev.detail.value);
    }
    
    function slideTo(index) {
      console.log("index: "+index);

      slides.slideTo(index);
    }
    
    // On Slide change update segment to the matching value
    async function onSlideDidChange(ev) {
      var index = await slides.getActiveIndex();
      console.log("index: "+index);
      clickSegment(index);
    }
    
    function clickSegment(index) {

      segment.value = index;
    }
    


  }
  

  intializeNotificationDetail() {
    document.getElementById("notificationDynamicFields").innerHTML="";
    console.log("intializeNotificationDetail");
    this.getPassNotificationDetails = this.common.sharedService.getSharedData(
      HomeComponent.NOTIFICATION_DATA,
      false
    );
    console.log(this.getPassNotificationDetails.ROW_NUM);
    this.notificationArray = this.common.sharedService.getSharedData(
      HomeComponent.NOTIFICATION_ARR,
      false
    );
    this.WorkListBodyObj = new WorkListBodyRequest();
    this.WorkListButtonsObj = new WorkListButtonRequest();

    this.WorkListActionObj = new WorkListActionRequest();

    this.WorkListBodyObj.P_TRANSACTION_ID = this.TransactionID;
    this.WorkListBodyObj.P_NOTIFICATION_ID = this.getPassNotificationDetails.NOTIFICATION_ID;
    this.WorkListBodyObj.P_PAGE_NUM = this.P_PAGE_NUM;;
    this.WorkListBodyObj.P_PAGE_LIMIT = this.P_PAGE_LIMIT;

    this.WorkListButtonsObj.P_NOTIFICATION_ID = this.getPassNotificationDetails.NOTIFICATION_ID;

    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.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.WorkListAttachObj.P_NOTIFICATION_ID = this.getPassNotificationDetails.NOTIFICATION_ID;

    this.getNotificationButtons(this.WorkListButtonsObj);
    this.getNotificationResAttr(this.WorkListButtonsObj);
    this.getActionHistory(this.WorkListActionHistoryObj);
    this.getAttachmentNotification(this.WorkListAttachObj);

    if (this.getPassNotificationDetails.REQUEST_TYPE == "EIT") {
      this.getEITNotificationDetails(this.WorkListBodyObj);
    } else if (this.getPassNotificationDetails.REQUEST_TYPE == "ABSENCE") {
      this.getAbsenceNotificationDetails(this.WorkListBodyObj);
    } else if (this.getPassNotificationDetails.REQUEST_TYPE == "PO"){
      this.getPONotificationDetails(this.WorkListBodyObj);       
    } else if (this.getPassNotificationDetails.REQUEST_TYPE == "MO"){
      this.getMONotificationDetails(this.WorkListBodyObj);
    } else if (this.getPassNotificationDetails.REQUEST_TYPE == "PR"){
      this.getPRNotificationDetails(this.WorkListBodyObj);  
    }
  }

  getPRNotificationDetails(notificationBodyObj){
    this.worklistMainService
    .getPRNotificationBody(notificationBodyObj)
    .subscribe((result: PRNotificatonBodyResponse) => {
      this.handleWorkListBodyResult(result, "PR");
    });
  }
  
   getMONotificationDetails(notificationBodyObj){
      this.worklistMainService
      .getMONotificationBody(notificationBodyObj)
      .subscribe((result: MONotificatonBodyResponse) => {
      this.handleWorkListBodyResult(result, "MO");
      });
    }
  
  getPONotificationDetails(notificationBodyObj){
    this.worklistMainService
    .getPONotificationBody(notificationBodyObj)
    .subscribe((result: PONotificatonBodyResponse) => {
     console.log(result.GetPoNotificationBodyList.POHeader[0].BUYER);
     this.handleWorkListBodyResult(result, "PO");
    });
  }

  getEITNotificationDetails(notificationBodyObj) {
    this.worklistMainService
      .getEITNotificationBody(notificationBodyObj)
      .subscribe((result: EITNotificatonBodyResponse) => {
        this.handleWorkListBodyResult(result, "EIT");
      });
  }

  getAbsenceNotificationDetails(notificationBodyObj) {
    this.worklistMainService
      .getAbsencesNotificationBody(notificationBodyObj)
      .subscribe((result: AbsenceNotificatonBodyResponse) => {
        this.handleWorkListBodyResult(result, "ABSENCE");
      });
  }

  handleWorkListBodyResult(result, Type) {
    this.notificationBodyRes = {};
    if (this.common.validResponse(result)) {
      this.pInformation = result.P_INFORMATION;
      this.pQuestion = result.P_QUESTION;
      if (Type == "MO") {
      if (result.GetMoNotificationBodyList) {
        this.notificationBodyRes =
          result.GetMoNotificationBodyList;
      }
    
    }
  }
  } //End handleWorkListBodyResult


 
  getNotificationButtons(notificationButtonsObj) {
    this.worklistMainService
      .getNotificationButtons(notificationButtonsObj)
      .subscribe((result: NotificatonButtonResponse) => {
        this.handleWorkListButtonsResult(result);
      });
  }

  handleWorkListButtonsResult(result) {
    if (this.common.validResponse(result)) {
      // this.sharedData.setSharedData(result, WorKListResponse.SHARED_DATA);
      if (result.GetNotificationButtonsList != null) {
        this.notificationButtonRes = result.GetNotificationButtonsList;
      } // if result == null
    } // valid it
  } // End handleWorkListButtonsResult

  applyAction(WorkListActionObj) {
    this.worklistMainService
      .actionButton(WorkListActionObj)
      .subscribe((result: any) => {
        this.handleApplayActionResult(result);
      });
  }

  handleApplayActionResult(result) {
    if (this.common.validResponse(result)) {
      if (result.MessageStatus == 1) {
        // this.navCtrl.push("HomePage");
        //this.common.openHome();
        //this.common.openNotificationPage();
        //   for( var i = 0; i < this.notificationArray.length; i++){ 
        //     if ( this.notificationArray[i].ROW_NUM === this.getPassNotificationDetails.ROW_NUM) {
        //       this.notificationArray.splice(i, 1); 
        //     }
        //  }
        this.getNotificationCountAfterSubmit();
        this.nextNotfification();
      }
    } // valid it
  }

  public nextNotfification() {
    //let itemExist = false;
    let itemNo = this.getPassNotificationDetails.ROW_NUM;
    itemNo += 1;
    if (itemNo > this.notificationArray.length) {
      this.common.openNotificationPage();
    } else {
      for (let i = 0; i <= this.notificationArray.length; i++) {
        if (this.notificationArray[i].ROW_NUM == itemNo) {
          this.common.sharedService.setSharedData(this.notificationArray[i], HomeComponent.NOTIFICATION_DATA);
          // itemExist = true;
          this.intializeNotificationDetail();
          break;
        }
      }
      //     if(itemExist==false){
      // this.nextNotfification();
      //     }
    }
  }

  actionButton() {
    let ButtonAction: string = this.actionType;
    var responseAttrDic = this.notExampleJsonObject;
    this.P_RESPOND_ATTRIBUTES_TBL = [];
    for (let i=0;i<this.notificationDynamicAttributeArr.length;i++) {
      let obj: any = {};
      obj.ATTRIBUTE_NAME = this.notificationDynamicAttributeArr[i].ATTRIBUTE_NAME;
      if (this.notificationDynamicAttributeArr[i].ATTRIBUTE_TYPE === "number") {
        obj.ATTRIBUTE_NUMBER_VALUE = (document.getElementById(this.notificationDynamicAttributeArr[i].ATTRIBUTE_NAME) as HTMLInputElement).value;
      }
      //  else if (isDate(responseAttrDic[key])) {
      //   obj.ATTRIBUTE_DATE_VALUE = responseAttrDic[key];
      // }
      else if(this.notificationDynamicAttributeArr[i].ATTRIBUTE_TYPE=="VARCHAR2") {
        obj.ATTRIBUTE_TEXT_VALUE =(document.getElementById(this.notificationDynamicAttributeArr[i].ATTRIBUTE_NAME) as HTMLInputElement).value;
      }
      this.P_RESPOND_ATTRIBUTES_TBL.push(obj);
    }
    let repUserName = this.selEmployeeID;
    if (this.hideForwordEmployee && this.hideForwordEmployee.ATTRIBUTE_NAME) {
      let obj: any = {};
      repUserName = "";
      obj.ATTRIBUTE_NAME = this.hideForwordEmployee.ATTRIBUTE_NAME;
      obj.ATTRIBUTE_TEXT_VALUE = this.selEmployeeID;
      this.P_RESPOND_ATTRIBUTES_TBL.push(obj);
    }
    if (
      ButtonAction == "APPROVE" ||
      ButtonAction == "REJECT" ||
      ButtonAction == "DEL" ||
      ButtonAction == "CLOSE"
    ) {
      this.WorkListActionObj.P_FORWARD_TO_USER_NAME = "";
      this.WorkListActionObj.P_ACTION_MODE = ButtonAction;
      this.WorkListActionObj.P_COMMENTS = "";
      this.WorkListActionObj.RespondAttributeList = this.P_RESPOND_ATTRIBUTES_TBL;
      //sheardData

      // this.common.sharedService.setSharedData(
      //   this.WorkListActionObj,
      //   WorklistMainMRComponent.PASS_NOTIFICATION_INFO
      // );
      if (ButtonAction == "APPROVE"){
        this.confirmMsg =this.ts.trPK('worklistMain', 'approveMsg')
      }
        else if (ButtonAction == "REJECT"){
          this.confirmMsg =this.ts.trPK('worklistMain', 'rejectMsg')

        }
        else if (ButtonAction == "DEL"){
          this.confirmMsg =this.ts.trPK('worklistMain', 'delMsg')

        }
        else if (ButtonAction == "CLOSE"){
          this.confirmMsg =this.ts.trPK('worklistMain', 'closeMsg')

        }
      
     this.common.confirmAlertDialogAction(
        () => {
          this.applyAction(this.WorkListActionObj);
        }, this.ts.trPK('general', 'ok'), 
        () => {}, this.ts.trPK('general', 'cancel'), 
        this.ts.trPK('vacation-rule', 'confirmation'),
         this.confirmMsg);



     
  
      // alert.onDidDismiss((data) => {
      //   if (data == true) {
      //     this.continueDelete(attach);
      //   }
      // });
      
      //this.openApplyModal(this.WorkListActionObj);
      
    

      ////////////this.applyAction(this.WorkListActionObj);
    }

    if (ButtonAction == "RFC") {
      // alert("Return For Correction");
      this.WorkListActionObj.P_ACTION_MODE = ButtonAction;
      // this.navCtrl.push("WorkListRfcPage", {
      //   passNotificationInfo: this.getPassNotificationDetails,
      //   passActionMode: ButtonAction,
      //   passResAttr: this.P_RESPOND_ATTRIBUTES_TBL
      // });
      this.common.sharedService.setSharedData(
        this.getPassNotificationDetails,
        WorklistMainMRComponent.PASS_NOTIFICATION_INFO
      );
      this.common.sharedService.setSharedData(
        ButtonAction,
        WorklistMainMRComponent.PASS_ACTION_MODE
      );
      this.common.sharedService.setSharedData(
        this.P_RESPOND_ATTRIBUTES_TBL,
        WorklistMainMRComponent.PASS_RES_ATTR
      );
      this.common.openWorklistRFCPage();
    } else if (ButtonAction == "ANSWER_INFO") {
      // this.navCtrl.push("WorkListReplacmentRollPage", {
      //   pQuestion: this.pQuestion,
      //   passNotificationInfo: this.getPassNotificationDetails,
      //   passActionMode: ButtonAction,
      //   passResAttr: this.P_RESPOND_ATTRIBUTES_TBL
      // });
      this.common.sharedService.setSharedData(this.pQuestion, "pQuestion");
      this.common.sharedService.setSharedData(
        this.getPassNotificationDetails,
        WorklistMainMRComponent.PASS_NOTIFICATION_INFO
      );
      this.common.sharedService.setSharedData(
        ButtonAction,
        WorklistMainMRComponent.PASS_ACTION_MODE
      );
      this.common.sharedService.setSharedData(
        this.P_RESPOND_ATTRIBUTES_TBL,
        WorklistMainMRComponent.PASS_RES_ATTR
      );
      this.common.openWorklistRollReplacement();
    } else if (
      ButtonAction == "DELEGATE" ||
      ButtonAction == "REQUEST_INFO" ||
      ButtonAction == "TRANSFER" ||
      ButtonAction == "TRANSFER_INFO"
    ) {
      //   alert("multi option: "+ ButtonAction);
      this.WorkListActionObj.P_ACTION_MODE = ButtonAction;
      // this.navCtrl.push("WorkListReplacmentRollPage", {
      //   passNotificationInfo: this.getPassNotificationDetails,
      //   passActionMode: ButtonAction,
      //   passResAttr: this.P_RESPOND_ATTRIBUTES_TBL
      // });
      this.common.sharedService.setSharedData(
        this.getPassNotificationDetails,
        WorklistMainMRComponent.PASS_NOTIFICATION_INFO
      );
      this.common.sharedService.setSharedData(
        ButtonAction,
        WorklistMainMRComponent.PASS_ACTION_MODE
      );
      this.common.sharedService.setSharedData(
        this.P_RESPOND_ATTRIBUTES_TBL,
        WorklistMainMRComponent.PASS_RES_ATTR
      );
      this.common.openWorklistRollReplacement();
    } else if (
      ButtonAction == "UPDATE_ACTION" ||
      ButtonAction == "CONTINUE_ACTION"
    ) {
      if (this.getPassNotificationDetails.REQUEST_TYPE == "EIT") {
        this.common.sharedService.setSharedData(
          this.notificationBodyRes,
          EITNotificatonBodyResponse.SHARED_DATA
        );
        this.common.sharedService.setSharedData(
          this.getPassNotificationDetails,
          EITNotificatonBodyResponse.NOT_WORKLIST
        );
        // this.navCtrl.push("EitUpdateListPage");
      } else if (this.getPassNotificationDetails.REQUEST_TYPE == "ABSENCE") {
        this.common.sharedService.setSharedData(
          this.notificationBodyRes,
          AbsenceNotificatonBodyResponse.SHARED_DATA
        );
        this.common.sharedService.setSharedData(
          this.getPassNotificationDetails,
          AbsenceNotificatonBodyResponse.NOT_WORKLIST
        );
        // this.navCtrl.push("SubmitAbsencePage", {
        //   dirfromNotificationPage: true,
        //   submitAbsObjList: this.notificationBodyRes[0].Collection_Notification
        // });
        this.common.openConfirmAbsece();
      }
    }
    else if (this.notificationButtonRes.length > 0 && !ButtonAction) {
      (this.elementRef.nativeElement.querySelectorAll(
        "ion-item"
      ) as HTMLElement[]).forEach(x => {
        if (x.classList.contains("requiredItem")) {
          x.classList.add("ng-touched");
          x.classList.remove("ng-untouched");
        }
      });
      this.common.presentAlert(
        this.ts.trPK("worklistMain", "actionRequird")
      );
    }
  }

  openActionHistory() {
    // this.navCtrl.push("WorkListActionHistoryPage", {
    //   passNotificationInfo: this.getPassNotificationDetails
    // });
    this.common.sharedService.setSharedData(
      this.getPassNotificationDetails,
      WorklistMainMRComponent.PASS_NOTIFICATION_INFO
    );
    this.common.openWorklistHistoryPage();
  }



  openSupportDocuments() {
    // this.navCtrl.push("WorkListAttachPage", {
    //   passNotificationInfo: this.getPassNotificationDetails
    // });
    this.common.sharedService.setSharedData(
      this.getPassNotificationDetails,
      WorklistMainMRComponent.PASS_NOTIFICATION_INFO
    );
    this.common.openWorklistAttachPage();
  }

  getNotificationResAttr(notificationButtonsObj) {
    this.worklistMainService
      .notificationResponseAttr(notificationButtonsObj)
      .subscribe((result: NotificatonButtonResponse) => {
        this.handleNotificationResAttrResult(result);
      });
  }

  handleNotificationResAttrResult(result) {
    if (this.common.validResponse(result)) {
      // this.sharedData.setSharedData(result, WorKListResponse.SHARED_DATA);
      this.NotRespondAttributeList =
        result.NotificationGetRespondAttributesList;
      if (
        result.NotificationRespondRolesList != "" &&
        result.NotificationRespondRolesList[0].ATTRIBUTE_NAME != null &&
        result.NotificationRespondRolesList[0].ATTRIBUTE_NAME != undefined
      ) {
        this.Resp2_val = result.NotificationRespondRolesList[0].ATTRIBUTE_NAME;
        this.hideForwordEmployee = result.NotificationRespondRolesList[0];
      }
      this.notificationDynamicFields(result.NotificationGetRespondAttributesList);
      this.NotificationGetRespondAttributesList=result.NotificationGetRespondAttributesList;

      this.common.sharedService.setSharedData(
        this.NotificationGetRespondAttributesList,
        WorklistMainMRComponent.PASS_RES_ATTR
      );

      this.notificationDynamicAttributeArr=result.NotificationGetRespondAttributesList;
      if (result.P_Schema) this.schemaNotific = JSON.parse(result.P_Schema);
    } // valid it
  } // End handleWorkListButtonsResult

  notificationDynamicFields(notificationAttr){
    const containerId = 'notificationDynamicFields';
    for(let i=0;i<notificationAttr.length;i++){
      if(notificationAttr[i].ATTRIBUTE_TYPE=="VARCHAR2"){
        this.textArea = new TextAreaInput(notificationAttr[i].ATTRIBUTE_DISPLAY_NAME, notificationAttr[i].ATTRIBUTE_NAME, "", containerId,"","","");
      }else if(notificationAttr[i].ATTRIBUTE_TYPE=="ROLE"){
        
      }else if(notificationAttr[i].ATTRIBUTE_TYPE=="DATE"){
        
      }else if(notificationAttr[i].ATTRIBUTE_TYPE=="NUMBER"){
        
      }
    }
  }

  openNotificationBody() {
    if (this.getPassNotificationDetails.REQUEST_TYPE == "EIT") {
      this.common.sharedService.setSharedData(
        this.notificationBodyRes,
        EITNotificatonBodyResponse.SHARED_DATA
      );
      this.common.sharedService.setSharedData(
        this.getPassNotificationDetails,
        EITNotificatonBodyResponse.NOT_WORKLIST
      );
      // this.navCtrl.push("WorkListDetailsPage", { NotBodyType: "EIT" });
      this.common.sharedService.setSharedData("EIT", "NotBodyType");
      this.common.openNotificationDetailsPage();
    } else if (this.getPassNotificationDetails.REQUEST_TYPE == "ABSENCE") {
      this.common.sharedService.setSharedData(
        this.notificationBodyRes,
        AbsenceNotificatonBodyResponse.SHARED_DATA
      );
      this.common.sharedService.setSharedData(
        this.getPassNotificationDetails,
        AbsenceNotificatonBodyResponse.NOT_WORKLIST
      );
      this.common.sharedService.setSharedData("ABSENCE", "NotBodyType");
      //this.navCtrl.push("WorkListDetailsPage", { NotBodyType: "ABSENCE" });
      this.common.openNotificationDetailsPage();
    }
  }

  getNotificationCountAfterSubmit() {
    const req: any = {};
    this.menuService.getNotificationCount(req).subscribe((result: any) => {
      if (this.common.validResponse(result)) {
        this.notificationCount =
          result.GetOpenNotificationsNumList.P_OPEN_NOTIFICATIONS_NUM;
        if (this.notificationCount <= 0) {
          this.notificationCount = null;
        }
        this.events.publish("getNotCount", this.notificationCount);
      }
    });
  }



  
   getAttachmentNotification(WorkListAttachObj) {

    this.worklistAttachService.getAttach(WorkListAttachObj).
      subscribe((result: NotificationGetAttachResponse) => {
        this.handleWorkListAttachResult(result);
      });
  }

  handleWorkListAttachResult(result) {
    if (this.common.validResponse(result)) {
      // this.sharedData.setSharedData(result, WorKListResponse.SHARED_DATA);
      if (result.GetAttachementList != null) {
        this.attachmentRes = result.GetAttachementList;
      } // if result == null
    } // valid it
  }

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


  }
  handleWorkListActionHistoryResult(result) {
    if (this.common.validResponse(result)) {
      if (this.common.hasData(result.GetActionHistoryList)) {
        this.actionHistoryRes = result.GetActionHistoryList;
        this.P_PAGE_NUM++;
        this.WorkListActionHistoryObj.P_PAGE_NUM = this.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,sender) {
    // let modalPage = this.modalCtrl.create('ViewNoteModalPage', { textNote: note });
    // modalPage.present();
    console.log("note"+note);

    this.common.sharedService.setSharedData(note, 'ViewNoteModalPage')
    this.common.sharedService.setSharedData(sender,'ViewNoteModalPageSender')

    const modal = await this.modalCtrl.create({
      component: ViewNoteModalComponent,
      backdropDismiss:false,

    });
    modal.cssClass = 'note-modal';

    return await modal.present();
  }

  doInfinite(infiniteScroll) {
    console.log("doInfinite action history MR");

    if (!this.IsReachEnd) {
      this.worklistService.getActionHistory(this.WorkListActionHistoryObj).
        subscribe((result: any) => {
          if (this.common.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

//**********Attachment *****************//


  async OpenAttachFiles(value, Type) {
    // let modal: Modal = this.modalCtrl.create('WorkListAttachViewPage', { displayData: value, TypeData: Type });
    // modal.present();

    this.common.sharedService.setSharedData({ displayData: value, TypeData: Type }, 'WorkListAttachViewPage')
    const modal = await this.modalCtrl.create({
      component: WorkListAttachViewComponent
    });

    return await modal.present();
  }



  async openApplyModal(WorkListActionObj) {
    // let modalPage = this.modalCtrl.create('ViewNoteModalPage', { textNote: note });
    // modalPage.present();
    // console.log("note"+note);
    this.common.sharedService.setSharedData( this.getPassNotificationDetails,WorklistMainMRComponent.PASS_NOTIFICATION_INFO);
    this.common.sharedService.setSharedData(WorkListActionObj, 'ApplyActionModalPage')
    // this.common.sharedService.setSharedData(
    //   this.WorkListActionObj,
    //   WorklistMainMRComponent.PASS_NOTIFICATION_INFO
    // );

    const modal = await this.modalCtrl.create({
      component: ApplyActionModalComponent,
      backdropDismiss:false,

    });
    modal.cssClass = 'note-modal';
 
    modal.onDidDismiss()
      .then((data) => {
        console.log(data); // Here's your selected user!
    });

  //   data => {
  //     console.log('MODAL DATA', data);
  // });
    // const { data } = await this.modalCtrl.onWillDismiss();
    // console.log(data);
    return await modal.present();
    
  }



  doInfiniteNotificationBody(infiniteScroll) {
    console.log("doInfiniteNotificationBody MR");
    if (!this.IsReachEnd) {
      this.worklistMainService.getMONotificationBody(this.WorkListActionHistoryObj).
        subscribe((result: any) => {
          if (this.common.validResponse(result)) {
            if (result.GetMoNotificationBodyList != undefined) {
              this.P_PAGE_NUM++;
              this.WorkListBodyObj.P_PAGE_NUM = this.P_PAGE_NUM;
              (result.GetMoNotificationBodyList).forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.notificationBodyRes.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




  loadMoreNotificationBody() {
    console.log("loadMoreNotificationBody MR");
    if (!this.IsReachEnd) {
      console.log("this.IsReachEnd"+ this.IsReachEnd);

      this.worklistMainService.getMONotificationBody(this.WorkListBodyObj).
        subscribe((result: any) => {
          if (this.common.validResponse(result)) {
            if (result.GetMoNotificationBodyList != undefined) {
              this.P_PAGE_NUM++;
              this.WorkListBodyObj.P_PAGE_NUM = this.P_PAGE_NUM;

              (result.GetMoNotificationBodyList).forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.notificationBodyRes.push(element);
              }, (Error) => console.log(Error), () => 
              //infiniteScroll.target.complete()
              console.log("test")
              );
            }// if list length >0
            else {
              this.IsReachEnd = true;
            }
          }// if response == 1
          //this.pageNum++;
         // infiniteScroll.target.complete();

        });
    } else {
      // if (infiniteScroll)
      //   infiniteScroll.target.complete();
    }
  }//end infiniteScroll
  loadMoreActionHistory() {
    console.log("doInfinite action history MR");

    if (!this.IsReachEnd) {
      this.worklistService.getActionHistory(this.WorkListActionHistoryObj).
        subscribe((result: any) => {
          if (this.common.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()
              console.log("TEST"));
            }// if list length >0
            else {
              this.IsReachEnd = true;
            }
          }// if response == 1
          //this.pageNum++;
        //  infiniteScroll.target.complete();

        });
    } else {
      //if (infiniteScroll)
        //infiniteScroll.target.complete();
    }
  }//end infiniteScroll




}
