<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">
              <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>
            </div>
            <div class="r-info">
              <span class="name">{{userInfo.nick_name}}</span>
              <div class="introduce">@{{userInfo.user_id}}</div>
            </div>
          </div>
          <div class="account-wrap min-height-account">
            <div class="title">已绑定账户</div>
            <template v-if="accountList">
              <div class="account-item" v-for="(item, key) in accountList" :key="key">
                <div class="type">
                  <strong>{{item.type.toUpperCase()}}</strong>
                </div>
                <p v-if="item.account && item.is_visible" class="key">{{item.account}}</p>
                <p v-else>未绑定或已隐藏</p>
              </div>
            </template>
          </div>
          <button v-if="$store.state.userId != userId" class="send-msg-btn" @click="sendMsg">发消息</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 {
      accountList: null,
      userInfo: null
    }
  },
  computed: {
    ...mapState({
      members: state => state.group.members,
      groupId: state => state.group.groupId,
      sessionList: state => state.chat.sessionList,
      meId: state => state.userId,
      meInfo: state => state.userInfo
    })
  },
  methods: {
    sendMsg () {
      let youId = this.userInfo.user_id
      let sessionId = Number(youId) < Number(this.meId)
        ? `${youId}-${this.meId}`
        : `${this.meId}-${youId}`

      let repeatFlag = this.sessionList.some(e => {
        return e.session_id == sessionId
      })

      if (!repeatFlag) {
        let obj = {
          cover_photo: this.userInfo.cover_photo,
          is_group: '0',
          name: this.userInfo.nick_name,
          // read_hash:null
          session_id: sessionId
        }
        this.$store.commit('addSessionItem', obj)
      }

      // 创建members,应对还没聊天记录的情况
      let myId = this.meInfo.user_id
      let members = {}
      members[myId] = {
        cover_photo: this.meInfo.cover_photo,
        nick_name: this.meInfo.nick_name
      }
      members[this.userInfo.user_id] = {
        cover_photo: this.userInfo.cover_photo,
        nick_name: this.userInfo.nick_name
      }

      this.$store.commit('updateGroup', {
        key: 'members',
        data: members
      })
      this.$store.commit('updateGroup', {
        key: 'privateName',
        data: this.userInfo.nick_name
      })
      this.$router.push({ path: `/pm/${sessionId}` })

      this.visible = false
    }
  },
  created () {
    this.userInfo = this.members[this.userId]
    API.user.getOtherInfo({
      target_id: this.userId,
      group_id: this.groupId || null
    }).then(({ data }) => {
      this.accountList = data.data.binds
    })
  }
}
</script>

<style lang="scss">
@import "./style.scss";
.min-height-account{
  min-height: 218px;
}
</style>
