<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 @click="toLogout">
          <div class="menu-item">
            <i class="icon icon-logout"></i>
            <span>退出登录</span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { headerMenuConfig } from "./menu";

export default {
  name: "NavBar",
  computed: {
    username() {
      return this.$store.state.user.name;
    },
  },
  data() {
    return {
      navs: headerMenuConfig,
      curNav: "",
    };
  },
  methods: {
    toPage(nav) {
      this.curNav = nav.name;
      this.$emit("on-nav-change", nav.name);
    },
    toLogout() {
      // console.log('Logout');
      this.$router.push("/login");
    },
  },
};
</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>
