<script setup lang="ts">
import {
  FilePptOutlined,
  FileSearchOutlined,
  NodeIndexOutlined,
  LogoutOutlined,
  HistoryOutlined,
  StarOutlined,
} from '@ant-design/icons-vue';
import { onMounted, ref, watch } from 'vue';
import { useRoute, RouterLink } from "vue-router";
import _ from "lodash";
import { useAuthStore } from '@/stores/auth.store';
import { routeToLogin } from "@/router";

const selectedKeys = ref<string[]>(['/']);
const openKeys = ref<string[]>(['/']);
// 当前路由
const route = useRoute();
// 授权store
const authStore = useAuthStore();

onMounted(() => {
  openKeys.value = getOpenKeys(route.path);
  selectedKeys.value = [route.path];

  watch(
    () => route.path,
    (path: string) => {
      const currentOpenkeys = getOpenKeys(path);
      openKeys.value = _.uniq(_.concat(openKeys.value, currentOpenkeys));
    }
  );
});

function getOpenKeys(path: string): string[] {
  const parts = _.dropRight(path.split('/').filter(x => x != '').map(x => '/'+x), 1);
  if (parts.length == 0) {
    return ["/"];
  }
  let openKeys = parts.reduce((acc, item) => {
    if (acc.length == 0) {
      return acc.concat([item]);
    }
    return acc.concat([acc[acc.length-1] + item]);
  }, [] as string[]);
  openKeys.unshift("/");
  return openKeys;
}

function logout(e: MouseEvent) {
  authStore.logout();
  routeToLogin();
  e.stopPropagation();
}
</script>

<template>
  <a-menu
    v-model:openKeys="openKeys"
    v-model:selectedKeys="selectedKeys"
    mode="inline"
  >
    <a-sub-menu key="/report">
      <template #icon>
        <FilePptOutlined />
      </template>
      <template #title>报告管理</template>
      <a-menu-item key="/report/index">
        <RouterLink to="/report/index">我的报告</RouterLink>
      </a-menu-item>
      <a-menu-item key="/report/template">
        <RouterLink to="/report/template">模板管理</RouterLink>
      </a-menu-item>
      <a-menu-item key="/report/create">
        <RouterLink to="/report/create">创建报告</RouterLink>
      </a-menu-item>
    </a-sub-menu>
    <a-sub-menu key="/search">
      <template #icon>
        <FileSearchOutlined />
      </template>
      <template #title>搜索</template>
      <a-menu-item key="/search/index">
        <RouterLink to="/search/index">智能搜索</RouterLink>
      </a-menu-item>
      <a-menu-item key="/search/advanced/index">
        <RouterLink to="/search/advanced/index">高级搜索</RouterLink>
      </a-menu-item>
    </a-sub-menu>
    <a-sub-menu key="/knowledge-graph">
      <template #icon>
        <NodeIndexOutlined />
      </template>
      <template #title>知识导航</template>
      <a-menu-item key="/knowledge-graph/search">
        <RouterLink to="/knowledge-graph/search">建筑工程科技导航</RouterLink>
      </a-menu-item>
    </a-sub-menu>
    <a-menu-item key="/history">
      <template #icon>
        <history-outlined />
      </template>
      <RouterLink to="/history">历史记录</RouterLink>
    </a-menu-item>
    <a-menu-item key="/favorite">
      <template #icon>
        <star-outlined />
      </template>
      <RouterLink to="/favorite">我的收藏</RouterLink>
    </a-menu-item>
    <a-menu-item key="/logout" @click="logout">
      <template #icon>
        <logout-outlined />
      </template>
      退出系统
    </a-menu-item>
  </a-menu>
</template>
