// pages/activity/inviteFriends.js
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false,
    userInfo: {},
    invationData: {}, //当前邀请的信息
    namestr: "", //名字加*
    invitees: [], //加入活动的人
    datedifference: "", //活动到期时间
    isfirst: false, //是否首次
  },
  preventTouchMove: function () {

  },

  tank: function (e) {
    console.log("点击了");
    this.setData({
      showModalStatus: !this.data.showModalStatus
    })
  },
  toActivityHistory: function () {
    wx.navigateTo({
      url: '/pages/activity/activityhistory/activityHistory',
    })
  },
  getActivityOngoing() {
    let that = this;
    wx.showLoading({
      title: '加载中',
    });
    util.request(api.ActivityOngoing, {
      userId: that.data.userInfo.id
    }).then(function (res) {
      console.info(res.data);
      if (res.errno === 0) {
        that.setData({
          invationData: res.data.assistance,
          invitees: res.data.invitees,
        })
        if (Object.keys(res.data.assistance).length != 0) {
          that.setData({
            datedifference: util.dateDifference( res.data.assistance.endTime)
          })
          that.setData({
            isfirst: false
          })
        } else {
          that.setData({
            isfirst: true
          })
        }
      }
      wx.hideLoading();
    }).catch(function (err) {
      console.log(err);
      wx.hideLoading();
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if (!app.globalData.hasLogin) {
    //   wx.navigateTo({
    //     url: "/pages/auth/login/login"
    //   });
    // }; 

    let userInfo = wx.getStorageSync('userInfo');
    //console.info(userInfo);
    this.setData({
      userInfo: userInfo,
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

    let str = wx.getStorageSync('userInfo').nickName;
    if (null != str) {
      this.setData({
            namestr:str
          })
      // if (str.length > 2) {
      //   this.setData({
      //     namestr: str.split("")[0] + "**" + str.split("")[str.split.length + 1]
      //   })
      // } else {
      //   this.setData({
      //     namestr: str.split("")[0] + "**"
      //   })
      // }
    }
  },

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

  },

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

  },

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

  },

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

  },


  lingqu() {
    //领取60元大洋
    //实际系统后台自动发送
    if (this.data.invationData.isSendCoupon == 1) {
      this.myshowtost("你已领取本次活动现金券");
    } else {
      this.myshowtost("领取成功,10分钟后发放到你的账户");
    }
  },
  myshowtost: function (v) {
    wx.showToast({
      title: v,
      icon: 'none',
      duration: 3000
    })
  },
  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 (e) {
    let state = e.target.dataset.state;
    console.log(state);
    let that = this;

    that.requestActivityCreate();

    console.log("that.data.userInfo.id" + that.data.userInfo.id);
    return {
      title: "邀请好友领60元现金券",
      path: '/pages/index/index?rId=' + that.data.userInfo.id,
      // path: '/pages/index/index?rId=1',
      success: function (res) {
        // 转发成功

        that.getActivityOngoing();
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {

        that.getActivityOngoing();
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    };


  },

})