import {Component} from '@angular/core';
import {ActivatedRoute, NavigationEnd, Router} from "@angular/router";
import {Subscription} from "rxjs";
import {AuthenticationService} from "@app/_services";

@Component({
  templateUrl: 'administration-page.component.html',
  styleUrls: ['administration-page.component.scss']
})
export class AdministrationPageComponent {
  public tabs = <any>[];
  routeSubscription: Subscription;

  constructor(private route: ActivatedRoute, private router: Router, private authService: AuthenticationService) {
    this.routeSubscription = this.router.events.subscribe(event => {
      if (event instanceof NavigationEnd && (this.route.snapshot.paramMap.get('tab') !== this.tab?.name)) this.switchTab(this.route.snapshot.paramMap.get('tab'));
    });
  }

  get tab() {
    return this.tabs.filter(tab => {return tab.active})[0];
  }

  ngOnInit() {
    this.authService.user.subscribe(val => {
      this.makeTabs();
    });
  }

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

  makeTabs() {
    //this.tabs = [{name: 'company', title: 'Структура ФАУ «ФЦС»'}, {name: 'committee', title: 'Структура ТК 465'}];
    //if (this.authService.isSuperAdmin) this.tabs.push({name: 'site-pages', title: 'Структура сайта'});
    this.tabs = [{name: 'site-pages', title: 'Структура сайта'}];
    this.switchTab(this.route.snapshot.paramMap.get('tab'));
  }

  switchTab(name: string) {
    this.tabs.map(tab => {tab.active = tab.name === name});
  }

}
