<template>
  <a-layout class="main-content">
    <a-layout-sider :collapsed="collapsed" theme="light" :trigger="null" collapsible>
      <div v-if="!collapsed" class="title flex-center">
        <div>超级后台</div>
      </div>
      <div v-else class="logo flex-center">
        <img src="@/assets/vue.svg" title="超级后台" alt="xx" />
      </div>
      <!--  -->
      <!-- <a-menu v-model:selectedKeys="selectedKeys" theme="light" mode="inline">
        <a-menu-item key="1">
          <router-link to="/index">
            <pie-chart-outlined />
            <span>首页</span>
          </router-link>
        </a-menu-item>
        <a-menu-item key="2">
          <router-link to="/register/index">
            <pie-chart-outlined />
            <span>注册</span>
          </router-link>
        </a-menu-item>
      </a-menu> -->
      <!-- 动态生成菜单项 -->
      <a-menu v-model:selectedKeys="selectedKeys" theme="light" mode="inline">
        <template v-for="route in staticRouter">
          <a-menu-item v-if="route.meta?.title === undefined && route.children">
            <router-link :to="`${route.children[0].path}`">
              <HomeOutlined v-if="route.name === 'dashboard'" />
              <InsuranceOutlined v-if="route.name === 'police'" />
              <SoundOutlined v-if="route.name === 'law'" />
              <ApartmentOutlined v-if="route.name === 'warning'" />
              <span>{{ route?.children[0]?.meta?.title }}</span>
            </router-link>
          </a-menu-item>
          <a-sub-menu v-if="route.children && route.children.length && route?.meta?.title" :key="route.path">
            <template #title>
              <MailOutlined v-if="route.name === 'query-'" />
              <UserOutlined v-if="route.name === 'user'" />
              <span>{{ route.meta?.title }}</span>
            </template>
            <a-menu-item v-for="child in route.children" :key="child.path">
              <router-link :to="`${route.path}/${child.path}`">{{ child.meta?.title }}</router-link>
            </a-menu-item>
          </a-sub-menu>
        </template>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header class="layout-header">
        <layout-header v-model:collapsed="collapsed" />
      </a-layout-header>
      <a-layout-content class="layout-content">
        <!-- <keep-alive> 会缓存已经访问过的组件，当你再次访问同一个页面时，它会直接从缓存中加载，而不会触发重新渲染 -->
        <!-- router-view 必须绑定一个key 否则就会出现 路由切换白屏的问题（路由切换后，页面不显示内容，刷新后正常显示），但是这样也会导致一个问题就是 会导致keep-alive失效、因为这种方式会强制刷新路由-->

        <router-view v-slot="{ Component, route }" :key="route.path">
          <!-- <router-view v-slot="{ Component, route }"> -->
          <transition appear name="fade-transform" mode="out-in">
            <keep-alive :include="keepAliveNames">
              <component :is="Component" :key="route.path" />
            </keep-alive>
          </transition>
        </router-view>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script setup lang="ts">
import { InsuranceOutlined, HomeOutlined, SoundOutlined, MailOutlined, ApartmentOutlined, UserOutlined, AppstoreOutlined } from '@ant-design/icons-vue'
import LayoutHeader from '@/components/layout/header/LayoutHeader.vue'
import { computed } from 'vue'
import { staticRouter } from '@/router/staticRouters'
import { useRoute } from 'vue-router'
const route = useRoute()

// 当前选中的菜单项
// const selectedKeys = computed(() => route.path)

const selectedKeys = computed(() => [route.path])
// 过滤出需要在菜单中显示的路由
// const menuRoutes = computed(() => staticRouter.filter((route) => route.meta && route.meta.title))
// staticRouter.forEach((element) => {
//   console.log(element.meta?.title)
// })

// 示例：动态控制缓存页面的名称
// const keepAliveNames = ref(['index'])

import { ref } from 'vue'
// 控制菜单折叠
const collapsed = ref<boolean>(false)

// const keepAliveNames = ref<string[]>(['Index', 'Register'])
const keepAliveNames = ref<string[]>(['index', 'rindex'])
</script>

<style scoped lang="scss">
.main-content {
  width: 100vw;
  height: 100vh;

  .layout-header {
    background: #ffffff;
    padding: 0;
  }

  .layout-content {
    margin: 10px;
    // padding: 4px;
    // background: #f5f7fd;
    min-height: 280px;
    border-radius: 5px;
    overflow: auto;
  }
}

.title {
  height: 32px;
  width: 168px;
  transition: width 2ms linear 2ms;
  margin: 16px;
  color: black;
  font-weight: bold;
  font-size: 20px;
}

.logo {
  margin: 16px;

  img {
    width: 50px;
    height: 50px;
  }
}

.site-layout .site-layout-background {
  background: #ffffff;
}
</style>
