<!-- 横向布局 -->
<template>
  <el-container class="layout">
    <el-header>
      <div class="logo flx-center">
        <div class="logo-img-box flx-center">
          <img class="logo-img" src="@/assets/images/logo.svg" alt="logo" />
        </div>
        <span class="logo-text">
          <span class="company">{{ AppCompany }}</span>
          <span>
            {{ AppName }}
          </span>
        </span>
      </div>
      <el-menu mode="horizontal" :default-active="activeMenu" :router="false" :unique-opened="true">
        <!-- 不能直接使用 SubMenu 组件，无法触发 el-menu 隐藏省略功能 -->
        <template v-for="subItem in menuList" :key="subItem.path">
          <el-sub-menu v-if="subItem.children?.length" :index="subItem.path + 'el-sub-menu'" :key="subItem.path">
            <template #title>
              <i
                class="iconfont flx-shrink"
                :class="currentRouterName == subItem.name ? subItem.meta.icon + '-fill' : subItem.meta.icon"
              ></i>
              <span class="sle">{{ t(`menu.${subItem.name}`) }}</span>
            </template>
            <SubMenu :menuList="subItem.children" :is-collapse="false" />
          </el-sub-menu>
          <el-menu-item v-else :index="subItem.path" :key="subItem.path + 'el-menu-item'" @click="handleClickMenu(subItem)">
            <i
              class="iconfont flx-shrink"
              :class="currentRouterName == subItem.name ? subItem.meta.icon + '-fill' : subItem.meta.icon"
            ></i>
            <template #title>
              <span class="sle">{{ t(`menu.${subItem.name}`) }}</span>
            </template>
          </el-menu-item>
        </template>
      </el-menu>
      <ToolBarRight @goto-refresh="refresh" />
    </el-header>
    <Main ref="MainRef" />
  </el-container>
</template>

<script setup lang="ts" name="layoutTransverse">
import { ref, computed, nextTick } from "vue";
import { useAuthStore } from "@/stores/modules/auth";
import { useRoute, useRouter } from "vue-router";
import Main from "@/layouts/components/Main/index.vue";
import ToolBarRight from "@/layouts/components/Header/ToolBarRight.vue";
import SubMenu from "@/layouts/components/Menu/SubMenu.vue";
import { useKeepAliveStore } from "@/stores/modules/keepAlive";
import type { Menu } from "@/typings/global";
import { useI18n } from "vue-i18n";

const { t } = useI18n();

const keepAliveStore = useKeepAliveStore();

const route = useRoute();
const router = useRouter();
const currentRouterName = computed(() => router.currentRoute.value.name);
const authStore = useAuthStore();
const menuList = computed(() => authStore.showMenuListGet);
const activeMenu = computed(() => (route.meta.activeMenu ? route.meta.activeMenu : route.path) as string);

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

const AppName = import.meta.env.VITE_GLOB_APP_TITLE;
const AppCompany = import.meta.env.VITE_GLOB_APP_COMPANY;

/**
 * @description 局部刷新
 */
const MainRef = ref<any>(null);
const refresh = () => {
  setTimeout(() => {
    keepAliveStore.removeKeepAliveName(route.name as string);
    MainRef.value.refreshCurrentPage(false);
    nextTick(() => {
      keepAliveStore.addKeepAliveName(route.name as string);
      MainRef.value.refreshCurrentPage(true);
    });
  }, 0);
};
</script>

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