<template>
  <div class="navbar">
    <hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar"/>
    <div class="right-menu">
      <template v-if="device !== 'mobile'">
        <screenfull id="screenfull" class="right-menu-item hover-effect" />
      </template>
      <div class="time">
        <div>{{ time }}</div>
        <div>{{getDisplayName()}},欢迎您！</div>
      </div>
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click" >
        <div class="avatar-wrapper">
          <img :src="adapterUrl(avatar || defaultFace,undefined,undefined,undefined,true)" class="user-avatar">
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
<!--          <router-link to="/">-->
<!--            <el-dropdown-item>平台首页</el-dropdown-item>-->
<!--          </router-link>-->
          <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 { mapGetters } from 'vuex'
import Hamburger from '../../components/Hamburger'
import Screenfull from '../../components/Screenfull'
import common from "../../components/Common/common";

export default {
  name: 'Navbar',
  mixins: [common], //监听外部框架的大小改变事件
  components: {
    Hamburger,
    Screenfull
  },
  data() {
    return {
      defaultFace: require('../../assets/common/face-man.png'),
      time: '',
      timer: null,
    }
  },
  computed: {
    ...mapGetters(['sidebar', 'avatar', 'device','name','userName']),
  },
  created() {
    clearInterval(this.timer);
    this.timer = null;
    this.setTimer()
  },
  destroyed() {
    clearInterval(this.timer);
    this.timer = null
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    getTime() {
    },
    setTimer() {
      if (this.timer == null) {
        this.timer = setInterval(() => {
          this.time = new Date().format('yyyy-MM-dd HH:mm:ss')
        }, 1000)
      }
    },
    async logout() {
      await this.$store.dispatch('user/logout');
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    },
    getDisplayName() {
      return this.userName?this.userName:this.name;
    }
  }
}
</script>

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

  .hamburger-container {
    line-height: 36px;
    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;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;
    // width: 480px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    line-height: 40px;
    margin-right: 25px;
    &:focus {
      outline: none;
    }
    .time {
      /*border-left: 1px solid #a5a9b1;*/
      /*padding: 0px 15px;*/
      color: #222222;
      font-size: 14px;
      // width: 340px;
      line-height: 40px;
      display:flex;
      div{
        display: inline-block;
        // width: 150px;
        margin: 0px 10px;
      }
    }
    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 14px;
      color: #5a5e66;
      vertical-align: text-bottom;
      &.iconfont:before {
        font-size: 24px;
      }
      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;
        color: #30B08F;
        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }
    .avatar-container {
      margin-right: 30px;
      .avatar-wrapper {
        margin-top: 5px;
        position: relative;
        .user-avatar {
          cursor: pointer;
          width: 30px;
          height: 30px;
          border-radius: 20px;
          overflow: hidden;
          background-color: #12B0B6;
        }
        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 10px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
