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

    <breadcrumb class="breadcrumb-container" />

    <div class="right-menu flex-r">
      <template v-if="device !== 'mobile'">
        <search id="header-search" class="right-menu-item" />

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

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

        <div class="right-menu-item hover-effect language-entry">
          <language-switch />
        </div>
      </template>

      <el-dropdown class="avatar-container" trigger="click">
        <div class="avatar-wrapper">
          <div class="user-avatar flex-c" :style="userAvatarColor">{{ firstNameStr }}</div>
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown" class="user-dropdown">
          <el-dropdown-item disabled>{{ $t('common.language') }}</el-dropdown-item>
          <el-dropdown-item
            v-for="item in languageOptions"
            :key="item.value"
            :disabled="$locale === item.value"
            @click.native="changeLanguage(item.value)"
          >
            {{ item.nativeLabel || item.label }}
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="resetPwd">{{ $t('common.resetPassword') }}</el-dropdown-item>
          <el-dropdown-item @click.native="logout">{{ $t('common.logout') }}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <rest-password v-if="resetPasswordVisible" @success="resetPasswordVisible=false" @close="resetPasswordVisible=false" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import RestPassword from '@/components/data/RestPassword'
import Screenfull from '@/components/Screenfull'
import SizeSelect from '@/components/SizeSelect'
import Search from '@/components/HeaderSearch'
import LanguageSwitch from '@/components/LanguageSwitch'
import { getLocaleOptions } from '@/lang'
import { extractColorByText } from '@/utils'

export default {
  name: 'NavbarLight',
  components: {
    Breadcrumb,
    Hamburger,
    RestPassword,
    Screenfull,
    SizeSelect,
    Search,
    LanguageSwitch
  },
  data() {
    return {
      resetPasswordVisible: false
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'device',
      'dashboard',
      'name'
    ]),
    languageOptions() {
      return getLocaleOptions()
    },
    userAvatarColor() {
      return 'background-color:' + extractColorByText(this.name) + ';'
    },
    firstNameStr() {
      return this.name ? this.name.trim().charAt(0) : '?'
    }
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    resetPwd() {
      this.resetPasswordVisible = true
    },
    changeLanguage(locale) {
      this.$setLocale(locale)
    },
    async logout() {
      this.$store.dispatch('user/resetToken').then(() => {
        location.reload()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: linear-gradient(90deg, rgba(255, 250, 243, 0.96), rgba(255, 244, 228, 0.94));
  border-bottom: 1px solid rgba(255, 147, 38, 0.12);
  box-shadow: 0 10px 30px rgba(111, 67, 25, 0.08);
  backdrop-filter: blur(12px);

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

    &:hover {
      background: rgba(255, 147, 38, 0.08);
    }
  }

  .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: #7A5C3F;
      vertical-align: text-bottom;

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

        &:hover {
          background: rgba(255, 147, 38, 0.08);
          color: #FF9326;
        }
      }
    }

    .language-entry {
      font-size: 14px;
    }

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

      .avatar-wrapper {
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 35px;
          height: 35px;
          border-radius: 100%;
          color: #FFFFFF;
          font-weight: bold;
          box-shadow: 0 10px 24px rgba(255, 147, 38, 0.24);
        }

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