<template>
  <pro-layout v-model:openKeys="vdata.openKeys" v-model:selectedKeys="vdata.selectedKeys"
    v-model:collapsed="proLayoutObject.collapsed" layout="side" isMobile="true" :active-name="$route.path"
    :nav-theme="vdata.theme" :sider-width="260" :menu-data="menuData" :collapsed-button-render="false" 
    :collapsed-width="80" class="addd">
    <router-view v-if="isRouterAlive" />
    <!-- 菜单头部渲染插槽 -->
    <template #menuDataRender>
      <router-link :to="{ path: '/' }" style="display: flex; align-items: center">
        <div v-if="!proLayoutObject.collapsed">
          <img v-if="vdata.theme === 'light'" :src="oemSiteInfo.sysLogoUrl" style="height: 30px" />
          <img v-else :src="oemSiteInfo.darkSysLogoUrl
            ? oemSiteInfo.darkSysLogoUrl
            : oemSiteInfo.sysLogoUrl
            " style="height: 30px" />
        </div>
        <div v-else>
          <img v-if="vdata.theme === 'light'" :src="oemSiteInfo.sysMinLogoUrl" style="height: 30px" />
          <img v-else :src="oemSiteInfo.darkSysMinLogoUrl
            ? oemSiteInfo.darkSysMinLogoUrl
            : oemSiteInfo.sysMinLogoUrl
            " style="height: 30px" />
        </div>
      </router-link>
    </template>

    <!-- 菜单底部换色按钮 -->
    <template #menuFooterRender>
      <div class="menu-footer" :class="{ 'menu-footer-dark': vdata.theme === 'dark' }" :style="{
        margin: proLayoutObject.collapsed ? '15px 15px 0' : '20px 25px 0'
      }" @click="vdata.theme = vdata.theme === 'dark' ? 'light' : 'dark'">
        <i class="bi bi-circle-half" />
        <span v-if="!proLayoutObject.collapsed">
          切换至{{ vdata.theme === 'dark' ? '亮色' : '暗色' }}</span>
      </div>
    </template>

    <!-- 自定义顶部内容， 包括刷新按钮，面包屑 -->
    <template #headerContentRender>
      <div class="customhead">
        <div class="refash">
          <MenuUnfoldOutlined v-if="proLayoutObject.collapsed" style="font-size: 16px; color: #36454d; margin-right: 10px"
            @click="proLayoutObject.collapsed = false" />
          <MenuFoldOutlined v-else style="font-size: 16px; color: #36454d; margin-right: 10px"
            @click="proLayoutObject.collapsed = true" />
          <ReloadOutlined style="font-size: 16px; color: #36454d" :spin="!isRouterAlive" @click="routeReload" />
        </div>

        <!-- 面包屑部分。数组第一项固定为首页，可以点击跳转，其他面包屑，不允许进行跳转 -->
        <a-breadcrumb style="margin-left: 10px">
          <a-breadcrumb-item>
            <router-link :to="breadcrumb[0].path">
              {{ breadcrumb[0].breadcrumbName }}
            </router-link>
          </a-breadcrumb-item>
          <a-breadcrumb-item v-for="(item, index) in breadcrumb" v-show="item.path !== '/'" :key="index">
            {{ item.breadcrumbName }}
          </a-breadcrumb-item>
        </a-breadcrumb>
      </div>
    </template>
    <!-- 自定义右侧区域 -->
    <template #rightContentRender>
      <div>
        <a-dropdown>
          <template #overlay>
            <a-menu>
              <a-menu-item @click="userInfo"><setting-outlined style="margin-right: 10px" /><span
                  style="cursor: hand">个人信息</span></a-menu-item>
              <a-menu-item @click="logout">
                <LogoutOutlined style="margin-right: 10px" /><span style="cursor: hand">退出登录</span>
              </a-menu-item>
            </a-menu>
          </template>
          <a-avatar shape="square" style="margin-right: 5px; width: 30px; height: 30px"
            :src="userStore.userInfo.avatarUrl" />
        </a-dropdown>
        <span>{{ userStore.userInfo['realname'] }}</span>
      </div>
    </template>
  </pro-layout>
</template>

<script setup lang="ts">
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/store/modules/user'
import {
  onMounted,
  ref,
  reactive,
  computed,
  watchEffect,
  nextTick,
  getCurrentInstance
} from 'vue'
import { useOem } from '@/store/modules/oem' // OEM信息
import { getMenuData, clearMenuItem } from '@ant-design-vue/pro-layout'

