// pages/activity/activityhistory/activityHistory.js
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabState: 1,
    myPost: [], //我发起的数据
    myJoin: [], //我参与的数据
    myPostQuery: {

    },
    myJoinQuery: {

    }
  },
  chooseTab: function (e) {
    console.log(e.currentTarget.dataset.state);
    this.setData({
      tabState: e.currentTarget.dataset.state
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    //console.info(userInfo);
    this.setData({
      userInfo: userInfo,
    });

    this.wofaqide(); //我发起的
    this.wochanyude(); //我参与的
  },
  wofaqide() {
    //我发起的
    let that = this;
    util.request(api.ActivityList, {
      userId: that.data.userInfo.id
    }).then(function (res) {
      console.info(res.data);
      if (res.errno === 0) {
        res.data.forEach(function (item, index) {
          item.shichang = util.dateDifference(item.startTime, item.endTime);
        })
        that.setData({
          myPost: res.data
        })
      }
    })
  },
  wochanyude() {
    let that = this;
    util.request(api.ActivityParticipate, {
      userId: that.data.userInfo.id
    }).then(function (res) {
      console.info(res.data);
      if (res.errno === 0) {
        res.data.forEach(function (item, index) {
          item.shichang = util.dateDifference(item.startTime, item.endTime);
        })
        that.setData({
          myJoin: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  async requestActivityCreate() {
    let tt = await util.request(api.ActivityCreate, {
      userId: this.data.userInfo.id
    }).then(function (res) {
      console.log(res.data);
    }).catch(function (err) {
      console.log(err);
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    console.log("进入邀请了");
    that.requestActivityCreate();
    return {
      title: "邀请好友领60元现金券",
      path: '/pages/index/index?rId=' + that.data.userInfo.id,
      // path: '/pages/index/index?rId=1',
      success: function (res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    };
  },
  goCouponList() {
    wx.navigateTo({
      url: '/pages/ucenter/couponList/couponList',
    })
  }
})