import {
  getUserInfo,
  setFans,
  getUserRead
} from '~/api/user'
import share from '~/mixins/share'
import event from '~/mixins/event'
import reachBottom from '~/mixins/reachBottom'
Page({
  behaviors: [reachBottom, share,event],
  /**
   * 页面的初始数据
   */
  data: {
    uid: '',
    userInfo: {},
    // type为pk,顶部显示为pk时样式,user为默认样式
    type: 'user',
    uid: '',
    localUid: wx.getStorageSync('uid')
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (wx.getStorageSync('uid') == options.uid) {
      wx.setNavigationBarTitle({
        title: '我的主页'
      })
    }
    this.setData({
      type: options.type || 'user',
      uid: options.uid
    })
    this.getUserInfo()
  },
 /*  onShow() {
    this.getUserInfo()
  }, */
  async getUserInfo() {
    let res = await getUserInfo({
      uid: this.data.uid
    })
    this.setData({
      userInfo: res
    })
    this.resetData()
  },
  loadMore() {
    this.getData(getUserRead, {
      uid: this.data.userInfo.user.uid,
      pageSize: 20
    })
  },
  // 关注
  async setFans() {
    if (wx.getStorageSync('uid') == this.data.uid) {
      return wx.showToast({
        title: '不可以关注自己哦~',
        icon: 'none'
      })
    }
    let newLike = !this.data.userInfo.like
    let res = await setFans({
      uid: this.data.userInfo.user.uid,
    }, 'put')
    this.setData({
      ['userInfo.like']: newLike
    })
    wx.showToast({
      title: newLike ? '已关注' : '取消关注',
      icon: 'none'
    })
  },
  clipboar() {
    wx.setClipboardData({
      data: this.data.userInfo.user.eid,
      success: function (res) { //成功回调函数
        wx.showToast({
          title: '已复制',
          icon: "none"
        })
      }
    })
  },
  toPkPage({
    currentTarget
  }) {
    if (this.data.userInfo.user.profession == '官方' || wx.getStorageSync('uid') == this.data.uid) {
      wx.navigateTo({
        url: `/pages/reading/index?videoId=${currentTarget.dataset.id}`
      })
    } else {
      wx.navigateTo({
        url: `/pages/pkPage/index?videoId=${currentTarget.dataset.id}`
      })
    }
  },
  sendMsg({
    currentTarget
  }) {
    let user = this.data.userInfo.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}`,
    })
  },
})