const app = require('../../utils/util.js');
const tool = require('../../utils/tool.js');
Page({
  data: {
    checkArr: [],
    member: [],
    planHeart: 0,
    heartTime: 0,
    myNum: 0
  },
  goTousu() {
    wx.switchTab({
      url: '/pages/service/service',
    })
  },
  love(e) {
    console.log(e)
    let status = this.data.member[e.currentTarget.dataset.index].hearted
    let status1 = 'member[' + e.currentTarget.dataset.index + '].hearted'
    console.log(status)
    if (status == 1) {
      wx.request({
        url: app.globalData.publicUrl + '/wx/actives/' + this.data.openid + '/' + this.data.activeId + '/unfollow',
        method: "get",
        data: {
          targetId: e.currentTarget.dataset.openid
        },
        success: (res) => {
          if (res.data.code == 0) {
            console.log(res, '我取消心动了')
            this.setData({
              [status1]: 0,
              member: this.data.member
            })
            console.log(this.data.member)
            this.getMyheartNowTimes();
          }
        }
      })
    } else {
      if (this.data.heartTime < this.data.planHeart) {
        wx.request({
          url: app.globalData.publicUrl + '/wx/actives/' + this.data.openid + '/' + this.data.activeId + '/heartbeat',
          method: "get",
          data: {
            targetId: e.currentTarget.dataset.openid
          },
          success: (res) => {
            if (res.data.code == 0) {
              console.log(res, '我心动了')
              this.setData({
                [status1]: 1,
                member: this.data.member
              })
              console.log(this.data.member)
              this.getMyheartNowTimes();
            }
          }
        })
      } else {
        wx.showToast({
          title: '您已经达到心动上限了呦',
          icon: 'none',
          duration: 3000,
        })
      }
    }
  },
  // 获取我的心动次数
  getMyheartNowTimes() {
    wx.request({
      url: app.globalData.publicUrl + '/wx/actives/' + this.data.openid + '/' + this.data.activeId + '/myHearts',
      method: "get",
      success: (res) => {
        console.log(res, '查询目前心动次数')
        if (res.data.code == 0) {
          this.setData({
            heartTime: res.data.heartCount
          })
        }
      }
    })
  },
  getActivies() {
    wx.request({
      url: app.globalData.publicUrl + '/wx/actives/' + this.data.openid + '/' + this.data.activeId + '/detail',
      method: "get",
      success: (res) => {
        console.log(res, '查询活动详情获取心动上限次数')
        if (res.data.code == 0) {
          this.setData({
            planHeart: Number(res.data.activeRoll.planHeart)
          })
        }
      }
    })
    wx.request({
      url: app.globalData.publicUrl + '/wx/actives/' + this.data.openid + '/' + this.data.activeId + '/members',
      method: "get",
      success: (res) => {
        if (res.data.code == 0) {
          console.log(res.data.members, '我获取人员名单')
          this.setData({
            member: res.data.members
          })
        }
      }
    })
    wx.request({
      // this.data.activeId
      url: app.globalData.publicUrl + '/wx/actives/' + this.data.openid + '/' + this.data.activeId + '/sign',
      method: "get",
      success: (res) => {
        console.log(res,'我签到的结果')
        if (res.data.code == 0) {
          this.setData({
            myNum: res.data.activeNumber
          })
        }
      }
    })
  },
  async onLoad(options) {
    console.log(options)
    tool.openidStatus().then(result => {
      this.setData({
        openid: result[0],
        sessionkey: result[1],
        activeId: options.id
      })
      this.getActivies();
      this.getMyheartNowTimes();
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.realTime = setInterval(()=>
    {
      this.getActivies();
      this.getMyheartNowTimes();
    }, 10000)//间隔时间
    // 更新数据
    this.setData({
      realTime:this.data.realTime,//实时数据对象(用于关闭实时刷新方法)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.realTime)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})