import { Component, ViewChild } from '@angular/core';
import { Router, RouterLink, RouterLinkActive } from '@angular/router';
import { CommonNzModule } from '../../../../common.nz.module';
import { BasicDataService } from '../../../../services/basic.service';
import { SettingService } from '../../../../services/setting.service';
import { StorageService } from '../../../../services/storage.service';
import { CustomDrawerComponent } from '../../../../shared/custom-drawer/custom-drawer.component';
import { IssueReportFormComponent } from '../../../../shared/issue-report-form/issue-report-form.component';
import { getRoleLabel, UserRole } from '../../user/user.const';

export interface IPath {
  path: string;
  label: string;
  icon?: string;
  weight?: number;
}

@Component({
  selector: 'manager-header',
  standalone: true,
  imports: [CommonNzModule, RouterLink, RouterLinkActive, CustomDrawerComponent, IssueReportFormComponent],
  templateUrl: './header.component.html',
  styleUrl: './header.component.less',
})
export class HeaderComponent {
  @ViewChild('issueReportForm') issueReportForm!: IssueReportFormComponent;
  menuList: IPath[] = [
    {
      path: '/manager/workbench',
      label: '我的工作台',
      icon: 'assets/images/nav/workbench.png',
    },
    {
      path: '/manager/risk',
      label: '风险管控',
      icon: 'assets/images/nav/knowledge.png',
    },
    {
      path: '/manager/potential-hazard',
      label: '隐患管理',
      icon: 'assets/images/nav/potential-hazard.png',
    },
    {
      path: '/manager/user',
      label: '用户管理',
      icon: 'assets/images/nav/user.png',
    },
    {
      path: '/manager/basic-data',
      label: '基础数据管理',
      icon: 'assets/images/nav/basic-data.png',
    },
  ];
  issueReport = {
    visible: false,
  };
  constructor(
    readonly setting: SettingService,
    private readonly storage: StorageService,
    private readonly basic: BasicDataService,
    private readonly router: Router
  ) {}
  handleLogout(ev: MouseEvent) {
    ev.preventDefault();
    this.setting.clear();
    this.storage.token = '';
    this.router.navigate(['/login']);
  }
  openIssueReportDrawer() {
    this.setting.getUsersOfCurrentCompany();
    this.issueReport.visible = true;
  }
  closeIssueReportDrawer() {
    this.issueReport.visible = false;
    this.issueReportForm.reset();
  }

  get user() {
    return this.setting.user;
  }
  get roleText() {
    const { roles } = this.user || {};
    if (!roles || !roles.length) return '';
    return roles
      .map(r => getRoleLabel(r as UserRole))
      .filter(Boolean)
      .join(',');
  }
}
