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

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

    <div class="right-menu">
      <div class="navbar-flex-wrapper">
        <template v-if="device !== 'mobile'">
          <screenfull id="screenfull" class="right-menu-item hover-effect" />
        </template>
        <!-- <div class="avatar-container">
        <img src="@/assets/avatar.png" class="user-avatar">
      </div> -->
        <div style="margin-left: 20px">
          <el-dropdown class="dropdown-container" trigger="click">
            <span class="el-dropdown-link">
              {{ `${name} (${userTypeShortName})` }}
              <!-- {{ name }}
              <span v-if="userTypeShortName"> ({{ userTypeShortName }}) </span> -->
              <i class="el-icon-caret-bottom" />
            </span>
            <el-dropdown-menu slot="dropdown" class="user-dropdown">
              <el-dropdown-item v-if="!isReviewer">
                <span style="display: block" @click="editInfo">{{
                  $t('system:navbar:button:Profile')
                }}</span>
              </el-dropdown-item>
              <!-- 切换角色 -->
              <el-dropdown-item divided v-if="hasRole">
                <span style="display: block" @click="openToggleRole">{{
                  $t('system:navbar:button:toggleRole')
                }}</span>
              </el-dropdown-item>
              <el-dropdown-item divided>
                <span style="display: block" @click="logout">{{
                  $t('system:navbar:button:Log Out')
                }}</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <TopLang></TopLang>
      </div>
    </div>
    <toggleRole v-if="toggleRoleVisible" :visible.sync="toggleRoleVisible" :loading="toggleRoleLoading"
      @save="loginByRole" />
  </div>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import Screenfull from '@/components/Screenfull'
import TopLang from './topLang'
import toggleRole from '@/components/toggleRole'
import { resetRouter } from '@/router'
export default {
  components: {
    Breadcrumb,
    Hamburger,
    Screenfull,
    TopLang,
    toggleRole,
  },
  data() {
    return {
      isReviewer: false,
      toggleRoleVisible: false,
      toggleRoleLoading: false,
    }
  },
  computed: {
    ...mapGetters(['sidebar', 'name', 'device', 'userTypeShortName']),
    roles() {
      return this.$store.state.user.roles
    },
    hasRole() {
      return this.roles && this.roles.length > 1
    },
  },
  created() {
    // this.isReviewer = JSON.parse(zzSessionStorage.getItem('IsReviewer'))
  },
  methods: {
    ...mapMutations({ setLanguage: 'lang/setLanguage' }),
    openToggleRole() {
      this.$store.dispatch('user/getUserInfo').then((res) => {
        this.toggleRoleVisible = true
      })
    },
    loginByRole(userRoleId) {
      if (this.$store.state.user.userId === userRoleId) {
        this.toggleRoleVisible = false
        this.toggleRoleLoading = false
        return false
      }
      this.toggleRoleLoading = true
      this.$store
        .dispatch('user/loginByRole', { userRoleId })
        .then((res) => {
          if (res) {
            this.$store
              .dispatch('permission/generateRoutes')
              .then(async (res) => {
                if (res && res.length > 0) {
                  resetRouter()
                  await this.$store.dispatch('global/getNoticeList')
                  this.$router.addRoutes(res)
                  this.toggleRoleLoading = false
                  if (this.loginType === 'DevOps') {
                    this.$router.replace({ path: res[0].path })
                    return
                  }
                  if (this.hasPermi(['role:radmin'])) {
                    this.$router.replace({ path: res[0].path })
                    return
                  }
                  if (
                    this.hasPermi([
                      'role:air',
                      'role:rpm',
                      'role:rcrc',
                      'role:rir',
                    ])
                  ) {
                    history.replaceState(null, null, '/trials/trials-list')
                    history.go(0)
                  } else {
                    history.replaceState(null, null, '/trials/trials-workbench')
                    history.go(0)
                  }
                  this.toggleRoleVisible = false
                  this.toggleRoleLoading = false
                  this.$EventBus.$emit('reload')
                } else {
                  // 此账户暂未配置菜单权限，请联系管理员处理后再登录。
                  this.toggleRoleLoading = false
                  this.$message.warning(this.$t('login:message:login2'))
                }
              })
              .catch((err) => {
                console.log(err)
                this.toggleRoleLoading = false
              })
          } else {
            this.toggleRoleLoading = false
          }
        })
        .catch(() => {
          this.toggleRoleLoading = false
        })
    },
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      var loginType = zzSessionStorage.getItem('loginType')
      await this.$store.dispatch('user/logout')
      if (loginType) {
        this.$router.push(`/login?loginType=${loginType}`)
      } else {
        this.$router.push(`/login`)
      }
      // //this.$i18n.locale = 'zh'
      // //this.setLanguage('zh')
      this.$updateDictionary()
    },
    editInfo() {
      this.$router.push({ name: 'BaiscInfo' })
    },
  },
}
</script>

<style lang="scss" scoped>
.navbar-flex-wrapper {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.navbar {
  height: 50px;
  overflow: hidden;
  // position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

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

  .right-menu {
    // float: right;
    height: 100%;
    line-height: 65px;

    // position: relative;
    &: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 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-container {
      display: inline-block;
      margin-right: 200px;

      .user-avatar {
        margin-top: 5px;
        width: 40px;
        height: 40px;
        border-radius: 10px;
      }
    }

    .dropdown-container {
      // margin-left: 30px;
      // position: absolute;
      // right: 20px;
      // top: 0px;
      cursor: pointer;

      .el-icon-caret-bottom {
        font-size: 12px;
      }
    }

    .el-dropdown-link {
      color: #999c9e;
      cursor: pointer;
    }
  }
}
</style>
