<template>
  <div class="navbar">
    <hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" />

    <breadcrumb id="breadcrumb-container" class="breadcrumb-container" />

    <div class="right-menu">
      <template v-if="device!=='mobile'">
        <!--        <search id="header-search" class="right-menu-item" />-->
        <div class="right-menu-item" style="color: #409EFF">{{ partInfo.full_name }} {{ partInfo.part_name }}</div>
        <div class="right-menu-item" style="color: #ff4949;font-size: 14px">{{ licence }}</div>
        <error-log class="errLog-container right-menu-item hover-effect" />

        <screenfull id="screenfull" class="right-menu-item hover-effect" />

        <el-tooltip content="字体大小" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" />
        </el-tooltip>

      </template>

      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <img :src="avator" class="user-avatar">
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <!--          <router-link to="/profile/index">-->
          <!--            <el-dropdown-item>Profile</el-dropdown-item>-->
          <!--          </router-link>-->
          <router-link to="/">
            <el-dropdown-item>{{ this.$t("action.home") }}</el-dropdown-item>
          </router-link>
          <!--          <a target="_blank" href="https://github.com/PanJiaChen/vue-element-admin/">-->
          <!--            <el-dropdown-item>Github</el-dropdown-item>-->
          <!--          </a>-->
          <!--          <a target="_blank" href="https://panjiachen.github.io/vue-element-admin-site/#/">-->
          <!--            <el-dropdown-item>Docs</el-dropdown-item>-->
          <!--          </a>-->
          <el-dropdown-item divided>
            <el-dropdown placement="right-start" @command="switchLanguage">
              <span class="el-dropdown-link">
                {{ this.$t("action.chooseLang") }}<i class="el-icon-arrow-right el-icon--right" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-for="(value,index) in language" :key="index" :command="value.value">
                  {{value.label}}
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="logout">
            <span style="display:block;">{{ this.$t("action.logout") }}</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import ErrorLog from '@/components/ErrorLog'
import Screenfull from '@/components/Screenfull'
import SizeSelect from '@/components/SizeSelect'
import Search from '@/components/HeaderSearch'
import avator from '@/assets/avatar.jpeg'
import * as API_Part from '@/api/calling-part'
import Storage from '@/utils/storage'
export default {
  components: {
    Breadcrumb,
    Hamburger,
    ErrorLog,
    Screenfull,
    SizeSelect,
    Search
  },
  data() {
    return {
      avator: avator,
      licence: '',
      value: this.$i18n.locale,
      language: [
        {
          value: 'en',
          label: 'English'
        },
        {
          value: 'zh',
          label: '中文简体'
        }
      ]

    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      // 'avatar',
      'device'
    ]),
    partInfo() {
      return this.$store.getters.organization
    }
  },
  mounted() {
    if (Number(this.$store.getters.partId) > 0) {
      API_Part.getExpire().then(res => {
        if (res.timeout === 'nolimit') {
          this.licence = this.$t('action.perpetualLicence')
        } else {
          const days = Math.floor(((Number(res.timeout) * 1000) - new Date()) / (1000 * 60 * 60 * 24))
          if (days > 30) {
            this.licence = this.$t('action.licenseValidity') + new Date((Number(res.timeout) * 1000)).toLocaleString()
          } else {
            this.licence = this.$t('action.licenseRemainsValid') + days + this.$t('action.getLicense')
          }
        }
      }).catch(err => {
        this.$message.error(err)
      })
    }
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$store.dispatch('permission/removeRoutes')
      console.log('hello')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    },
    switchLanguage(value) {
      if (value === 'zh') {
        this.$i18n.locale = 'zh'
      } else if (value === 'en') {
        this.$i18n.locale = 'en'
      }
      // 在选择了显示的语言后，将配置保存到缓存里
      Storage.setItem('DefaultLanguage', value)
      // location.reload()
      this.$router.go(0)
    }
  }
}
</script>

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

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

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

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

  .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 .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

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

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
