import { Component, OnInit } from '@angular/core';
import { Platform, Events, MenuController } from '@ionic/angular';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { AuthenticationService } from 'src/app/hmg-common/services/authentication/authentication.service';
import { AuthenticatedUser } from 'src/app/hmg-common/services/authentication/models/authenticated-user';
import { SharedDataService } from 'src/app/hmg-common/services/shared-data-service/shared-data.service';
import { MowadhafiService } from '../mowadhafi.service';
import { Location } from '@angular/common';
import { MenuResponse } from 'src/app/hmg-common/services/menu/models/menu-response';
import { LoginModel } from 'src/app/authentication/models/LoginModel';
import { SendActivationByType } from 'src/app/authentication/models/sendActivationByType';
import { FileUploader } from 'ng2-file-upload';

@Component({
  selector: 'app-hr-request',
  templateUrl: './hr-request.component.html',
  styleUrls: ['./hr-request.component.scss'],
})
export class HRRequestComponent implements OnInit {

  public EmpName: string;
  public EmpNum: string;
  public EmpGroup: string;
  public EmpMobile: string;
  public EmpJob: string;
  public EmpOrgName: string;
  public EmpPayrol: string;
  public EmpPosition: string;
  public EmpEmail: string;
  public EmpCatMeaning: string;
  public EmpGRADENAME: string;
  public EmpLocation: string;
  public EmpImg: string;
  public userImage: any = '../assets/imgs/profile.png';
  public loginData = new LoginModel();
 
 
 
  
    public direction: string;
    constructor(
      public ts: TranslatorService,
      public cs: CommonService,
      public authService: AuthenticationService,
      public sharedData: SharedDataService,
      public events: Events,
      public MowadhafiService: MowadhafiService,
      private location: Location,
      private menu: MenuController,
    
    ) {
      this.direction = TranslatorService.getCurrentLanguageName();
     }

  ngOnInit() {
    // this.getUserInformation();
    this.getUserDATA();
    this.checkERM('Before Service Initiation');


  }

  goback() {
    this.location.back();

  }

  getUserInformation() {
    // const request = {
    //   // P_SELECTED_EMPLOYEE_NUMBER:"121816",
    //   P_SELECTED_RESP_ID: -999,
    //   P_PAGE_NUM: 1,
    //   P_PAGE_LIMIT: 1

    // };
    // this.MowadhafiService.getUserInfo(request, () => { }, this.ts.trPK('general', 'retry')).subscribe((result) => {
    //   this.handleUserInfoResult(result);

    // })
  }

  getUserDATA() {
    const userData = this.cs.sharedService.getSharedData('user-info', false);
    this.EmpName = userData.EMPLOYEE_NAME;
    this.EmpNum = userData.EMPLOYEE_NUMBER;
    this.EmpGroup = userData.BUSINESS_GROUP_NAME;
    this.EmpMobile = userData.EMPLOYEE_MOBILE_NUMBER;
    // this.EmpJob = userData.JOB_NAME;
    this.EmpOrgName = userData.ORGANIZATION_NAME;
    this.EmpPayrol = userData.PAYROLL_NAME;
    // this.EmpPosition = userData.POSITION_NAME;

    let jobTitle = userData.POSITION_NAME
    jobTitle = jobTitle.split('.');
    if (jobTitle && jobTitle.length > 1) {
      this.EmpPosition = jobTitle[0] + " " + jobTitle[1];
    }​ else {
      this.EmpPosition = '';
    }

    this.EmpEmail = userData.EMPLOYEE_EMAIL_ADDRESS;
    this.EmpCatMeaning = userData.EMPLOYMENT_CATEGORY_MEANING;
    this.EmpGRADENAME = userData.GRADE_NAME;
    this.EmpLocation = userData.LOCATION_NAME;
    this.EmpImg = userData.EMPLOYEE_IMAGE
    // console.log("total no "+this.EmpTotal)
  }


  public createRequest() {
    this.cs.openHRRequestFormPage();
    // this.menu.toggle();
  }
  checkERM(status) {

    this.authService.checkAds({
      EmployeeNumber: '',
      ItgEnableAt: status, //After Service Submission
      ItgServiceName: "Ticket"
    }, () => { }, this.ts.trPK('general', 'ok')).subscribe(res => {

      var result = JSON.parse(res.Mohemm_ITG_ResponseItem).result.data;

      this.cs.sharedService.setSharedData(
        result,
        AuthenticationService.SERVEY_DATA
      )
      if (!CommonService.SKIP && result) {
        if (result.notificationType == 'Survey') {
          this.cs.navigateForward('/erm-channel/survey');
        } else {
          this.authService.adsDetails({
            "ItgNotificationMasterId": result['notificationMasterId']
          },
            () => { }, this.ts.trPK('general', 'ok')
          ).subscribe((result) => {
            var data = result.Mohemm_ITG_ResponseItem;
            if (data) {
              this.cs.sharedService.setSharedData(
                JSON.parse(data),
                AuthenticationService.ADS_DATA
              );
              this.cs.navigateForward('/erm-channel/home');
            }

          })
        }
      }
    })

  }

}
