<!-- 横向布局 -->
<template>
  <el-container class="layout">
    <el-header>
      <div class="logo flx-center">
        <img class="logo-img" src="@/assets/images/logo.svg" alt="logo" />
        <span class="logo-text">{{ title }}</span>
      </div>
      <el-menu mode="horizontal" :router="false" :default-active="activeMenu">
        <!-- 不能直接使用 SubMenu 组件，无法触发 el-menu 隐藏省略功能 -->
        <template v-for="subItem in menuList" :key="subItem.path">
          <el-sub-menu
            v-if="subItem.children?.length"
            :key="subItem.path"
            :index="subItem.path + 'el-sub-menu'"
          >
            <template #title>
              <svg-icon :icon-class="subItem.meta.icon" />
              <span>{{ subItem.meta.title }}</span>
            </template>
            <SubMenu :menu-list="subItem.children" />
          </el-sub-menu>
          <el-menu-item
            v-else
            :key="subItem.path + 'el-menu-item'"
            :index="subItem.path"
            @click="handleClickMenu(subItem)"
          >
            <svg-icon :icon-class="subItem.meta.icon" />
            <template #title>
              <span>{{ subItem.meta.title }}</span>
            </template>
          </el-menu-item>
        </template>
      </el-menu>
      <ToolBarRight />
    </el-header>
    <Main />
  </el-container>
</template>

<script setup lang="ts">
import { useUserStore } from '@/stores/modules/user'
import Main from '@/layout/components/Main/index.vue'
import ToolBarRight from '@/layout/components/Header/ToolBarRight.vue'
import SubMenu from '@/layout/components/Menu/SubMenu.vue'
import { MenuOptions } from '@/stores/typings/userType'

const title = import.meta.env.VITE_APP_TITLE

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const menuList = computed(() => userStore.getShowMenus)
const activeMenu = computed(
  () => (route.meta.activeMenu ? route.meta.activeMenu : route.path) as string
)

const handleClickMenu = (subItem: MenuOptions) => {
  if (subItem.meta.isLink) {
    window.open(subItem.path, '_blank')
  } else {
    router.push(subItem.path)
  }
}
</script>

<style scoped lang="scss">
@import './index.scss';
</style>
