<template>
  <transition name="msgbox-fade">
    <div class="pub-wrapper" v-if="visible">
      <div class="pub-mask"></div>
      <div class="pub-modal avater-modal">
        <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="userInfo.cover_photo" :src="userInfo.cover_photo" 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>更改头像</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">已绑定账户</div>
            <div class="account-item" v-for="(item, key) in userInfo.binds" :key="key">
              <div class="type">
                <strong>{{item.type.toUpperCase()}}</strong>
                <span class="open-tips" v-if="item.type == 'eth' || item.type == 'tron'">即将开放</span>
                <div class="fr" v-if="item.account">
                  <span v-if="item.is_visible === 0">已隐藏</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>
              </div>
              <p v-if="item.account" class="key">{{item.account}}</p>
              <el-button @click="bindAccount(item.type)" :disabled="item.type == 'eth' || item.type == 'tron'" v-else>绑定</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 API from '@/api'

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

export default {
  name: 'infoPopup',
  data () {
    return {
      isMe: true,
      newNickName: '', // 新用户名
      isEditName: false, // 编辑用户名
      newUserName: '', // 新@名
      isEditIntroduce: false // 编辑简介
    }
  },
  computed: {
    ...mapState({
      userInfo: state => {
        return state.userInfo
      }
    })
  },
  methods: {
    // 账号显示与隐藏
    hanldeChange (item, val) {
      let type = item.type
      API.user.setVisible({
        type,
        is_visible: val
      }).then(({ data }) => {

      })
    },
    bindAccount (type) {

    },
    // 编辑用户名昵称
    handleEditName () {
      if (this.newNickName === this.userInfo.nick_name) {
        this.isEditName = false
        return
      }
      if (this.newNickName.length > 36) {
        this.$showTips('用户呢称过长，请输入36个字符以内')
        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('updateMemberNickName', {
            userId: this.$store.state.userId,
            nickName: this.newNickName
          })
          this.$showTips('修改成功')
        })
      }
    },
    // 编辑用户名
    handleEditIntroduce () {
      if (this.newUserName === this.userInfo.user_name) {
        this.isEditIntroduce = false
        return
      }
      if (!/^[a-zA-Z_0-9]{5,36}$/i.test(this.newUserName)) {
        this.$showTips('只能是5-20位的数字，字母下，划线')
        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('updateMemberNickName', {
            userId: this.$store.state.userId,
            userName: this.newUserName
          })
          this.$showTips('修改成功')
        })
      }
    }
  }
}
</script>

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