<template>
  <div class="header-menu">
    <ul class="header-menu-list">
      <li class="header-menu-item" v-for="(menu, index) of menus" :key="menu" @click="click(index, menu)" :class="{ active: activeIndex == index }">
        {{ menu.text }}
      </li>
    </ul>
    <ul class="header-menu-dropdown" :class="{ dropdown: dropdown }">
      <li class="header-menu-dropdown-title" @click="clickMenu()"><i class="fa fa-gear"></i>菜单<i class="fa fa-sort-down down"></i></li>
      <ul class="header-menu-dropdown-list">
        <li class="header-menu-dropdown-item" v-for="(menu, index) of menusDropdown" :key="menu" @click="clickSubMenu(index, menu.code)">
          {{ menu.text }}
        </li>
      </ul>
    </ul>
    <ul class="header-menu-user">
      <li class="header-menu-user-title"><i class="fa fa-user"></i>Administrator</li>
    </ul>
  </div>
</template>
<script>
export default {
  data() {
    return {
      menus: [
        {
          id: "monitor",
          text: "状态监视",
          path: "/",
          isActive: true,
        },
        {
          id: "datacenter",
          text: "数据管理",
          path: "/",
          isActive: false,
        },
        {
          id: "statistic",
          text: "统计分析",
          path: "/",
          isActive: false,
        },
        {
          id: "health",
          text: "健康管理",
          path: "/",
          isActive: false,
        },
        {
          id: "decision",
          text: "决策支持",
          path: "/",
          isActive: false,
        },
        {
          id: "knowledge",
          text: "知识管理",
          path: "/",
          isActive: false,
        },
        {
          id: "report",
          text: "智能报表",
          path: "/",
          isActive: false,
        },
      ],
      activeIndex: 0,
      menusDropdown: [
        {
          text: "子菜单1",
          code: "submenu1",
        },
        {
          text: "子菜单2",
          code: "submenu2",
        },
        {
          text: "子菜单3",
          code: "submenu3",
        },
      ],
      dropdown: false,
    };
  },
  emits: {
    onMenuClick: null,
  },
  methods: {
    click(index, data) {
      this.activeIndex = index;
      this.$emit("onMenuClick", { id: data.id });
    },
    clickSubMenu(index, code) {
      console.log(index, code);
    },
    clickMenu() {
      this.dropdown = !this.dropdown;
    },
  },
  computed: {
    activeClass(data) {
      return data.isActive ? "active" : "";
    },
  },
  watch: {
    $route: {
      handler: function(val, oldVal) {
        this.menus.some((t, index) => {
          if (t.id == val.query.root) {
            this.activeIndex = index;
          }
        });
      },
      //深度观察监听
      deep: true,
    },
  },
};
</script>

<style lang="less">
.header-menu {
  display: flex;
  flex-direction: row;
  width: 100%;
  height: 100%;
  font-size: @fontsize;

  .header-menu-list {
    margin: 0;
    padding: 0;
    list-style: none;
    margin-left: auto;
    display: flex;
    flex-direction: row;
    height: 100%;

    .header-menu-item {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 9.259vh;
      height: 100%;
      color: @gray;
      cursor: pointer;
      transition: color 0.2s ease-in-out;

      &.active {
        color: @green;
        position: relative;
        background: @greenLinearTop;
        transition: color 0.2s ease-in-out;

        &::after {
          content: "";
          position: absolute;
          width: 100%;
          height: 0.463vh;
          border: 0.093vh solid @green;
          border-top: 0;
          left: 0;
          bottom: 0;
          box-sizing: border-box;
        }
      }
    }
  }

  .header-menu-dropdown {
    width: 11.111vh;
    height: 100%;
    margin: 0;
    padding: 0;
    list-style: none;

    .header-menu-dropdown-title {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      color: @gray;
      cursor: pointer;

      i {
        margin: 0 0.741vh;
        transition: all 0.3s;
      }
    }

    .header-menu-dropdown-list {
      display: none;
      margin: 0;
      padding: 0;
      list-style: none;
      margin-left: auto;
      position: absolute;

      .header-menu-dropdown-item {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 11.111vh;
        height: 3.704vh;
        color: @gray;
        cursor: pointer;
        position: relative;
        z-index: 1;
        background-color: fade(@write, 5%);

        &::after {
          content: "";
          position: absolute;
          width: 100%;
          height: 0.463vh;
          border: 0.093vh solid @green;
          border-top: 0;
          left: 0;
          bottom: 0;
          box-sizing: border-box;
        }

        &:hover {
          color: @write;
          background-color: fade(@write, 10%);
        }
      }
    }

    &.dropdown {
      .header-menu-dropdown-title {
        .down {
          transform: rotate(180deg);
        }
      }

      .header-menu-dropdown-list {
        display: flex;
        flex-direction: column;
      }
    }
  }

  .header-menu-user {
    width: 16.667vh;
    height: 100%;
    margin: 0;
    padding: 0;
    list-style: none;

    .header-menu-user-title {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      color: @gray;
      cursor: pointer;

      i {
        margin: 0 0.741vh;
      }
    }
  }
}
</style>
