<template>
  <scroll-bar class="navbar">
    <el-menu
      mode="vertical"
      :default-active="$route.path"
      unique-opened
      background-color="transparent"
      text-color="#a9c2fb"
      active-text-color="#fff"
      ref="menu"
    >
      <sidebar-item :routes="routers"></sidebar-item>
    </el-menu>
  </scroll-bar>
</template>

<script>
import Vue from "vue";
import SidebarItem from "./SidebarItem";
import ScrollBar from "@/components/ScrollBar";
import { Menu, MenuItem, Submenu } from "element-ui";
// import { constantRouterMap } from "../../../../router/index.js";
Vue.component(Menu.name, Menu);
Vue.component(Submenu.name, Submenu);
Vue.component(MenuItem.name, MenuItem);

export default {
  name: "sidebar-nav",
  components: {
    SidebarItem,
    ScrollBar
  },
  props: {
    routers: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    $route(newRoute, oldRoute) {
      if (newRoute.matched.length === 1 && oldRoute.matched.length > 1) {
        this.$refs.menu.close(
          oldRoute.matched[0].name || oldRoute.matched[0].path
        );
      }
    }
  },
  computed: {
    isCollapse() {
      return this.$route.matched.length === 2;
    }
  }
};
</script>

<style lang="scss">
@import "../../assets/styles/variables.scss";
.navbar {
  transition: width 0.28s;
  width: $sideBar-width;
  height: 100%;
  z-index: 1001;
  overflow: hidden;
  // background-image: linear-gradient(0deg, #2362f6, #6895fe);
  a {
    display: inline-block;
    text-decoration: none;
    width: 100%;
  }
  .svg-icon {
    margin-right: 6px;
  }
  .img-icon {
    width: 8px;
    height: 8px;
    margin-right: 6px;
  }
  .el-menu {
    border: none;
    width: 100%;
  }
  .hideSidebar {
    .sidebar-container,
    .sidebar-container .el-menu {
      width: 40px;
      // overflow: inherit;
    }
    .main-container {
      margin-left: 40px;
    }
  }
  .hideSidebar {
    .submenu-title-noDropdown {
      padding-left: 10px;
      position: relative;
      span {
        height: 0;
        width: 0;
        overflow: hidden;
        visibility: hidden;
        transition: opacity 3s cubic-bezier(0.55, 0, 0.1, 1);
        opacity: 0;
        display: inline-block;
      }
      &:hover {
        span {
          display: block;
          border-radius: 3px;
          z-index: 1002;
          width: 140px;
          height: 56px;
          visibility: visible;
          position: absolute;
          right: -145px;
          text-align: left;
          text-indent: 20px;
          top: 0px;
          background-color: $subMenuBg !important;
          opacity: 1;
        }
      }
    }
    .el-submenu {
      & > .el-submenu__title {
        padding-left: 10px !important;
        & > span {
          display: none;
        }
        .el-submenu__icon-arrow {
          display: none;
        }
      }
      .nest-menu {
        .el-submenu__icon-arrow {
          display: block !important;
        }
        span {
          display: inline-block !important;
        }
      }
    }
  }
  .el-menu-item,
  .el-submenu .el-menu-item,
  .el-submenu__title {
    height: 40px;
    line-height: 40px;
    padding: 0 0 0 8px !important;
    font-size: 12px;
  }
  .el-menu-item {
    &:hover {
      background-color: $menuHover !important;
    }
    &.is-active {
      background-color: $menuHover !important;
    }
    .menu-arrow {
      position: absolute;
      top: 10px;
      right: 0;
      border-right: 8px solid #004cff;
      border-top: 8px solid transparent;
      border-bottom: 8px solid transparent;
    }
    &.is-active .menu-arrow {
      border-right-color: #eee;
    }
  }
  .nest-menu .el-submenu > .el-submenu__title,
  .el-submenu .el-menu-item {
    background-color: $subMenuBg !important;
    width: $sideBar-width;
    min-width: $sideBar-width;
    text-align: center;
    &:hover {
      background-color: $menuHover !important;
    }
    &.is-active {
      background-color: $menuActions !important;
    }
  }
  .el-menu--collapse .el-menu .el-submenu {
    min-width: 132px !important;
  }
  .el-submenu {
    .el-submenu__icon-arrow {
      right: 8px;
      margin-top: -5px;
    }
    .el-submenu__title {
      background-color: transparent !important;
      position: relative;
      display: flex;
      align-items: center;
      &::after {
        content: "";
        position: absolute;
        z-index: 2;
        bottom: -5px;
        left: 0;
        right: 0;
        width: 100%;
        height: 5px;
        background-image: linear-gradient(180deg, #1252e9, transparent);
        transition: all 0.28s 0.28s;
        opacity: 0;
      }
    }
    .el-menu-item {
      text-align: left;
      text-indent: 20px;
    }
    &.is-opened {
      .el-submenu__title {
        color: #fff !important;
        background-color: $subMenuBg !important;
        &::after {
          opacity: 1;
        }
      }
    }
  }
}

@media screen and (min-width: $bigScreenWidth) {
  $sideBar-width: 132px;
  .navbar {
    width: $sideBar-width;
    .img-icon {
      width: 16px;
      height: 16px;
      margin-right: 6px;
    }
    .el-menu-item,
    .el-submenu .el-menu-item,
    .el-submenu__title {
      height: 56px;
      line-height: 56px;
      padding: 0 0 0 12px !important;
      font-size: 14px;
    }
    .el-menu-item {
      .menu-arrow {
        top: 18px;
      }
    }
    .nest-menu .el-submenu > .el-submenu__title,
    .el-submenu .el-menu-item {
      width: $sideBar-width;
      min-width: $sideBar-width;
    }
    .el-submenu {
      .el-menu-item {
        text-indent: 25px;
      }
    }
  }
}
</style>

