import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Router } from '@angular/router';

import { routes } from '../../../../consts';
import { User } from '../../../../pages/auth/models';
import { MenuService } from 'src/app/_providers/_services/menu.service';
import { MatDialog } from '@angular/material/dialog';
import { UserDto } from 'src/app/_dto/user.dto';
import { PcdUserDto } from 'src/app/_dto/pcduser.dto';
import { apiUrl } from 'src/app/_providers/_resources/api-url.properties';
import { MasterService } from 'src/app/_providers/_services/master.service';
import { UiService } from 'src/app/_providers/_services/ui.service';
import { AuthService } from 'src/app/_providers/_services/auth.service';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss']
})
export class UserComponent {
  @Input() user: User;
  @Output() signOut: EventEmitter<void> = new EventEmitter<void>();
  signedOut : boolean
  public routes: typeof routes = routes;
  public flatlogicEmail: string = "https://flatlogic.com";
  adminUser: boolean = false;
  username: string;
  secuLevel: string;
  dialogRef: any;
  userDetails = new PcdUserDto()
  prCmpltCertFilePath: any;
  prCmpltCertdocSize: any;
  cmpPrfilePath: any;
  cmpPrdocSize: any;
  userId: string;
  email: string;
  
  public signOutEmit(): void {
    localStorage.removeItem('emailId')
    this.signOut.emit();
    if(this._router.url == '/auth/login' || this._router.url == '/auth/admin-home')
    this.signedOut = true
  // console.log('user==email======',this._router);
    
  }
 constructor(public _router : Router,
             public dialog :MatDialog,
             public _masterService :MasterService,
             public _ui:UiService,
             public _authService : AuthService){
  // console.log('user==email======',this._router);
        this.userId =localStorage.getItem('userId')
 }

ngOnInit(){
  this.getUser();
  this.getEmail();
  this.getData();
}


getData(){
  
  let body ={
     
    username:this.email
  } 

  this._masterService.saveMasterData(apiUrl.userInfo,body).then(res=>{
    this.userDetails = res;

  })

}
 getUser(){
  this.secuLevel = localStorage.getItem('seculevel')
  if(this.secuLevel && this.secuLevel == 'B'){
    
    if(this.secuLevel === 'B'){
      this.adminUser = true
    }
    else{
      this.adminUser= false
    }
    // this.cdr.detectChanges();
  }
  this.username =  localStorage.getItem('username')
  if(this.username)
  return this.username
  else
  return ''
 }
 getEmail(){
 let email =  localStorage.getItem('emailId')
 this.email =  email;
  if(email)
  return email
  else
  return ''
 }


 openEditProfile(templateRef){
  this.dialogRef = this.dialog.open(templateRef, {
    width: '400px',
    disableClose: false,
    panelClass: 'profile-dialog-container' 
  });
 }

 closeDialog() {
  this.dialogRef.close();
}


removeCmpPrFile(){
  this._masterService.deleteMasterData(apiUrl.cmpPrFileUpload,this.userDetails.companyProfile ).then(res=>{
    this.userDetails.companyProfile  = null
    this.cmpPrfilePath = null
    })  
}
uploadCmpPrFiles(event){
  console.log('event ---> ',event)
  let file: File | undefined = event.target.files[0];
  if (!file) {
    return;
  }

  const fd = new FormData();
fd.append('file', file );
   
this._masterService.saveMasterData(apiUrl.cmpPrFileUpload,fd).then(res=>{
  this.userDetails.companyProfile = res.fileName;
 this.cmpPrfilePath = res.filePath;
 this.cmpPrdocSize = res.fileSizeKB;

})
} 



removeprCmpltCertFile(){
this._masterService.deleteMasterData(apiUrl.projectCompleteCertFile,this.userDetails.projectCompletedCert ).then(res=>{
  this.userDetails.projectCompletedCert  = null
  this.prCmpltCertFilePath = null
  }) 
}

uploadprCmpltCertFiles(event){
console.log('event ---> ',event)
let file: File | undefined = event.target.files[0];
if (!file) {
  return;
}

const fd = new FormData();
fd.append('file', file );
 
this._masterService.saveMasterData(apiUrl.projectCompleteCertFile,fd).then(res=>{
this.userDetails.projectCompletedCert = res.fileName;
this.prCmpltCertFilePath = res.filePath;
this.prCmpltCertdocSize = res.fileSizeKB;

})
}

saveData(){
this._masterService.updateMasterData(apiUrl.userDetails,this.userDetails,this.userId).then(res=>{
  this._ui.toastMessage('User Details edited successfully');
  this.dialogRef.close();
  
},err=>{
  this._ui.toastMessage(err.error.message);
  this.dialogRef.close();

})
}

}
