<template>
  <div class="navbar">
    <div class="left_menu">
      <img
        src="../../static/home_logo.png"
        style="height: 30px; width: 100px; margin-right: 60px"
        mode="scaleToFill"
      />
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        active-text-color="#EC7C00"
      >
        <el-menu-item
          index="1"
          @click="
            $router.push({
              path: '/',
              query: 111,
            })
          "
          >首页</el-menu-item
        >
        <el-menu-item
          index="2"
          @click="
            $router.push({
              path: '/trend/index',
            })
          "
          >趋势板块</el-menu-item
        >
        <el-menu-item
          index="3"
          @click="
            $router.push({
              path: '/private/index',
            })
          "
          >个股详情</el-menu-item
        >
        <el-menu-item
          index="4"
          @click="
            $router.push({
              path: '/trade/index',
              query: 111,
            })
          "
          >交易记录</el-menu-item
        >
        <el-menu-item
          index="5"
          @click="
            $router.push({
              path: '/statistics/index',
              query: 111,
            })
          "
          >交易统计</el-menu-item
        >
        <el-menu-item
          index="6"
          @click="
            $router.push({
              path: '/collection/index',
              query: 111,
            })
          "
          >我的收藏</el-menu-item
        >
        <el-menu-item
          index="8"
          @click="
            $router.push({
              path: '/set/index',
              query: 111,
            })
          "
          >设置</el-menu-item
        >
      </el-menu>
    </div>
    <div class="right-menu">
      <el-dropdown class="avatar-container" trigger="click">
        <div class="avatar-wrapper">
          <img src="../../static/moren_avatar.png" class="user-avatar" />
          <div style="margin-left: 20px">
            <div class="name">用户</div>
            <div class="deadline">{{ data.phone }}</div>
          </div>
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown" class="user-dropdown">
          <el-dropdown-item divided @click.native="logout">
            <span style="display: block">退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import Hamburger from "@/components/Hamburger";
import { removeToken } from "@/utils/auth";
import { resetRouter } from "@/router";
import Cookies from "js-cookie";
export default {
  components: {
    Hamburger,
  },
  data() {
    return {
      activeIndex: "1",
      data: {
        avatar: "",
        phone: "",
      },
    };
  },
  created() {
    let index = Cookies.get("NavIndex");
    this.data.phone = Cookies.get("gupiao_phone");
    if (this.data.phone) {
      var reg = /^(\d{3})\d{4}(\d{4})$/;
      this.data.phone = this.data.phone.replace(reg, "$1****$2");
    }
    index ? (this.activeIndex = index) : (this.activeIndex = "1");
    console.log("nav");
  },
  computed: {
    ...mapState(["count"]),
  },
  watch: {
    // watch 可以监听 data 数据 也可以监听 全局 vuex数据
    count() {
      this.activeIndex = this.$store.state.count;
    },
  },
  methods: {
    handleSelect(key) {
      console.log(key);
      Cookies.set("NavIndex", key);
    },
    submit() {
      console.log(111);
    },
    async logout() {
      // await this.$store.dispatch("user/logout");
      Cookies.remove("NavIndex");
      removeToken(); // must remove  token  first
      resetRouter();
      this.$store.commit("RESET_STATE");
      this.$router.push(`/login`);
    },
  },
};
</script>

<style lang="scss" scoped>
.navbar {
  padding: 0 20px;
  height: 61px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .right-menu {
    float: right;
    height: 100%;
    // line-height: 50px;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        font-size: 24rpx;
        margin-top: 5px;
        position: relative;
        display: flex;
        align-items: center;
        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 40px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
.left_menu {
  float: left;
  display: flex;
  align-items: center;
}
.el-dropdown-menu__item--divided {
  border: 0;
  margin: 0;
}
.deadline {
  font-size: 12px;
  margin-top: 5px;
}
.name {
  font-size: 16px;
  font-weight: bold;
}
</style>
