
import {
  AbstractControl, FormControl,
  FormGroup,
  NonNullableFormBuilder, ReactiveFormsModule,
  ValidatorFn,
  Validators
} from "@angular/forms";
import {Component, inject} from '@angular/core';
import {DATA, INIT_FLAG, LOGIN_DATA, USER_TOKEN} from '../../../data/login/localStorage.namespace';
import {LoginService} from '../../../services/login.service';
import {NzConfigService} from 'ng-zorro-antd/core/config';
import {LocalStorageService} from '../../../utils/localStorage.service';
import {Router, RouterLink} from '@angular/router';
import {NzMessageService} from 'ng-zorro-antd/message';
import {NzContentComponent, NzHeaderComponent, NzLayoutComponent, NzSiderComponent} from 'ng-zorro-antd/layout';
import {NzColDirective, NzRowDirective} from 'ng-zorro-antd/grid';
import {NgClass, NgForOf} from '@angular/common';
import {NzDropDownDirective, NzDropdownMenuComponent} from 'ng-zorro-antd/dropdown';
import {NzMenuDirective, NzMenuItemComponent, NzSubMenuComponent} from 'ng-zorro-antd/menu';
import {NzIconDirective} from 'ng-zorro-antd/icon';
import {TabComponent} from '../tab/tab.component';



@Component({
  selector: 'app-index',
  standalone: true,
  imports: [
    NzLayoutComponent,
    NzHeaderComponent,
    NzRowDirective,
    NzColDirective,
    NgClass,
    NgForOf,
    NzDropDownDirective,
    NzMenuDirective,
    NzSiderComponent,
    NzIconDirective,
    NzSubMenuComponent,
    NzMenuItemComponent,
    RouterLink,
    NzContentComponent,
    TabComponent,
    NzDropdownMenuComponent


  ],
  templateUrl: './index.component.html',
  styleUrl: './index.component.scss'
})
export class IndexComponent {
  // 菜单数据
  menuData: any = [{
    'menuName' : '首页',
    'selected' : true,
  }];
  userInfo: any;
  // 左侧菜单栏数据
  leftMenuData: any = [];
  // 侧边菜单展示开关
  isCollapse = true;

  // 当前顶级菜单
  currentParentMenu: any = {};
  private login = inject(LoginService);

  constructor(
    private message: NzMessageService ,
    private storage: LocalStorageService ,
    private router: Router,  // 路由
  ) {

    // @ts-ignore
    this.menuData = this.menuData.concat(this.storage.getItem(DATA)['menuTree']);
    // @ts-ignore
    this.userInfo = this.storage.getItem(DATA)['account'];
  }


  // 选择操作
  isSelected(item: any) {
    this.menuData.map((data: any) => {
      data.selected = data.menuName === item.menuName;
    });
    if (item.menuName === '首页') {
      // 激活首页路由
      this.router.navigateByUrl('/admin/index').finally();
    }

    this.currentParentMenu = item;
    this.leftMenuData = item['childMenuList'];
    this.isCollapse = item['menuName'] === '首页';

  }

  // 退出登录
  public loginOut(): void {
    this.login.loginOut( (data: any) => {
      if (data['return_code'] === '000000') {
        this.storage.removeItem(LOGIN_DATA);
        this.storage.removeItem(DATA);
        this.storage.removeItem(USER_TOKEN);
        this.storage.removeItem(INIT_FLAG);
        location.reload();

        this.router.navigateByUrl('/login').then();
      } else {
        this.message.error(data['return_msg']);
      }
    });
  }



}