// 导入全局函数
const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties
// 获取登出确认框

// 获取网站信息
const oemSiteInfo = useOem().getSiteInfo()
// 响应式数据
const vdata = reactive({
  openKeys: [] as any,
  selectedKeys: [] as any,
  theme: 'light' //light or dark
})

// proLayout对象
const proLayoutObject = reactive({
  collapsed: false
})

// 获取到路由对象
const router = useRouter() //这是全部路由
const route = useRoute() //这是当前页面的路由
// vdata.selectedKeys.push(route.path) //为selectedKeys赋值当前页面路由
// vdata.openKeys.push(route.matched[1].path) //为openKeys赋值当前路由所属菜单的展开项

//如果存在多级路由嵌套
if (route.matched.length > 3) {
  for (let i = 2; i < route.matched.length - 1; i++) {
    vdata.openKeys.push(route.matched[i].path)
  }
}

// store对象
const userStore = useUserStore()

// 使用pro-layout 封装menu对象信息
const { menuData } = reactive(getMenuData(clearMenuItem(router.getRoutes())))
console.log(menuData,'menuData')
// menuData.forEach((item)=>{
//   item.meta.title = `<span v-pre="4444">666</span>`
// })
// 判断路由是否可用状态（用作刷新使用）
let isRouterAlive = ref(true)

// 面包屑导航参数

const breadcrumb = computed(() =>
  router.currentRoute.value.matched.concat().map((item) => {
    return {
      path: item.path,
      breadcrumbName: item.meta.title || ''
    }
  })
)
watchEffect(() => {
  if (router.currentRoute) {
    const matched = router.currentRoute.value.matched.concat()

    vdata.selectedKeys = matched
      .filter((r) => r.name !== 'index')
      .map((r) => r.path)
    vdata.openKeys = matched
      .filter((r) => r.path !== router.currentRoute.value.path)
      .map((r) => r.path)
  }
})
// 刷新页面函数
function routeReload() {
  isRouterAlive.value = false
  nextTick(() => {
    isRouterAlive.value = true
  })
}

// 登出函数
function logout() {
  $infoBox.confirmDanger(
    '是否退出登录？',
    `你好${userStore.userInfo['realname']}确认退出登录吗？`,
    () => {
      userStore.logout()
    }
  )
}
//个人信息页
function userInfo() {
  router.push({
    path: '/currentUserinfo'
  })
}

setTimeout(() => {
let titleList:any =window.document.querySelectorAll('.ant-pro-menu-item-title')
console.log(titleList);
titleList.forEach((item)=>{
  if(item.innerText==='产品列表'){
    item.innerHTML+=`<span style="background: #ff4949;
    border-radius: 50%;color: #fff;margin-left: 10px; display:inline-block;width:20px;text-align:center; height: 20px;
    line-height: 20px;">10</span>`
  }
})
}, 1000);

</script>

<style scoped lang="less">
#collapsed {
  background: #000;
}

.test {
  width: 79px;
  height: 40px;
  background: #dddddd;
  border-radius: 12px;
  margin-top: 5px;
}

.refash {
  display: flex;
  justify-content: center;
  align-items: center;
}

.customhead {
  display: flex;
  height: 100%;
  align-items: center;
}
</style>

<style lang="less">
.ant-menu {
  background: transparent !important;
}

.ant-layout-sider-children {
  //导航整体的右侧新增一条border
  border-right: 1px solid #08174212;
  background: transparent;
  padding-bottom: 30px;
}

.ant-layout-sider-light {
  //亮色模式下整体的背景色
  background: @background-color;
}

.ant-pro-sider-light {
  //取消导航栏投影
  box-shadow: none !important;
}

.ant-pro-sider-logo {
  // 顶部logo的padding
  padding: 25px !important;
}

.ant-pro-sider-menu {
  //导航内容区域两边新增padding，并去掉了右侧的border
  padding: 0 15px !important;
  border: none !important;
}

.ant-menu-inline-collapsed {
  //收起状态下，导航内容区域两侧的padding变小
  padding: 0 10px !important;
}

.ant-menu-submenu-title {
  //使导航在收起状态下，带有子item的一级菜单的图标居中
  margin: 0 auto;
}

