<template>
  <div class="navs" :style="{ background: COLOR.NAVS_BACKGROUND }">
    <div class="big-scene-nav" v-if="show_big_scene_nav">
      <img class="navs-back-img" src="/images/img11.png" alt="" />
      <div
        class="big-scene-title"
        :style="{ 'background-image': COLOR.NAVS_TITLE_COLOR }"
      >
        {{ CONFIG.TITLE }}
      </div>
    </div>
    <transition name="el-collapse-transition">
      <div class="normal-nav">
        <div class="logo">
          <img :src="CONFIG.LOGO_URL" alt="" />
        </div>
        <div
          class="title"
          v-if="!show_big_scene_nav"
          :style="{ 'background-image': COLOR.NAVS_TITLE_COLOR }"
        >
          {{ CONFIG.TITLE }}
        </div>

        <div class="nav-item">
          <div
            class="item"
            v-for="(item, index) in routes"
            :key="index"
            @mouseenter.stop="handlerSelectNavPrev(item, index, true)"
            @mouseleave.stop="handlerSelectNavPrev(item, index, false)"
            @click.stop="handlerClickNavPrev(item, index)"
          >
            <div v-show="show_big_scene_nav" class="nav-item-img-none"></div>
            <img
              v-show="!show_big_scene_nav"
              :src="item.meta.iconfont"
              :alt="item.meta.name"
            />
            <span :style="{ color: COLOR.NAVS_SIDEBAR_COLOR }"
              >{{ item.meta.name }}
            </span>
            <div
              :style="{ 'background-color': COLOR.NAVS_SIDEBAR_SELECT_LINE }"
              :class="index === page_nav_index ? 'nav-item-active' : ''"
            ></div>
            <transition name="menu-fade">
              <div
                class="sidebar"
                v-if="index === select_nav_index && item.children"
              >
                <div class="sidebar-box">
                  <div
                    class="sidebar-item"
                    v-for="(item2, index2) in item.children"
                    :key="index2"
                    @click.stop="handlerClickNavNext(item2, index, index2)"
                  >
                    <span>{{ item2.meta.name }}</span>
                  </div>
                </div>
              </div>
            </transition>
          </div>
        </div>
        <div class="nav-end"></div>
      </div>
    </transition>
  </div>
</template>

<script>
import { deepClone } from "../../tool/Tool";
import { loginout } from "../../api/login";
export default {
  name: "Navs",
  data() {
    return {
      select_nav_index: 0,
      page_nav_index: 0,
    };
  },
  computed: {
    routes() {
      // 加载Layout下面的路由
      let _router_copy = deepClone(this.$router.options.routes);
      let _routers = _router_copy.filter((e) => e.name === "Layout");
      return _routers[0].children.filter((item) => item.show == true);
    },
    show_big_scene_nav() {
      return this.$store.getters.show_big_scene_nav;
    },
  },
  created() {
    this.handlerGetPageNavIndex(this.$route.path);
  },
  watch: {
    $route: function (e) {
      this.handlerGetPageNavIndex(e.path);
    },
  },
  methods: {
    //鼠标移入一级菜单
    handlerSelectNavPrev(item, index, type) {
      if (!type) {
        this.select_nav_index = -1;
        return false;
      }
      if (item.children) {
        this.select_nav_index = index;
      } else {
        this.select_nav_index = -1;
      }
    },
    //选择一级菜单
    handlerClickNavPrev(item, index) {
      if (item.children) {
        return false;
      } else {
        this.page_nav_index = index;
        if (item.meta.name == "退出") {
          loginout().then((res) => {
            if (res.data.code == 200) {
              localStorage.removeItem("X-Token");
              this.$router.push("/");
              this.$message.success('退出成功')
            }
          });
        } else {
          this.$router.push(item.path);
        }
      }
    },
    //选择二级菜单
    handlerClickNavNext(item, index) {
      this.page_nav_index = index;
      if (item.meta.name == "退出") {
        loginout().then((res) => {
          if (res.data.code == 200) {
            localStorage.removeItem("X-Token");
            this.$router.push("/");
          }
        });
      } else {
        this.$router.push(item.path);
      }
    },
    // 页面刷新时，选中当前的按钮
    handlerGetPageNavIndex(path) {
      let i = 0;
      let j = 0;
      while (i < this.routes.length) {
        if (path === this.routes[i].path) {
          this.page_nav_index = i;
          break;
        } else {
          if (this.routes[i].children) {
            while (j < this.routes[i].children.length) {
              if (path === this.routes[i].children[j].path) {
                this.page_nav_index = i;
                break;
              }
              j++;
            }
          }
        }
        // console.log(i);
        i++;
      }
    },
  },
};
</script>

<style scoped>
.navs {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 70px;
}

.normal-nav {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-color: #2773fe;
}

.navs .navs-back-img {
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: -1;
}

.navs .logo {
  width: 60px;
  height: 60px;
  margin: 0 25px;
}

.navs .logo img {
  width: 100%;
  height: 100%;
}

.navs .title {
  font-size: 32px;
  font-weight: bold;
  font-family: Avenir, Helvetica, Arial, sans-serif;
  min-width: 300px;
  flex: 1;
  -webkit-background-clip: text;
  color: transparent;
}

.navs .big-scene-nav {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 5;
}

.navs .big-scene-nav .big-scene-title {
  font-size: 32px;
  font-weight: bold;
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-background-clip: text;
  color: transparent;
  text-align: center;
  margin-top: 5px;
}

.navs .nav-item {
  display: flex;
  margin-right: 40px;
  height: 100%;
  position: absolute;
  top: 0;
  right: 50px;
  z-index: 6;
}

.nav-item-active {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 4px;
}

.navs .nav-item .item {
  display: flex;
  flex-direction: column;
  text-align: center;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  width: 80px;
  margin: 0 10px;
  position: relative;
}

.navs .nav-item .item img {
  width: 30px;
  height: 30px;
}

.navs .nav-item .item .nav-item-img-none {
  width: 30px;
  height: 30px;
  background: transparent;
}

.navs .nav-item .item span {
  font-size: 14px;
  margin-top: 5px;
  font-weight: bold;
}

.sidebar {
  position: absolute;
  left: 0;
  top: 100%;
  width: 200px;
  justify-content: center;
  align-items: center;
  z-index: 3;
}

.sidebar-box {
  margin-top: 10px;
  padding: 10px;
}

.sidebar-item {
  width: 100px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fafaff;
}
.sidebar-item:hover {
  background: rgb(167, 167, 167);
}

.menu-fade-enter,
.menu-fade-leave-to {
  top: 100%;
  opacity: 1;
  transition: all 0.2s;
}

.menu-fade-enter-active,
.menu-fade-leave-active {
  top: 100%;
  opacity: 0;
  transition: all 0.2s;
}
</style>