<script setup lang="ts">
import { ref } from 'vue';
import { onBeforeRouteUpdate, useRouter } from 'vue-router';
import { getStoreData } from '@/utils/composition';
import { checkPrivil } from '@/apps/app-bmc/router/routes';
import { UserPrivil } from '@/model/base-enum';

const $router = useRouter();
// 常规配置权限
const isBaseConfig = checkPrivil(UserPrivil.baseConfig);
let checkTabIndex = ref('');
const tabList = ref([
  {
    id: 'spManage',
    index: '0',
    label: 'IBMC_SP_BASE_INFO',
    name: 'bios',
    router: '/navigate/manager/sp/sp-manage',
  },
]);
function getInitTableList() {
  if (isBaseConfig) {
    tabList.value.push({
      id: 'spTask',
      index: '1',
      label: 'SP_TITLE',
      name: 'cpu',
      router: '/navigate/manager/sp/sp-task',
    });
  }
}

getInitTableList();
let currentRoute = $router.currentRoute.value.fullPath;

const currentTabItem = tabList.value.find(item => {
  return item.router === currentRoute;
});
if (currentTabItem) {
  checkTabIndex.value = currentTabItem.index;
}

onBeforeRouteUpdate((to: any) => {
  tabList.value.forEach((item: any) => {
    if (item.router === to.path) {
      checkTabIndex.value = item.index;
      return;
    }
  });
});

const handleSelect = (key: any) => {
  let res = tabList.value.find(item => {
    return item.index === key;
  });
  if (res) {
    $router.push(res.router);
  }
};
</script>

<template>
  <!-- tab切换区域 -->
  <el-menu
    :default-active="checkTabIndex"
    class="el-menu-report"
    mode="horizontal"
    @select="handleSelect"
  >
    <el-menu-item v-for="item in tabList" :id="item.id" :key="item.label" :index="item.index">
      {{ $t(item.label) }}
    </el-menu-item>
  </el-menu>
  <router-view />
</template>

<style lang="scss" scoped>
.el-menu-report {
  background-color: transparent;
  margin-bottom: 16px;
  height: 32px !important;
  .el-menu-item {
    color: var(--o-text-color-secondary);
    font-size: 14px;
    padding: 0;
    margin-right: 32px;
    background-color: rgba(0, 0, 0, 0) !important;
    font-weight: 600;
    &.is-active {
      color: var(--o-color-primary) !important;
      border-bottom: 2px solid var(--o-color-primary) !important;
    }

    &:hover {
      color: var(--o-color-primary-secondary) !important;
      background-color: rgba(0, 0, 0, 0);
    }
  }
}
</style>
