<template>
  <div class="avatar">
    <div class="avatar-l" @click="goProfile">
      <img :src="data.avatarUrl" alt="">
      <span>{{ data.nickName }}</span>
    </div>
    <div class="avatar-r">
      <span class="follow" v-show="!isFollowed" @click="follow">关注</span>
      <span class="follow-cancel" v-show="isFollowed" @click="cancelFollow">已关注</span>
    </div>
  </div>
</template>

<script>
import { baseUrl } from '../constant.js';
export default {
  props: {
    data: Object
  },
  data() {
    return {
      isFollowed: false
    }
  },
  mounted() {
    this.isFollowed = this.data.isFollowed;
  },
  methods: {
    goProfile() {
      location.href = this.data.profileUrl;
    },

    follow() {
      this.$http.post(`${baseUrl}/api/feed/concern`, {
        mid: this.data.mid,
        uid: this.data.uid
      }).then(res => {
        if (res.data.code === 0) {
          this.isFollowed = true;
        }
      });
    },

    cancelFollow() {
      this.$http.post(`${baseUrl}/api/feed/des-concern`, {
        mid: this.data.mid,
        uid: this.data.uid
      }).then(res => {
        if (res.data.code === 0) {
          this.isFollowed = false;
        }
      });
    }
  },
}
</script>

<style lang="scss" scoped>
  .avatar{
    display: flex;
    justify-content: space-between;
    line-height: 0.4rem;
    padding: 0 0.14rem;
    padding-bottom: 0.14rem;
    &-l{
      img{
        width: 0.32rem;
        height: 0.32rem;
        border: 1px solid rgba(0,0,0,0.05);
        border-radius: 50%;
        vertical-align: middle;
      }
      span{
        display: inline-block;
        max-width: 1.32rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 0.22rem;
        color: #999999;
        vertical-align: middle;
        margin-left: 0.1rem;
      }
    }
    &-r{
      text-align: right;
      span{
        display: inline-block;
        height: 0.4rem;
        line-height: 0.4rem;
        background: #FFFFFF;
        border-radius: 42px;
        font-size: 0.2rem;
        text-align: center;
        &::before{
          content: '';
          display: inline-block;
          width: 0.2rem;
          height: 0.2rem;
          margin-right: 2px;
        }
        &.follow{
          width: 0.94rem;
          color: #FF8200;
          border: 1px solid #FF8200;
          &::before{
            background: url('../imgs/icon_follow.png') no-repeat;
            background-size: 100% 100%;
            background-position: center center;
          }
        }
        &.follow-cancel{
          width: 1.14rem;
          color: #636363;
          border: 1px solid #636363;
          &::before{
            background: url('../imgs/icon_followed.png') no-repeat;
            background-size: 0.2rem 0.2rem;
            background-position: center center;
          }
        }
      }
    }
  }
</style>