// subPackage/page/pages/myInvite/myInvite.js
const app = getApp()
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
const { memberId } = wx.getStorageSync('_baseUserInfo')
import { Integer } from '../../../../utils/integerDigitalConvertion'

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    currentInviteNum:'', //当前邀请人数
    yesterdayInviteNum:'', //昨日邀请新增
    pageNo: 1,
    pageSize: 50,
    inviteMemberList: [],
    inviteBanner: '',
    isGoToInvite: false
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInviteNumber()
    this.getYesterdayInviteNumber()
    this.getInviteMemberList()
    this.getInviteBanner()
    wx.hideShareMenu()
  },

  // 获取当前邀请人数
  getInviteNumber(){
    wxService.post(`/member/share/inviteFriends/buyer/count`,{
      userId: memberId,
      tentacleType: 3
    }).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          currentInviteNum: data
        })
      }
    }).finally(() => {
    })
  },

  // 获取昨日邀请人数
  getYesterdayInviteNumber() {
    // 昨天的时间
    var day1 = new Date();
    day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
    var yesterday = day1.getFullYear() + "-" + (day1.getMonth() + 1) + "-" + day1.getDate() +' ' +  '00:00:00';

    //今天的时间
    var day2 = new Date();
    day2.setTime(day2.getTime());
    var today = day2.getFullYear() + "-" + (day2.getMonth() + 1) + "-" + day2.getDate() + ' ' + '00:00:00';

    let params = {
      userId: memberId,
      activateEndTime: today,
      activateStartTime: yesterday,
      tentacleType: 3
    }
    wxService.post(`/member/share/inviteFriends/buyer/count`, params).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          yesterdayInviteNum: data
        })
      }
    }).finally(() => {
    })
  },

  // 邀请会员列表
  getInviteMemberList(){
    let params = {
      userId: memberId,
      tentacleType: 3
    }
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wxService.post(`/member/share/inviteFriends/buyer/findPage?pageNo=${this.data.pageNo}&pageSize=${this.data.pageSize}`, params).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          this.setData({
            inviteMemberList: data
          }, () => {
          })
        }
      }
    })
  },

  // 获取邀请页面banner
  getInviteBanner(){
    wxService.get(`/member/share/inviteFriends/buyer/getValid`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          this.setData({
            inviteBanner: data.gradientAwardPage
          }, () => {
          })
          let now = new Date().getTime() 
          let endTime = new Date(data.endTime).getTime() 
          if (now < endTime){
            this.setData({
              isGoToInvite: true
            })
          }
        }
      }
    })
  },

  // 点击去分享页面
  gotoInviteShare(){
    if (this.data.isGoToInvite){
      wxService.router('/subPackage/page/pages/share/share')
    }
  },

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

  },

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

  },

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

  },


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

  }
})