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 == 0) {
      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]:1,
              member:this.data.member
            })
            console.log(this.data.member)
            this.getMyheartNowTimes();
          }
        }
      })
    } else {
      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]:0,
              member:this.data.member
            })
            console.log(this.data.member)
            this.getMyheartNowTimes();
          }
        }
      })
    }
  },
  // 获取我的心动次数
  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, '我获取人员名单')
          for (let i = 0; i < res.data.members.length; i++) {
            if (res.data.members[i].openId == this.data.openid) {
              this.setData({
                myNum: res.data.members[i].activeNumber
              })
              // 删除自己
             res.data.members.splice(i,1);
            }
          }
          this.setData({
            member: res.data.members
          })
        }
      }
    })
  },
  async onLoad(options) {
   
    tool.openidStatus().then(result => {
      this.setData({
        openid: result[0],
        sessionkey: result[1],
        activeId: options.id
      })
      this.getActivies();
      this.getMyheartNowTimes();
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

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

  }
})