<template>
  <div class="user-wrapper">
    <div class="content-box">
      <a-dropdown>
        <span class="action ant-dropdown-link user-dropdown-menu">
          <a-icon type="user" style="font-size: 16px; color: #ffffff;" />
        </span>
        <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
          <a-menu-item key="0" style="margin: 5px 0 8px; font-weight: 700; background-color: #fff; cursor: default;">
            <span>您好！{{ userName }}用户</span>

          </a-menu-item>
          <a-menu-item key="3" @click="handleClear">
            <a-icon type="sync" />
            <span>清理缓存</span>
          </a-menu-item>
          <a-menu-divider/>
          <a-menu-item key="5">
            <a href="javascript:;" @click="handleLogout">
              <a-icon type="logout"/>
              <span>退出登录</span>
            </a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
    </div>
  </div>
</template>

<script>
// import { Tooltip } from 'ant-design-vue'
import { mapActions, mapGetters, mapState } from 'vuex'
export default {
  name: 'UserMenu',
  components: {
  },
  data () {
    return {
      searchMenuVisible: false,
      searchMenuOptions: [],
      searchMenuComp: 'span',
      userName: localStorage.getItem('userName'),
      keydown: '',
      arrKey: []
    }
  },
  props: {
    prefixCls: {
      type: String,
      default: 'ant-pro-global-header-index-action'
    }
  },
  computed: {
    ...mapGetters(['nickname', 'avatar']),
    ...mapState({
        // 后台菜单
        permissionMenuList: state => state.user.permissionList
    })
  },
  created () {
    const lists = []
    this.searchMenus(lists, this.permissionMenuList)
    this.searchMenuOptions = [...lists]
    document.addEventListener('keydown', this.handleKeyDown)
    document.addEventListener('keyup', this.handleKeyUp)
  },
  watch: {
      // update-begin author:sunjianlei date:20200219 for: 菜单搜索改为动态组件，在手机端呈现出弹出框
      device: {
        immediate: true,
        handler () {
          this.searchMenuVisible = false
          this.searchMenuComp = 'span'
        }
      }
      // update-end author:sunjianlei date:20200219 for: 菜单搜索改为动态组件，在手机端呈现出弹出框
  },
  methods: {
    ...mapActions(['Logout']),
    changeLang () {
      this.$i18n.locale = this.$i18n.locale === 'zh_CN' ? 'zh_CN' : 'en_US'
    },
    showClick () {
      this.searchMenuVisible = true
    },
    filterOption (input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    searchMenus (arr, menus) {
      for (const i of menus) {
        if (!i.hidden && i.component !== 'layouts/RouteView') {
          arr.push(i)
        }
        if (i.children && i.children.length > 0) {
          this.searchMenus(arr, i.children)
        }
      }
    },
    handleLogout () {
      this.$confirm({
        title: '提示',
        content: '真的要注销登录吗 ?',
        onOk: () => {
          return this.Logout({}).then(() => {
            setTimeout(() => {
              window.location.reload()
            }, 16)
          }).catch(err => {
            this.$message.error({
              title: '错误',
              description: err.message
            })
          })
        },
        onCancel () {
        }
      })
    },
    handleClear () {
      this.$confirm({
        title: '提示',
        content: '真的要清空缓存吗 ?',
        onOk: () => {
          for (var e in localStorage) {
            if (e.substr(0, 5) === 'batch' || e.substring(e.length - 7) === 'Service') {
              localStorage.removeItem(e)
            }
          }
        },
        onCancel () {
        }
      })
    },
    hiddenClick () {
      this.searchMenuVisible = false
    },
    searchMethods (value) {
      const route = this.searchMenuOptions.filter(item => item.id === value)[0]
      if (route.meta.internalOrExternal === true || route.component.includes('layouts/IframePageView')) {
        window.open(route.meta.url, '_blank')
      } else {
        this.$router.push({ path: route.path })
      }
      this.searchMenuVisible = false
    },
    handlePasswordChange () {
      this.$refs.passwordmodal.show(this.userName)
    }
  }
}
</script>
<style lang="less" scoped>
  /* update_begin author:zhaoxin date:20191129 for: 让搜索框颜色能随主题颜色变换*/
  /* update-begin author:sunjianlei date:20191220 for: 解决全局样式冲突问题 */
  .user-wrapper .search-input {
    width: 100px;
    /deep/ .ant-select-selection {
      background-color: inherit;
      border: 0;
      color: #fff;
      border-bottom: 1px solid white;
    }
    /deep/ .ant-select-search__field {
      color: #fff;
    }
    /deep/ .ant-select-selection-selected-value {
      color: #fff;
    }
  }
  /* update-end author:sunjianlei date:20191220 for: 解决全局样式冲突问题 */
  /* update_end author:zhaoxin date:20191129 for: 让搜索框颜色能随主题颜色变换*/
</style>
