<template>
  <div class="user-info">
    <template v-if="userAvatar && userNickName">
      <img class="avatar" :src="userAvatar" alt="" />
      <div class="nickName">{{userNickName}}</div>
      <i class="iconfont icon-show_drop-down"></i>
      <ul class="menu">
        <li><span @click="onClickPersonalCenter">{{personal_center}}</span></li>
        <li><span class="warn" @click="onClickLogout" >{{logout}}</span></li>
      </ul>
    </template>
    <template v-else>
      <div class="loginAndRegis">
        <span @click="onClickLogin">{{login}}</span>
        |
        <span @click="onClickRegister">{{register}}</span>
      </div>
    </template>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {i18n} from "@/lang"

export default {
  data() {
    return {
      personal_center: i18n.t("personal_center.personal_center"),
      logout: i18n.t("personal_center.logout"),
      login: i18n.t("personal_center.login"),
      register: i18n.t("personal_center.register"),

    }
  },
  computed: {
    ...mapGetters([
      'userAvatar',
      'userNickName'
    ]),
  },
  methods: {
    onClickLogin(){
      window.location.href = '/#/login/login?from=%2F'
    },
    onClickRegister(){
      window.location.href = '/#/login/register?from=%2F'
    },
    onClickPersonalCenter() {
      window.location.href = '/#/information'
    },
    onClickLogout() {
      localStorage.setItem('token', '')
      localStorage.setItem('info', '')
      window.location.href = '/#/'
    }
  },
  mounted() {
  }
}

</script>

<style lang="less" scoped>
.user-info {
  display: flex;
  align-items: center;
  position: relative;
  height: 80%;
  .avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.05);
    margin-right: 8px;
    object-fit: cover;
  }
  .nickName {
    color: #323233;
    font-size: 14px;
    margin-right: 5px;
  }
  .icon-show_drop-down {
    color: #323233;
    font-size: 12px;
    transform: scale(0.583) translateX(-35%);
  }
  &:hover {
    .menu {
      display: block;
    }
  }
  .menu {
    display: none;
    position: absolute;
    padding: 0 16px;
    background: #fff;
    box-shadow: 0px 2px 12px 0px rgba(50, 50, 51, 0.12);
    top: 50px;
    right: -10px;
    &:hover{
      display: block;
    }
    // 菜单的箭头
    &::before {
      border: 7px solid transparent;
      border-bottom: 7px solid #fff;
      width: 0;
      height: 0px;
      content: "";
      display: inline-block;
      position: absolute;
      top: -14px;
      right: 12px;
    }
    li {
      display: block;
      padding: 11px 20px;
      border-bottom: solid 1px #EBEDF0;
      &:last-child {
        border-bottom: none;
      }
      span {
        font-size: 16px;
        color: #323233;
        cursor: pointer;
        word-break: keep-all;
        &.warn {
          color: #FA5555;
        }  
      }
    }
  }
  .loginAndRegis {
    font-size: 14px;
    color: #323233;
    span {
      cursor: pointer;
      padding: 0 5px;
    }
  }
}
</style>