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

@Component({
  selector: 'header',
  templateUrl: 'header.component.html',
  styleUrls: ['header.component.scss']
})
export class HeaderComponent {
  public menuItems = <any>[];
  public user: any;
  public loading: boolean = false;
  public error: string = '';
  subscription: Subscription;
  showBottom = false;

  constructor(private router: Router, private pagesService: PagesService, public authenticationService: AuthenticationService, private listsService: ListsService) {
    this.subscription = this.authenticationService.user.subscribe(user => {
      this.user = user;
    });
  }

  ngOnInit() {
    this.fetchMenu();
    let showSettingSite = localStorage.getItem('showSettingSite');
    if (showSettingSite === null || showSettingSite === 'no') {
      localStorage.setItem('showSettingSite', 'no');
      this.listsService.result('showSettingSite').next(false);
    }
  }

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


  fetchMenu() {
    let include = ['children.children'];
    this.pagesService.root({include: include.join(',')}).subscribe(res => {
      this.menuItems = res.data;
    });
  }

  settingsSite() {
    let showSettingSite = localStorage.getItem('showSettingSite');
    showSettingSite = showSettingSite === 'yes' ? 'no' : 'yes';
    localStorage.setItem('showSettingSite', showSettingSite);
    this.listsService.result('showSettingSite').next(showSettingSite==='yes');
  }

  login() {
    this.authenticationService.popup('login');
  }

  itemSelect(event:any){
    if(!event.children?.data.length) this.showBottom = false;
  }
}
