<template>
  <el-menu class="navbar" mode="horizontal">
    <hamburger :toggle-click="toggleSideBar" :is-active="sidebar.opened" class="hamburger-container"/>
    <breadcrumb />
    <div class="avatar-container" >
      <!-- 切换地图仅调试用，实际开发不用 -->
      <!-- <div class="theme-main">
        <theme-picker></theme-picker>
      </div> -->
      <el-dropdown trigger="click">
        <div class="avatar-wrapper">
          <svg-icon icon-class="user" class="user-avatar" />
          <span class="user-name">{{userName}}</span>
          <i class="el-icon-caret-bottom"/>
        </div>

        <el-dropdown-menu slot="dropdown" class="user-dropdown">
          <el-dropdown-item>
            <span style="display:block;" @click="logout">退出</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </el-menu>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import Cookies from 'js-cookie'
import { logout } from '@/api/login/index'
import themePicker from '@/components/ThemePicker'
import { getToken, removeToken } from '@/utils/auth'

let userName = ''
setTimeout(() => {
  userName = getToken() ? JSON.parse(getToken()).username : ''
}, 10)

export default {
  data() {
    return {
      userName
    }
  },
  components: {
    Breadcrumb,
    Hamburger,
    themePicker
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar'
    ])
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('ToggleSideBar')
    },
    logout() {
      this.$http(logout(), (res) => {
        this.$store.dispatch('FedLogOut').then(() => {
          location.reload() // 为了重新实例化vue-router对象 避免bug
        })
        removeToken()
        this.$message({
          message: res.message,
          type: 'success'
        })
        // 返回登录页
        this.$router.push('/login')
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.navbar {
  height: 50px;
  line-height: 50px;
  box-shadow:0px 2px 10px 0px rgba(59,119,227,0.2);
  .hamburger-container {
    line-height: 58px;
    height: 50px;
    float: left;
    padding: 0 10px;
  }
  .screenfull {
    position: absolute;
    right: 90px;
    top: 16px;
    color: red;
  }
  .theme-main{
    display: inline-block;
    vertical-align: middle;
    line-height: 50px;
  }
  .avatar-container {
    width: auto;
    height: 50px;
    display: inline-block;
    position: absolute;
    right: 35px;

    .avatar-wrapper {
      cursor: pointer;
      margin-top: 5px;
      position: relative;
      line-height: initial;
      display: inline-block;
      vertical-align: middle;
      color: #5B8CFF;
      .user-avatar {
        width: 20px;
        height: 20px;
        float: left;
        margin:  10px 5px 0 15px;
      }
      .user-name {
        height: 40px;
        line-height: 40px;
      }
      .el-icon-caret-bottom {
        position: absolute;
        right: -20px;
        top: 14px;
        font-size: 12px;
        color: #5B8CFF;
      }
    }
  }
}
</style>

