<template>
  <div class="nav-bar">
    <div class="nav-bar-logo">
      <img
        src="https://img.alicdn.com/tfs/TB13UQpnYGYBuNjy0FoXXciBFXa-242-134.png"
        width="40"
      />
      <!-- <span class="site-name">ADMIN LITE</span> -->
    </div>
    <div class="nav-bar-menu menu-list">
      <ul>
        <li
          v-for="(nav, index) in navs"
          :key="index"
          @click="toPage(nav)"
          :class="{ 'menu-item-act': curNav === nav.name }"
        >
          <div class="menu-item">
            <i
              :class="[
                'icon',
                curNav === nav.name ? `${nav.icon}-act` : `${nav.icon}`,
              ]"
            ></i>
            <span>{{ nav.title }}</span>
          </div>
        </li>
      </ul>
    </div>
    <div class="nav-bar-user menu-list">
      <ul>
        <li>
          <div class="menu-item">
            <i class="icon icon-user"></i>
            <span v-if="username">{{ username }}，欢迎你</span>
          </div>
        </li>
        <li>
          <div class="menu-item" @click="toFullscreen">
            <i class="icon icon-full-screen"></i>
            <span>全屏</span>
          </div>
        </li>
        <li>
          <div class="menu-item" @click="toLogout">
            <i class="icon icon-logout"></i>
            <span>退出登录</span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import {
  systemMenuConfig,
  businessMenuConfig,
  invigilationMenuConfig,
  headerMenuConfig,
} from "./menu";

export default {
  name: "NavBar",
  computed: {
    username() {
      return this.$store.state.user.name;
    },
  },
  data() {
    return {
      navs: headerMenuConfig,
      curNav: "",
      modaleNavs: {
        systemMenuConfig,
        business: businessMenuConfig,
        invigilation: invigilationMenuConfig,
      },
      isFullscreen: false,
    };
  },
  mounted() {
    const curRouterName = this.$route.meta.relate || this.$route.name;
    Object.keys(this.modaleNavs).forEach((mkey) => {
      if (this.curNav) return;
      let curRouter = null;
      this.modaleNavs[mkey].forEach((item) => {
        item.children.forEach((elem) => {
          if (elem.name === curRouterName) curRouter = elem;
        });
      });

      if (curRouter) this.toPage({ name: mkey });
    });
    this.registFullscreenChange();
  },
  methods: {
    toPage(nav) {
      this.curNav = nav.name;
      this.$emit("on-nav-change", nav.name);
    },
    toLogout() {
      // console.log('Logout');
      this.$router.push("/login");
    },
    checkDocIsFullscreen() {
      return (
        document.fullscreenElement ||
        document.msFullscreenElement ||
        document.mozFullscreenElement ||
        document.webkitFullscreenElement
      );
    },
    registFullscreenChange() {
      document.documentElement.onfullscreenchange = () => {
        if (this.checkDocIsFullscreen()) {
          this.isFullscreen = true;
          document.body.className += "app-fullscreen";
        } else {
          this.isFullscreen = false;
          document.body.className = document.body.className.replace(
            "app-fullscreen",
            ""
          );
        }
      };

      document.addEventListener("keydown", (e) => {
        if (
          e.keyCode === 122 ||
          (e.keyCode === 27 && this.checkDocIsFullscreen())
        ) {
          e.preventDefault();
          this.toFullscreen();
        }
      });
    },
    async toFullscreen() {
      const fullscreenEnabled =
        document.fullscreenEnabled ||
        document.mozFullScreenEnabled ||
        document.webkitFullscreenEnabled ||
        document.msFullscreenEnabled;
      if (!fullscreenEnabled) {
        this.$message.error("当前浏览器不支持全屏！");
        return;
      }
      const de = document.documentElement;
      const requestFullscreen =
        de.requestFullscreen ||
        de.mozRequestFullScreen ||
        de.webkitRequestFullscreen;
      const exitFullscreen =
        document.exitFullscreen ||
        document.mozCancelFullScreen ||
        document.webkitCancelFullScreen;

      if (this.isFullscreen) {
        await exitFullscreen.call(document).catch(() => {});
      } else {
        await requestFullscreen.call(de).catch(() => {});
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 60px;
  z-index: 1001;
  overflow: hidden;
  background: #fff;

  &::after {
    content: "";
    position: absolute;
    width: 100%;
    left: 0;
    bottom: 0;
    z-index: auto;
    border-bottom: 1px solid #eee;
  }

  &-logo {
    width: 220px;
    float: left;
    padding: 14px 30px;
    font-size: 20px;
    line-height: 32px;
    > img {
      display: block;
      max-width: 160px;
      height: 32px;
    }
  }
  &-menu {
    float: left;
  }
  &-user {
    float: right;
  }
  .menu-list {
    ul {
      padding: 0;
    }
    li {
      display: inline-block;
      vertical-align: top;
      padding: 20px 15px;
      line-height: 20px;
      font-size: 14px;
      position: relative;
      text-align: center;
      font-weight: 600;
      color: #626a82;
      cursor: pointer;

      &:hover,
      &.menu-item-act {
        color: #1886fe;
      }

      span {
        display: inline-block;
        vertical-align: middle;
        margin-left: 8px;
      }
    }
  }
}
</style>
