<template>
  <nav class="aui-navbar"
       :class="`aui-navbar--${$store.state.navbarLayoutType}`">
    <div class="aui-navbar__header">
      <h1 class="aui-navbar__brand"
          style="text-transform: none;"
          @click="$router.push({ name: 'home' })">
        <a class="aui-navbar__brand-lg"
           href="javascript:;">{{ $t('brand.lg') }}</a>
        <a class="aui-navbar__brand-mini"
           href="javascript:;">{{ $t('brand.mini') }}</a>
      </h1>
    </div>
    <div class="aui-navbar__body">
      <el-menu class="aui-navbar__menu"
               mode="horizontal">
        <el-menu-item index="1"
                      @click="$store.state.sidebarFold = !$store.state.sidebarFold">
          <svg class="icon-svg aui-navbar__icon-menu aui-navbar__icon-menu--switch"
               aria-hidden="true">
            <use xlink:href="#icon-outdent"></use>
          </svg>
        </el-menu-item>
        <el-menu-item index="2"
                      @click="refresh()">
          <svg class="icon-svg aui-navbar__icon-menu aui-navbar__icon-menu--refresh"
               aria-hidden="true">
            <use xlink:href="#icon-sync"></use>
          </svg>
        </el-menu-item>
      </el-menu>
      <el-menu
          class="aui-navbar__menu"
          mode="horizontal"
          @select="selectGoHome">
          <el-menu-item index="1">首页</el-menu-item>
        </el-menu>
      <div class="mr-auto aui-menu-w menu-width">
        <i class="arrow el-icon-arrow-left" @click="scrollTab('left')"></i>
        <el-menu
          class="aui-navbar__menu scroll-ui"
          mode="horizontal"
          id="tab-menu"
          @select="handleSelect">
          <el-menu-item v-for="(item,index) in routeList_F" :key="index" :id="'li'+index" :index="index.toString()">{{item.name}}</el-menu-item>
        </el-menu>
        <i class="arrow el-icon-arrow-right" @click="scrollTab('right')"></i>
      </div>
      <el-menu class="aui-navbar__menu"
               mode="horizontal">
        <!-- <el-menu-item index="1">
          <el-dropdown placement="bottom"
                       :show-timeout="0">
            <el-button size="mini">{{ $t('_lang') }}</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="(val, key) in i18nMessages"
                                :key="key"
                                @click.native="$i18n.locale = key">{{ val._lang }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
        <el-menu-item index="2">
          <a href="//www.renren.io/"
             target="_blank">
            <svg class="icon-svg aui-navbar__icon-menu"
                 aria-hidden="true">
              <use xlink:href="#icon-earth"></use>
            </svg>
          </a>
        </el-menu-item> -->
        <el-menu-item index="3"
                      @click="fullscreenHandle()">
          <svg class="icon-svg aui-navbar__icon-menu"
               aria-hidden="true">
            <use xlink:href="#icon-fullscreen"></use>
          </svg>
        </el-menu-item>
        <el-menu-item index="4"
                      class="aui-navbar__avatar">
          <el-dropdown placement="bottom"
                       :show-timeout="0">
            <span class="el-dropdown-link">
              <img src="~@/assets/img/avatar.png">
              <span>{{ $store.state.user.realName }}</span>
              <i class="el-icon-arrow-down"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()">{{ $t('updatePassword.title') }}</el-dropdown-item>
              <el-dropdown-item @click.native="logoutHandle()">{{ $t('logout') }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password v-if="updatePassowrdVisible"
                     ref="updatePassowrd"></update-password>
  </nav>
</template>

<script>
import { messages } from '@/i18n'
import screenfull from 'screenfull'
import UpdatePassword from './main-navbar-update-password'
import { clearLoginInfo } from '@/utils'
export default {
  inject: ['refresh'],
  data () {
    return {
      i18nMessages: messages,
      updatePassowrdVisible: false,
      offsetCount: 0
    }
  },
  props: {
    routeList_F: {
      type: Array,
      required: true
    }
  },
  components: {
    UpdatePassword
  },
  methods: {
    // 全屏
    fullscreenHandle () {
      if (!screenfull.enabled) {
        return this.$message({
          message: this.$t('fullscreen.prompt'),
          type: 'warning',
          duration: 500
        })
      }
      screenfull.toggle()
    },
    // 修改密码
    updatePasswordHandle () {
      this.updatePassowrdVisible = true
      this.$nextTick(() => {
        this.$refs.updatePassowrd.init()
      })
    },
    // 退出
    logoutHandle () {
      this.$confirm(this.$t('prompt.info', { 'handle': this.$t('logout') }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.post('/auth/logout').then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          window.sessionStorage.removeItem('routeList')
          clearLoginInfo()
          this.$router.push({ name: 'login' })
          window.localStorage.setItem('isGoProgect', false)
        }).catch(() => { })
      }).catch(() => { })
    },
    handleSelect (e) {
      this.$emit('routeList_child', JSON.stringify(this.routeList_F[e].children))
    },
    selectGoHome (e) {
      window.localStorage.setItem('isGoProgect', false)
      this.$router.go(0)
    },
    // 滑动tab页签
    scrollTab (direction) {
      if (this.routeList_F.length === 0) return
      let menu = document.querySelector('#tab-menu')
      // 计算ul宽度和当前
      let ulWidth = menu.clientWidth
      const getWidth = (idName) => {
        let li = document.querySelector(idName)
        return li.clientWidth
      }
      let liWidth = 0
      this.routeList_F.forEach((ele, index) => {
        liWidth = liWidth + getWidth(`#li${index}`)
      })
      // 根据方向和宽度计算偏移量
      if (liWidth < ulWidth) return
      let distance = this.offsetCount * 100

      if (direction === 'left') {
        if (this.offsetCount > 0) {
          this.offsetCount = this.offsetCount - 1
        }
      } else if (direction === 'right') {
        if ((ulWidth + distance) < liWidth) {
          this.offsetCount = this.offsetCount + 1
        }
      }
      distance = this.offsetCount * 100
      menu.scrollTo({
        'left': distance,
        'behavior': 'smooth'
      })
      // console.log(distance)
    }
  }
}
</script>
<style lang="scss" scoped>
.aui-navbar{
  background: linear-gradient(180deg, #0445EC, #1BACFF);
  box-shadow: 0px 2px 8px 0px rgba(187, 187, 227, 0.35);
}
.menu-width{
  display: flex;
  align-items: center;
  justify-content: center;
  width: 70%;
}
.arrow{
  color: #fff;
  cursor: pointer;
  margin: 0 20px;
}
.scroll-ui {
  display: flex;
  height: 100%;
  overflow-x: scroll;
  overflow-y: hidden;
  -webkit-overflow-scrolling : touch;
  scrollbar-width: none; /* firefox */
  -ms-overflow-style: none; /* IE 10+ */
}
.scroll-ui::-webkit-scrollbar {
  display: none; /* Chrome Safari */
}
</style>
