import { Component, OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { WorklistMainService } from '../service/work-list.main.service';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { TextAreaInput } from 'src/app/uI-elements/text-area.input';
import { WorkListActionRequest } from '../models/NotificationActionReq';

@Component({
  selector: 'app-apply-action-modal',
  templateUrl: './apply-action-modal.component.html',
  styleUrls: ['./apply-action-modal.component.scss'],
})
export class ApplyActionModalComponent implements OnInit {
  private textArea: TextAreaInput;
  getpassResAttr: any;
  getPassNotificationDetails: any;
  getPassNotification: any;
  confirmMsg: string;
  getPassActionMode: any;
  P_RESPOND_ATTRIBUTES_TBL: any;
  selEmployeeID: any;
  hideForwordEmployee: any;
  private WorkListActionObj: WorkListActionRequest;
  messageSuccess: boolean;


  constructor( public ts: TranslatorService,private modalCtrl: ModalController, private cs: CommonService,public worklistMainService: WorklistMainService,
    ) {
      
      this.getPassNotificationDetails = this.cs.sharedService.getSharedData('passNotificationInfo',false);
      this.getpassResAttr = this.cs.sharedService.getSharedData('notificationDynamicAttributeArr' ,false);
      this.getPassNotification = this.cs.sharedService.getSharedData('ApplyActionModalPage',false);
      this.getPassActionMode = this.cs.sharedService.getSharedData('passActionMode',false);
      this.WorkListActionObj = new WorkListActionRequest();


      
     }

  ngOnInit() {
    document.getElementById("notificationDynamicFieldsModal").innerHTML="";
    console.log(this.getPassNotificationDetails);
    //this.applyAction(this.getPassNotificationDetails);
    this.notificationDynamicFields(this.getpassResAttr);
    console.log("confirmMsg" + this.confirmMsg)
    if (this.getPassActionMode === "APPROVED") {
      this.confirmMsg = this.ts.trPK('worklistMain', 'approveMsg')
    }
    else if (this.getPassActionMode === "REJECTED") {
      this.confirmMsg = this.ts.trPK('worklistMain', 'rejectMsg')

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

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

    }

  }

 

  
  notificationDynamicFields(notificationAttr){
    const containerId = 'notificationDynamicFieldsModal';
    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"){
        
      }
    }
  }

  closeModel() {
    this.modalCtrl.dismiss();

  }


  actionButton() {

    let ButtonAction: string = this.getPassActionMode;
    this.P_RESPOND_ATTRIBUTES_TBL = [];
    for (let i = 0; i < this.getpassResAttr.length; i++) {
      let obj: any = {};
      obj.ATTRIBUTE_NAME = this.getpassResAttr[i].ATTRIBUTE_NAME;
      if (this.getpassResAttr[i].ATTRIBUTE_TYPE === "number") {
        obj.ATTRIBUTE_NUMBER_VALUE = (document.getElementById(this.getpassResAttr[i].ATTRIBUTE_NAME) as HTMLInputElement).value;
      }
    
      else if (this.getpassResAttr[i].ATTRIBUTE_TYPE == "VARCHAR2") {
        obj.ATTRIBUTE_TEXT_VALUE = (document.getElementById(this.getpassResAttr[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);
    }
    
      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;

      
      

    this.applyAction(this.WorkListActionObj);




    
  }
  

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


  handleApplayActionResult(result) {
    if (this.cs.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.messageSuccess = true;

        setTimeout(() => {
          this.messageSuccess = false;
        }, 5000);
        this.cs.openNotificationPage();

         //  this.getNotificationCountAfterSubmit();
        //this.nextNotfification();
      }
    } // valid it
  }
}
