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 { PerformanceAppraisalResponse } from 'src/app/hmg-common/services/dashbored/performance-appraisal.response';
import { DashboredService } from 'src/app/hmg-common/services/dashbored/dashbored.service';
import { SharedDataService } from 'src/app/hmg-common/services/shared-data-service/shared-data.service';
import { DomSanitizer } from '@angular/platform-browser';
// import { DomSanitizer } from '@angular/platform-browser';


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


  User_name_Emp: string = "";
  User_Job_name: string;

  private menu: MenuController;
  setImage:any;
  // imageSrc: any = "../assets/imgs/profile.png";
  user_image: any = "../assets/imgs/profile.png";
  personalInfo: any;
  appraisalArr :any =[]; 
  public performanceData: any=[];
  public static PERFORMANCE_DATA = 'perAppData';
  
  constructor(
    public ts: TranslatorService,
    public cs: CommonService,
    public authService: AuthenticationService,
    public DS :DashboredService,
    public sharedData: SharedDataService,
    public events: Events,
    private sanitizer: DomSanitizer,
  //  private events: Events,
  //  private sanitizer: DomSanitizer,

  )
  {
  this.events.subscribe("img-change", displayImg => {
          console.log("app compont: "+displayImg);
          this.user_image = this.sanitizer.bypassSecurityTrustUrl("data:Image/*;base64,"+displayImg);
    
        });


  }

  ngOnInit() {
     this.getProfile();
  }

  getProfile() {
    console.log("getProfile");
    this.authService
      .loadAuthenticatedUser()
      .subscribe((user: AuthenticatedUser) => {
        if (user) {
          this.personalInfo = user;
          this.User_name_Emp=this.personalInfo.EMPLOYEE_NAME;
          this.User_Job_name=this.personalInfo.JOB_NAME;
          if(this.cs.getUpdateImage().status){
            this.user_image =this.sanitizer.bypassSecurityTrustUrl("data:image/png;base64,"+this.cs.getUpdateImage().img);
           }else{
          this.user_image = user.EMPLOYEE_IMAGE
          ? "data:image/png;base64," + user.EMPLOYEE_IMAGE
          : "../assets/imgs/profile.png";
           }
          console.log("name: "+ this.personalInfo.EMPLOYEE_NAME);
          console.log("user name: "+ user.EMPLOYEE_NAME);
          console.log("name: "+ this.personalInfo.JOB_NAME);
          console.log("job name: "+ user.JOB_NAME);
         

      }});
}

openEditprofile(){
  this.cs.openProfile();
}

openPerormance(){
  this.showPerformanceAppraisal();
  
}

showPerformanceAppraisal(){
  this.DS.getPerformanceAppraisal(()=>{
  this.showPerformanceAppraisal();
  }).subscribe((result: PerformanceAppraisalResponse) => {
    this.handlePerformanceAppraisalResult(result);
  
  });
}
  
private handlePerformanceAppraisalResult(result){
  if (this.cs.validResponse(result)) {
    if (this.cs.hasData(result.GetPerformanceAppraisalList)) {
      this.appraisalArr =result.GetPerformanceAppraisalList;
      this.performanceData=[];

      for(let i=0;i<this.appraisalArr.length;i++){
        this.performanceData.push({name:this.appraisalArr[i].APPRAISAL_YEAR,value :this.appraisalArr[i].APPRAISAL_SCORE});
      }
      console.log("PerformanceAppraisalResponse");
      this.sharedData.setSharedData(this.performanceData, PerformanceAppraisalResponse.PERFORMANCE_DATA);
      //localStorage.setItem("performanceData", this.performanceData)
      this.cs.openPerformanceevaluation();
    }
  }
}
}
