import { CommonModule } from '@angular/common';
import { Component, OnInit, ViewChild, inject } from '@angular/core';
import { Router, RouterOutlet } from '@angular/router';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { SHARED_IMPORTS } from '@shared';
import { NzDropDownModule } from 'ng-zorro-antd/dropdown';

import { Menu } from '../menu/lj-menu.interface';
import { MenuService } from '../menu/lj-menu.service';

@Component({
  selector: 'data-v-navigation',
  standalone: true,
  imports: [RouterOutlet, CommonModule, ...SHARED_IMPORTS],
  templateUrl: './navigation.component.html',
  styleUrls: ['./navigation.component.less']
})
export class DataVNavigationComponent implements OnInit {
  private readonly http = inject(_HttpClient);
  private readonly modal = inject(ModalHelper);
  private readonly router = inject(Router);
  menuNetDataList: Menu[] = [];
  // 添加一个属性来标记是否有菜单被选中
  selectedParentMenu: Menu | undefined;
  imageUrl: string = 'assets/dashboard/menu_btn_l.png';
  title: string = 'Label';

  context: string = '2024-02-29 10:00';
  constructor(private menuService: MenuService) {}

  ngOnInit(): void {
    this.menuNetDataList = [
      {
        menuCode: 'Home',
        text: '我的工作站',

        i18n: null,
        icon: null,
        grp: true,
        leafFlag: false,
        rootFlag: true,
        hideInBreadcrumbFlag: true,
        link: '/',
        srtno: 0,
        valiFlag: false,
        menuType: 0,
        parentId: null,
        clientId: 'Platform_Datav',
        acl: null,
        id: '3a1167f1-a86c-194d-eabf-258993a337ac'
      }
    ];
    this.menuService.getMenus().subscribe((menus: Menu[]) => {
      console.log('this.menuNetDataList');

      this.menuNetDataList = this.menuNetDataList.concat(menus);

      console.log(this.menuNetDataList);
      // this.menuList;
    });

    // this.updateselectedParentMenuStatus();
  }
  // 更新是否有菜单被选中的状态
  //   this.hasSelectedMenu = this.menuNetDataList.some(m => m.selected);
  //   console.log(this.hasSelectedMenu);
  // }
  add(): void {}

  menuOver(menu: any, $event: any) {}

  isActive(menu: any) {}

  navigate(menu: any) {
    // 清除所有菜单的选中状态
    this.menuNetDataList.forEach(m => (m.selected = false));

    // 如果是一级菜单并且有子菜单，则导航到新页面并传递数据
    if (menu.link && menu.rootFlag && menu.children && menu.children.length > 0) {
      console.log('来时跳转2级');
      console.log(menu.link);
      console.log(menu.children);

      // 设置当前菜单为选中状态
      menu.selected = true;
      this.selectedParentMenu = menu;

      this.router.navigate([menu.link, menu.id], {
        state: { menu: menu.children }
      });
    } else if (menu.menuCode == 'Home') {
      menu.selected = true;
      this.selectedParentMenu = menu;
      this.router.navigateByUrl(menu.link);
    } else {
      menu.selected = true;
      this.router.navigate([menu.link, menu.id], {
        state: { menu: menu.children }
      });
    }
  }
}
