<template>
  <div v-if="!loading" v-loading="loading" class="right-menu">
    <div class="dk-nav-operation z">
      <ul>
        <li v-if="layout !== 'default'" v-auth="['administrator']">
          <a href="/admin/#/dashboard"><i class="el-icon-set-up" /> 控制台</a>
        </li>
        <li v-if="layout !== 'customer'" v-auth="['customer']">
          <a href="/admin/#/customer"><i class="el-icon-monitor" /> 客户端</a>
        </li>
      </ul>
    </div>
    <slot name="user-info" />
    <div v-html="hooks['header_user_left_hook']" />
    <div class="dk-nav-avatar z">
      <el-dropdown class="avatar-container" trigger="click">
        <div class="avatar-wrapper">
          <el-avatar :src="userinfo.avatar" class="user-avatar" alt="">
            <i class="el-icon-user-solid" style="font-size:20px" />
          </el-avatar>
          <div class="username ">{{ userinfo.username }}</div>
        </div>
        <el-dropdown-menu slot="dropdown" class="user-dropdown">
          <el-dropdown-item v-if="userGroup">
            <el-tag size="mini" :type="userGroup.type">{{ userGroup.group_name }}</el-tag>
          </el-dropdown-item>
          <el-dropdown-item v-for="(item,key) in userMenus" :key="key" @click="onClickMenu(item)">
            {{ item.title }}
          </el-dropdown-item>
          <el-dropdown-item v-auth="['administrator']" @click.native="clear">
            清除缓存
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="logout">
            <span style="display:block;">退出</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div v-html="hooks['header_user_right_hook']" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getPlaid } from '../../utils/auth'
import { delCache, getTemplateHooks } from '../../api/common'

export default {
  name: 'DkUserInfo',
  props: {
    value: {
      default: '',
      type: String
    },
    userMenus: {
      default: () => {
        return []
      },
      type: Array
    }
  },
  data() {
    return {
      plaid: getPlaid(),
      loading: true,
      hooks: []
    }
  },
  computed: {
    ...mapGetters([
      'userinfo',
      'userGroup',
      'layout'
    ])
  },
  created() {
    // this.getTemplateHooks()
  },
  mounted() {
    this.getTemplateHooks()
  },
  methods: {
    onClickMenu(item) {
      this.$emit('onClick', item)
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    },
    clear() {
      delCache('cache').then(res => {
        this.$message.success(res.msg)
      })
    },
    getTemplateHooks() {
      var hooks = ['header_user_right_hook', 'header_user_left_hook']
      getTemplateHooks(hooks).then(res => {
        this.hooks = res.data
        this.loading = false
      })
    }
  }
}
</script>

<style scoped lang="scss">
.right-menu {
  float: right;
  height: 100%;
  display: flex;
  align-items: center;
  margin-right: 30px;

  &: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)
      }
    }
  }

  .dk-nav-avatar {
    //头像
    .avatar-container {

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

        .username {
          float: right;
          line-height: 40px;
        }

        .user-avatar {
          width: 32px;
          height: 32px;
          margin: 4px;
          border-radius: 100%;
        }

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

  .dk-nav-operation {
    //控件
    margin-right: 10px;

    li {
      float: left;
      margin: 0 10px;
    }
  }
}
</style>
