<template>
  <transition name="msgbox-fade">
    <div class="pub-wrapper" v-if="visible">
      <div class="pub-mask" @click="visible = false"></div>
      <div class="pub-modal avater-modal" v-loading="isLoading">
        <div class="modal-hd">
          <i class="el-icon-close" @click="visible = false"></i>
        </div>
        <div class="modal-bd" v-if="userInfo">
          <div class="user-top">
            <div class="user-avatar" @click="$editUserAvatar(userInfo.cover_photo)">
              <img v-if="avatarUrl" :src="avatarUrl" alt="">
              <div v-else class="user-avatar"
              :class="'avatar_bg' + userInfo.user_id % 9"
              :data-name="userInfo.nick_name.slice(0,2).toUpperCase()"
              ></div>
              <p>{{$t('userinfo.changePhoto')}}</p>
            </div>
            <div class="r-info">
              <span class="name" v-if="!isEditName" @click="isEditName = true;newNickName = userInfo.nick_name">{{userInfo.nick_name}} <i class="el-icon-edit"></i></span>
              <input class="edit-name-input" type="text" v-else
                v-model="newNickName"
                v-focus
                @blur="handleEditName">
              <div class="introduce" v-if="!isEditIntroduce" @click="isEditIntroduce = true;newUserName = userInfo.user_name">@{{userInfo.user_name}} <i class="el-icon-edit"></i></div>
              <input class="edit-name-input" type="text" v-else
                v-model="newUserName"
                v-focus
                @blur="handleEditIntroduce">
            </div>
          </div>
          <div class="account-wrap">
            <div class="title">{{$t('userinfo.bindAccounts')}}</div>
            <div class="account-item" v-for="(item, key) in userInfo.binds" :key="key">
              <div class="type">
                <strong>{{item.type.toUpperCase()}}</strong>
                <div class="fr" v-if="item.account">
                  <span>{{item.is_visible === 0?$t('userinfo.private'):$t('userinfo.public')}}</span>
                  <el-switch
                    v-model="item.is_visible"
                    :active-value="0"
                    :inactive-value="1"
                    @change="hanldeChange(item, $event)"
                    active-color="#2298f0"
                    inactive-color="#cbcbcb">
                  </el-switch>
                  <div class="btn-unbind" @click.stop="unbindAccount(item.type)">{{$t('userinfo.unbind')}}</div>
                </div>
              </div>
              <p v-if="item.account" class="key">{{item.account}}</p>
              <el-button @click="bindAccount(item.type)" v-else>{{$t('userinfo.bind')}}</el-button>
            </div>
          </div>
          <!-- <button class="send-msg-btn">发消息</button> -->
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { Button, Message, Switch } from 'element-ui'
import Vue from 'vue'
import { mapState } from 'vuex'
import { bindAccountMixins } from '@/mixins/user'
import { mobileInputBlur } from '@/util/util'
import API from '@/api'

Vue.component(Button.name, Button)
Vue.component(Message.name, Message)
Vue.component(Switch.name, Switch)

export default {
  name: 'infoPopup',
  mixins: [bindAccountMixins],
  data () {
    return {
      isMe: true,
      newNickName: '', // 新用户名
      isEditName: false, // 编辑用户名
      newUserName: '', // 新@名
      isEditIntroduce: false // 编辑简介
    }
  },
  computed: {
    ...mapState({
      scatter: state => state.scatter,
      userInfo: state => state.userInfo
    }),
    avatarUrl () {
      if (/^http/.test(this.userInfo.cover_photo)) return `${this.userInfo.cover_photo}?imageview/0/w/180`
      else return this.userInfo.cover_photo
    }
  },
  methods: {
    // 账号显示与隐藏
    hanldeChange (item, val) {
      let type = item.type
      API.user.setVisible({
        type,
        is_visible: val
      }).then(({ data }) => {

      })
    },
    bindAccount (type) {
      switch (type) {
        case 'eos':
          this.bindEos('eos')
          break
        case 'tg':
          this.bindTg()
          break
        case 'eth':
          this.bindEth()
          break
        case 'tron':
          this.bindTron()
          break
        case 'meetone':
          this.bindEos('meetone')
          break
      }
    },
    // 编辑用户名昵称
    handleEditName () {
      mobileInputBlur()
      if (this.newNickName === this.userInfo.nick_name) {
        this.isEditName = false
        return
      }
      if (this.newNickName.length > 16) {
        this.$showTips(this.$t('userinfo.nickTooLong'))
        return
      }

      this.isEditName = false
      if (this.newNickName.length) {
        API.user.changeNickName({
          nick_name: this.newNickName
        }).then(({ data }) => {
          this.$store.commit('setUserNickName', this.newNickName)
          this.$store.commit('updateMemberInfo', {
            userId: this.$store.state.userId,
            nickName: this.newNickName
          })
          this.$showTips(this.$t('public.updateSucc'))
        })
      }
    },
    // 编辑用户名
    handleEditIntroduce () {
      mobileInputBlur()
      if (this.newUserName === this.userInfo.user_name) {
        this.isEditIntroduce = false
        return
      }
      if (!/^[a-zA-Z_0-9]{5,20}$/i.test(this.newUserName)) {
        this.$showTips(this.$t('userinfo.wrongPattern'))
        return
      }

      this.isEditIntroduce = false
      if (this.newUserName.length) {
        API.user.changeUserName({
          user_name: this.newUserName
        }).then(({ data }) => {
          this.$store.commit('setUserUserName', this.newUserName)
          this.$store.commit('updateMemberInfo', {
            userId: this.$store.state.userId,
            userName: this.newUserName
          })
          this.$showTips(this.$t('public.updateSucc'))
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import "./style.scss";
</style>
