import {
  getFansList,
  getMyInfo,
  setFans
} from '~/api/user'
import event from '~/mixins/event'
import reachBottom from '~/mixins/reachBottom'
Page({
  behaviors: [reachBottom,event],
  data: {
    // 1我的关注;2我的粉丝
    currentType: 1,
    c1: 0,
    c2: 0,
    userInfo: {},
    localUid: wx.getStorageSync('uid')
  },
  async onShow() {
    this.resetData()
    let c1 = await getFansList({
      pageSize: 1,
      type: 1
    })
    let c2 = await getFansList({
      pageSize: 1,
      type: 2
    })
    let userInfo = await getMyInfo()

    this.setData({
      c1: c1.totalSize,
      c2: c2.totalSize,
      userInfo
    })
  },
  setType({
    currentTarget
  }) {
    this.setData({
      currentType: currentTarget.dataset.type
    })
    this.resetData()
  },
  // 获取分类的内容
  loadMore() {
    let type = this.data.currentType
    this.getData(getFansList, {
      pageSize: 20,
      type
    })
  },
  jumpUserInfo({
    currentTarget
  }) {
    let uid = currentTarget.dataset.uid
    wx.navigateTo({
      url: `/pages/personal/index?uid=${uid}`,
    })
  },
  async setFans({
    currentTarget
  }) {
    if (this.data.currentType == '1') {
      this.jumpUserInfo({
        currentTarget
      })
    } else {
      if (currentTarget.dataset.iseachother) {
        this.jumpUserInfo({
          currentTarget
        })
      } else {
        await setFans({
          uid: currentTarget.dataset.uid
        })
        let listCopy = JSON.parse(JSON.stringify(this.data.list))
        listCopy.forEach(item => {
          if (item.user.uid == currentTarget.dataset.uid) {
            item.isEachOther = true
          }
        })
        this.setData({
          list: listCopy
        })
        wx.showToast({
          title: '已关注',
          icon: 'none'
        })
      }
    }
  },
  searchFriend() {
    wx.navigateTo({
      url: '/pages/searchFriend/index',
    })
  },
  clipboar({
    currentTarget
  }) {
    wx.setClipboardData({
      data: currentTarget.dataset.eid,
      success: function (res) { //成功回调函数
        wx.showToast({
          title: '已复制',
          icon: "none"
        })
      }
    })
  },
  sendMsg({
    currentTarget
  }) {
    let user = currentTarget.dataset.user
    let {
      nickName,
      eid,
      uid
    } = user
    if (this.data.localUid == uid) {
      return wx.showToast({
        title: '不可以给自己发私信哦~',
        icon: 'none'
      })
    }
    wx.navigateTo({
      url: `/pages/chat/index?title=${nickName||eid}&uid=${uid}`,
    })
  },
  onReachBottom() {
    this.loadMore()
  },
})