<template>
  <el-aside class="siderbar_container" :width="isCollapse ? '72px' : '150px'">
    <div @click="toggleCollapse" class="collapse_box">
      <img v-if="isCollapse" src="@/assets/icon/open.svg" alt="">
      <img v-else src="@/assets/icon/close.svg" alt="">
    </div>
    <el-menu :collapse="isCollapse" router>
      <el-menu-item v-for="item in menuItems" :key="item.route" :index="item.route" :class="activeRoute == item.route ? 'is-active' : ''" >
        <template #default>
          <i class="iconfont" :class="item.icon"></i>
          <span v-if="!isCollapse">{{ item.title }}</span>
        </template>
        <template #title>
          <span v-if="isCollapse">{{ item.title }}</span>
        </template>
      </el-menu-item>
    </el-menu>
  </el-aside>
</template>
<script lang="ts">
export default {
  name: 'SiderBar'
}
</script>
<script lang="ts" setup> 
import { ref, watch, onMounted } from 'vue';
import { useRoute } from 'vue-router'
import router from '@/router';

const route = useRoute()
const activeRoute = ref(route.path)
watch(() => route.path, (newVal) => {
  activeRoute.value = newVal
})

const isCollapse = ref(false)
interface menuItem {
  label: string;
  icon: string;
  route: string;
}
const menuItems = ref<menuItem[]>()

onMounted(() => {
  const menuListStr = localStorage.getItem('menuList')
  console.log(menuListStr)
  if (menuListStr) {
    menuItems.value = JSON.parse(menuListStr)
    // router.push(menuItems.value[0].route)
  }
  console.log(menuItems.value)
});
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}
</script>

<style lang="scss" scoped>
.siderbar_container {
  background: #fff;
  padding: 16px;
}
.el-menu {
  width: 100%;
  border: none;
}
.el-menu-item {
  padding: 0;
}
.iconfont {
  margin-right: 5px;
  display: inline-block;
  color: #666666;
  font-size: 20px;
}
.collapse_box {
  padding-left: 10px;
  // text-align: center;
}
.el-menu.el-menu--vertical :deep(.el-menu-item) {
  height: 40px;
  margin: 10px 0;
  padding: 0 10px;
  border-radius: 4px;
  .iconfont {
    margin-right: 8px;
    text-align: center;
  }
}
.el-menu--collapse .el-menu-item span {
  display: none;
}
.el-menu-item :deep(.el-menu-tooltip__trigger) {
  padding: 0 10px;
}
.el-menu-item {
  &.is-active,
  &.is-active:hover {
    background: #2E64FA;
    color: #fff;
    .iconfont {
      color: #fff;
    }
  }
  &:not(.is-active):hover {
    background: inherit;
    color: #2E64FA;
    .iconfont {
      color: #2E64FA;
    }
  }
}
</style>