<template>
  <div class="navbar">
    <hamburger :toggle-click="toggleSideBar" :is-active="sidebar.opened" class="hamburger-container"/>

    <breadcrumb class="breadcrumb-container"/>

    <div class="right-menu">
      <template v-if="device!=='mobile'">
        <div v-if="userInfo.identifier === 'admin'" class="preview-switch animated fadeIn">
          <el-switch
            v-model="previewSwitch"
            active-text="开"
            inactive-text="关"
            active-color="#13ce66"
            inactive-color="#ff4949"
          @change="handlePreviewSwitch">
          </el-switch>
        </div>

        <a class="animated fadeIn hi">{{ tip }},{{ userInfo.name }}</a>

        <el-tooltip :content="$t('navbar.lock')" effect="dark" placement="bottom">
          <lock class="lock right-menu-item"/>
        </el-tooltip>

        <el-tooltip :content="$t('navbar.screenfull')" effect="dark" placement="bottom">
          <screenfull class="screenfull right-menu-item"/>
        </el-tooltip>

        <el-tooltip :content="$t('navbar.size')" effect="dark" placement="bottom">
          <size-select class="international right-menu-item"/>
        </el-tooltip>

        <el-tooltip :content="$t('navbar.theme')" effect="dark" placement="bottom">
          <theme-picker class="theme-switch right-menu-item"/>
        </el-tooltip>
      </template>

      <el-dropdown class="avatar-container right-menu-item" trigger="click">
        <div class="avatar-wrapper">
          <img :src="userInfo.avatarUrl" class="user-avatar">
          <i class="el-icon-caret-bottom"/>
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/personal/message">
            <el-dropdown-item>
              个人资料
            </el-dropdown-item>
          </router-link>
          <router-link to="/personal/password">
            <el-dropdown-item>
              修改密码
            </el-dropdown-item>
          </router-link>
          <el-dropdown-item divided>
            <span style="display:block;" @click="logout">{{ $t('navbar.logOut') }}</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapState } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import Screenfull from '@/components/Screenfull'
import SizeSelect from '@/components/SizeSelect'
import LangSelect from '@/components/LangSelect'
import ThemePicker from '@/components/ThemePicker'
import Lock from '@/components/Lock'
import { messageSuccess, messageFail } from '@/utils/util'
import { previewSwitch, getPreviewSwitch } from '@/api/admin/route'

export default {
  components: {
    Breadcrumb,
    Hamburger,
    Screenfull,
    SizeSelect,
    LangSelect,
    ThemePicker,
    Lock
  },
  data () {
    return {
      avatarUrl: '',
      tip: '',
      previewSwitch: true
    }
  },
  created () {
    this.userInfo.sex = parseInt(this.userInfo.sex)
    this.getTip()
    this.getPreviewSwitch()
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'name',
      'avatar',
      'device'
    ]),
    ...mapState({
      userInfo: state => state.user.userInfo
    })
  },
  methods: {
    toggleSideBar () {
      this.$store.dispatch('toggleSideBar')
    },
    logout () {
      this.$store.dispatch('LogOut').then(() => {
        location.reload()// In order to re-instantiate the vue-router object to avoid bugs
      })
    },
    getTip () {
      let self = this
      let date = new Date()
      if (date.getHours() >= 0 && date.getHours() < 12) {
        self.tip = '上午好'
      } else if (date.getHours() >= 12 && date.getHours() < 18) {
        self.tip = '下午好'
      } else {
        self.tip = '晚上好'
      }
    },
    handlePreviewSwitch () {
      this.$prompt('请输入Secret', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        previewSwitch({ secret: value, enable: this.previewSwitch }).then(response => {
          console.log(response.data)
          messageSuccess(this, '操作成功')
        }).catch(error => {
          console.error(error)
          this.previewSwitch = !this.previewSwitch
        })
      }).catch(() => {
        this.previewSwitch = !this.previewSwitch
      })
    },
    getPreviewSwitch () {
      getPreviewSwitch().then(response => {
        this.previewSwitch = response.data.data === 'true'
      }).catch(error => {
        console.error(error)
        messageFail(this, '获取演示模式状态失败')
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.navbar {
  height: 50px;
  line-height: 50px;
  border-radius: 0px !important;
  .hamburger-container {
    line-height: 58px;
    height: 50px;
    float: left;
    padding: 0 10px;
  }
  .breadcrumb-container{
    float: left;
  }
  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }
  .right-menu {
    float: right;
    height: 100%;
    &:focus{
     outline: none;
    }
    .right-menu-item {
      display: inline-block;
      margin: 0 8px;
    }
    .lock {
      height: 20px;
    }
    .screenfull {
      height: 20px;
    }
    .international{
      vertical-align: top;
    }
    .theme-switch {
      vertical-align: 15px;
    }
    .avatar-container {
      height: 50px;
      margin-right: 30px;
      .avatar-wrapper {
        cursor: pointer;
        margin-top: 5px;
        position: relative;
        .user-avatar {
          width: 40px;
          height: 40px;
          border-radius: 50%  !important;
        }
        .el-icon-caret-bottom {
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
    .hi {
      vertical-align: 15px;
      margin-right: 30px;
    }
  }
  .preview-switch {
    vertical-align: 15px;
    margin-right: 30px;
    display: inline-block;
  }
}
</style>
