<template>
  <div class="home">
    <div :class="['left-slide', !isCollapse ? '' : 'collapse-slide']">
      <div class="logo">
        <div class="none-collapse pic" v-if="!isCollapse">
          <img src="@/assets/images/leftslide/logo.png" alt="" />
        </div>
        <div class="collapse pic" v-else>
          <img src="@/assets/images/leftslide/logo2.png" alt="" />
        </div>
      </div>
      <div class="toggle pic cp" @click="isCollapse = !isCollapse">
        <img v-if="!isCollapse" src="@/assets/images/leftslide/close.png" alt="" />
        <img v-else src="@/assets/images/leftslide/open.png" alt="" />
      </div>
      <el-menu
        :collapse="isCollapse"
        :default-active="currentPath"
        class="el-menu-vertical-demo"
        background-color="transparent"
      >
        <template v-for="item in routes" :key="item.name">
          <el-sub-menu :index="item.path" v-if="item.children!.length > 0">
            <template #title>
              <div :class="['flex-start', 'sub-item', 'collapse']">
                <div class="icno pic flex-start">
                  <img
                    :src="
                      currentPath === item.path ||
                      item.children?.findIndex((citem) => {
                        return citem.path === currentPath
                      }) !== -1
                        ? item.lightIcno
                        : item.icno
                    "
                    alt=""
                  />
                </div>
                <span
                  v-show="!isCollapse"
                  :class="
                    currentPath === item.path ||
                    item.children?.findIndex((citem) => {
                      return citem.path === currentPath
                    }) !== -1
                      ? 'active'
                      : ''
                  "
                  >{{ item.name }}</span
                >
              </div>
            </template>
            <el-menu-item
              :index="citem.path"
              v-for="citem in item.children"
              :key="citem.name"
              @click="changeRouter(citem.path)"
              ><span>{{ citem.name }}</span></el-menu-item
            >
          </el-sub-menu>

          <template v-else>
            <el-menu-item :index="item.path" :key="item.name" @click="changeRouter(item.path)">
              <div :class="['flex-start', 'sub-item', 'collapse']">
                <div class="icno pic flex-start">
                  <img :src="currentPath !== item.path ? item.icno : item.lightIcno" alt="" />
                </div>
              </div>
              <template #title>
                {{ item.name }}
              </template>
            </el-menu-item>
          </template>
        </template>
      </el-menu>
    </div>
    <div class="right-content">
      <div class="header">
        <breadVue />
      </div>
      <div class="content">
        <router-view v-slot="{ Component }">
          <transition name="slide-fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { MyRouteRecordRaw } from '@/router/model'
import { computed, ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import breadVue from './components/bread.vue'
const router = useRouter()
const route = useRoute()

const routes = computed(() => {
  const arr = router.options.routes[2].children as MyRouteRecordRaw[]
  return arr.filter((item) => {
    return item.name !== '个人中心'
  })
})

const currentPath = computed(() => {
  return route.path
})

watch(
  () => {
    return routes.value
  },
  () => {
    if (routes.value.length > 0 && currentPath.value === '/home') {
      router.push(
        (routes.value[0].children as MyRouteRecordRaw[]).length > 0
          ? (routes.value[0].children as MyRouteRecordRaw[])[0].path
          : routes.value[0].path
      )
    }
  },
  {
    deep: true,
    immediate: true
  }
)

const isCollapse = ref(false)

const changeRouter = (path: string) => {
  router.push(path)
}
</script>

<style scoped lang="less">
.home {
  width: 100%;
  height: 100%;
  display: flex;
  .collapse-slide {
    padding-left: 0px !important;
  }
  .left-slide {
    height: 100%;
    transition: all 0.3s;
    background-color: #dce1ee;
    padding-top: 120px;
    padding-left: 16px;
    position: relative;
    overflow-x: hidden;
    .active {
      color: #0b31ba !important;
    }
    .logo {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      top: 32px;
      .none-collapse {
        width: 196px;
        height: 36px;
      }

      .collapse {
        width: 32px;
        height: 36px;
      }
    }
    .toggle {
      position: absolute;
      left: 22px;
      bottom: 32px;
      width: 20px;
      height: 20px;
    }
    .el-menu {
      border-right: none !important;
    }

    .el-menu-vertical-demo:not(.el-menu--collapse) {
      width: 224px;
    }
    // .el-menu--collapse {
    //   .el-menu-item.is-active {
    //     background: #dce1ee !important;
    //   }
    // }
    ::v-deep(.el-menu-vertical-demo) {
      .el-sub-menu__title {
        height: 46px;
        border-radius: 100px 0px 0px 100px;
        &:hover {
          background: #cbd3eb !important;
          border-radius: 100px 0px 0px 100px;
        }
      }

      .el-menu-item {
        height: 46px;
        font-size: 16px;
        color: #565682;
        font-weight: 500;
        border-left: 0px;
        border-radius: 100px 0px 0px 100px;
        &:hover {
          background: #cbd3eb !important;
          border-radius: 100px 0px 0px 100px;
        }
        span {
          margin-left: 20px;
        }
      }
      .el-sub-menu__icon-arrow {
        font-size: 15px !important;
        font-weight: 600 !important;
      }
      .sub-item {
        height: 46px;
        font-size: 16px;
        color: #565682;
        font-weight: 500;
        .icno {
          width: 24px !important;
          height: 24px !important;
          margin-right: 16px;
        }
      }
      .el-menu-item.is-active {
        background: #edeff2;
        border-radius: 100px 0px 0px 100px;
        color: #0b31ba !important;
      }
    }
  }

  .right-content {
    flex: 1;
    width: 0px;
    height: 100%;
    background: #edeff2;
    .header {
      height: 76px;
      padding-left: 24px;
    }
    .content {
      width: 100%;
      height: calc(100% - 76px);
      padding: 0 24px 24px 24px;
    }
  }
}
</style>
