<!-- 横向布局 -->
<template>
  <el-container class="layout">
    <el-header>
      <div class="logo flx-center">
        <img class="logo-img" src="@/assets/images/logo.png" 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>
              <el-icon>
                <component :is="subItem.meta.icon" />
              </el-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)">
            <el-icon>
              <component :is="subItem.meta.icon" />
            </el-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" name="layoutTransverse">
import { computed } from "vue";
import { ElMessage } from "element-plus";
import { useAuthStore } from "@/stores/modules/auth";
import { useRoute, useRouter } from "vue-router";
import { checkMenuClickPermission } from "@/utils/permission";
import Main from "@/layouts/components/Main/index.vue";
import ToolBarRight from "@/layouts/components/Header/ToolBarRight.vue";
import SubMenu from "@/layouts/components/Menu/SubMenu.vue";

const title = import.meta.env.VITE_GLOB_APP_TITLE;

const route = useRoute();
const router = useRouter();
const authStore = useAuthStore();
const menuList = computed(() => authStore.showMenuListGet);
const activeMenu = computed(() => {
  if (!route || !route.path) return "";
  return (route.meta?.activeMenu ? route.meta.activeMenu : route.path) as string;
});

const handleClickMenu = async (subItem: Menu.MenuOptions) => {
  if (subItem.meta.isLink) return window.open(subItem.meta.isLink, "_blank");

  if (!subItem.path) {
    ElMessage.warning("菜单路径不存在");
    return;
  }

  // 调用权限检查方法，判断是否可以点击（方法内部会显示相应的提示信息）
  const { canClick } = await checkMenuClickPermission(subItem.path);

  if (!canClick) {
    return;
  }

  // 允许访问，执行路由跳转
  try {
    await router.push(subItem.path);
  } catch (error: any) {
    handleRouteError(error);
  }
};

// 处理路由错误
const handleRouteError = (error: any) => {
  // 如果是导航重复的错误，忽略它
  if (error?.name === "NavigationDuplicated") {
    return;
  }
  // 如果是路由不存在的错误
  if (error?.message?.includes("No match") || error?.message?.includes("not found")) {
    ElMessage.warning("该页面不存在或尚未配置");
  } else {
    console.error("路由跳转失败:", error);
    ElMessage.error("页面跳转失败，请稍后重试");
  }
};
</script>

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