.ant-menu-item {
  margin: 0 auto !important; //使导航在收起状态下，没有子item的一级菜单图标居中
  border-radius: 3px;

  &:hover {
    background: @primary-glass; //导航item的hover样式
  }
}

.ant-menu-sub.ant-menu-inline>.ant-menu-item {
  //二级导航item的上下边距
  margin: 5px 0 !important;
}

.ant-menu-vertical.ant-menu-sub {
  //收起状态下，子菜单的容器padding
  padding: 5px;
}

.ant-menu-dark .ant-menu-inline.ant-menu-sub {
  //暗色模式下，子菜单背景颜色调整
  background: #001529;
}

.ant-menu-sub.ant-menu-inline {
  //亮色模式下，子菜单背景颜色调整
  background-color: @background-color;
}

.ant-pro-sider-light .ant-layout-sider-children ::-webkit-scrollbar-track {
  //亮色状态下，去掉滚动条轨道颜色
  background: transparent;
}

.ant-pro-sider-dark .ant-layout-sider-children ::-webkit-scrollbar-track {
  //暗色状态下，去掉滚动条轨道颜色
  background: transparent;
}

.ant-pro-sider .ant-layout-sider-children ::-webkit-scrollbar-track {
  //去掉滚动条轨道的内阴影
  box-shadow: none;
}

.ant-menu:not(.ant-menu-horizontal) .ant-menu-item-selected {
  //选中状态下的item的样式
  background-color: var(--ant-primary-color) !important;
  color: #fff !important;
  border-radius: 3px;

  & a:hover {
    color: #fff !important;
  }
}

.ant-menu-item-selected a {
  //选中状态下的文字颜色继承父元素
  color: inherit !important;
}

.ant-menu-item .ant-menu-item-icon+span {
  //去掉文字变色的延迟动画效果
  transition: 0s;
}

.ant-menu-vertical .ant-menu-item::after,
.ant-menu-vertical-left .ant-menu-item::after,
.ant-menu-vertical-right .ant-menu-item::after,
.ant-menu-inline .ant-menu-item::after {
  //取消选中状态下item右侧的蓝色方条
  content: none;
}

//header样式
.ant-layout-header {
  background-color: transparent;
  height: auto !important;
  width: 100%;
  padding: 25px 0 !important;
  line-height: inherit !important;
}

.ant-pro-global-header {
  padding: 0 30px !important;
  background-color: transparent;
  box-shadow: none;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

aside {
  max-width: 300px;
}

aside::before {
  // content: '';
  position: absolute;
  background-color: @jee-theme;
  width: 10px;
  height: 100vh;
  left: 0;
  top: 0;
  z-index: 99;
}

.ant-pro-basicLayout-content {
  position: relative;
  margin: 0 15px;
}

// 菜单底部按钮
.menu-footer {
  cursor: pointer;
  height: 36px;
  margin: 0 25px;
  border-radius: 3px;
  background: rgba(235, 238, 242, 0);
  border: 1px dashed #d2d3d9;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #7a8a99;
  transition: 0.3s ease;

  &:hover {
    border: 1px dashed var(--ant-primary-color);
    color: var(--ant-primary-color);
    background: @primary-glass;
  }

  .bi-circle-half {
    transform: rotate(45deg);
    transition: 0.2s ease;
  }

  span {
    font-weight: normal;
    font-size: 14px;
    letter-spacing: 0.05em;
    text-align: left;
    margin-left: 7px;
    transition: 0.2s ease;
  }
}

.menu-footer-dark {
  height: 36px;
  border-radius: 3px;
  background: rgba(235, 238, 242, 0);
  border: 1px dashed #4d5c6a;
  color: #fff;

  .bi-circle-half {
    transform: rotate(225deg);
  }
}
</style>
<style>
.ant-pro-sider-menu {
  background-color: transparent !important;
  overflow: hidden !important;
}

.ant-pro-menu-item:hover {
  color: var(--ant-primary-color) !important;
}

.ant-layout-header {
  background-color: transparent !important;
  padding: 0 !important;
}

.ant-pro-global-header-layout-side {
  background-color: transparent !important;
  box-shadow: none !important;
  padding: 25px 25px !important;
}

.ant-pro-sider .ant-layout-sider-children {
  height: 100vh !important;
}

.ant-layout-content {
  margin: 0 15px 24px 15px !important;
}

.ant-layout:first-child .ant-layout {
  height: 100vh !important;
  overflow-y: scroll !important;
}

</style>
