import {Component, Input} from '@angular/core';
import {AuthenticationService} from "@app/_services";
import {Router} from "@angular/router";
import {Subscription} from "rxjs";
import { PagesService } from '@app/_services/pages.service';


@Component({
  selector: 'header-user-bar',
  templateUrl: 'header-user-bar.component.html',
  styleUrls: ['header-user-bar.component.scss']
})
export class HeaderUserBarComponent {
  public user: any;
  public menuItems = <any>[];
  public ddHidden = true;
  subscriptionUser: Subscription;

  constructor(
    public authService: AuthenticationService, 
    private router: Router,
    private pagesService: PagesService) {
      this.subscriptionUser = this.authService.user.subscribe(user => {
        console.log(user)
        this.user = user;
      });
  }

  get avatar() {
    return this.user?.avatar?.data;
  }
  get isJournalVisible() {
    return !this.authService.isMainCompanyMember || this.authService.isExpert || this.authService.isAdmin;
  }

  ngOnInit() {
    
  }

  ngOnDestroy() {
    this.subscriptionUser?.unsubscribe();
  }


  close() {
    setTimeout(() => {
      this.ddHidden = true;
    }, 10);    
  }


  logout() {
    if (confirm('Вы деествительно хотите выйти из системы?')) {
      this.pagesService.editMode.next(false)
      this.authService.logout();
      this.ddHidden = true;
      this.router.navigate(['']).then();
    }
  }

  link(link: string) {
    this.close()
    this.router.navigate([link]).then();
  }
}
