<!-- 分栏布局 -->
<template>
  <el-container class="layout">
    <div class="aside-split">
      <div class="logo flx-center">
        <img class="logo-img" src="@/assets/images/logo.svg" alt="logo" />
      </div>
      <el-scrollbar>
        <div class="split-list">
          <el-tooltip
            effect="dark"
            :content="item.meta.title"
            placement="right"
            :show-after="200"
            v-for="item in menuList"
            :key="item.path"
          >
            <div
              class="split-item"
              :class="{ 'split-active': splitActive === item.path || `/${splitActive.split('/')[1]}` === item.path }"
              @click="changeSubMenu(item)"
            >
              <i
                class="iconfont flx-shrink"
                :class="currentRouterName == item.name ? item.meta.icon + '-fill' : item.meta.icon"
              ></i>
              <!-- <span class="title">{{ item.meta.title }}</span> -->
            </div>
          </el-tooltip>
        </div>
      </el-scrollbar>
    </div>
    <el-aside :class="{ 'not-aside': !subMenuList.length }" :style="{ width: isCollapse ? '65px' : '210px' }">
      <div class="logo flx-center">
        <span class="logo-text" v-show="subMenuList.length">
          <template v-if="isCollapse">L</template>
          <template v-else>
            <span class="company">{{ AppCompany }}</span>
            <span>
              {{ AppName }}
            </span>
          </template>
        </span>
      </div>
      <el-scrollbar>
        <el-menu
          :default-active="activeMenu"
          :router="false"
          :collapse="isCollapse"
          :collapse-transition="false"
          :unique-opened="true"
        >
          <SubMenu :menuList="subMenuList" :is-collapse="false" />
        </el-menu>
      </el-scrollbar>
    </el-aside>
    <el-container>
      <el-header>
        <ToolBarLeft />
        <ToolBarRight @goto-refresh="refresh" />
      </el-header>
      <Main ref="MainRef" />
    </el-container>
  </el-container>
</template>

<script setup lang="ts" name="layoutColumns">
import { ref, computed, watch, nextTick } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useAuthStore } from "@/stores/modules/auth";
import { useGlobalStore } from "@/stores/modules/global";
import Main from "@/layouts/components/Main/index.vue";
import ToolBarLeft from "@/layouts/components/Header/ToolBarLeft.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";
const keepAliveStore = useKeepAliveStore();

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

const subMenuList = ref<Menu.MenuOptions[]>([]);
const splitActive = ref("");
watch(
  () => [menuList, route],
  () => {
    // 当前菜单没有数据直接 return
    if (!menuList.value.length) return;
    splitActive.value = route.path;
    const menuItem = menuList.value.filter((item: Menu.MenuOptions) => {
      return route.path === item.path || `/${route.path.split("/")[1]}` === item.path;
    });
    if (menuItem[0].children?.length) return (subMenuList.value = menuItem[0].children);
    subMenuList.value = [];
  },
  {
    deep: true,
    immediate: true
  }
);

// change SubMenu
const changeSubMenu = (item: Menu.MenuOptions) => {
  splitActive.value = item.path;
  if (item.children?.length) return (subMenuList.value = item.children);
  subMenuList.value = [];
  router.push(item.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>
