// nova-werun/pages/activity/index.js
const Parse = getApp().Parse;
const company = getApp().globalData.company;
const uid = Parse.User.current()?.id
const dateF = require("../../../utils/date")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activtList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getActivity()
  },

   /**判断用户参与活动状态 */
   async getActStatus(activity, query, count) {
    let now = new Date()
    if (now > activity.get('endDate')) {
      return {
        title: '已结束',
        isCanSign: false
      }
    }
    if (!activity.get('isEnabled')) {
      return {
        title: '未开启',
        isCanSign: false
      }
    }
    if (activity.get('signFromTo')?.fromDate && now < activity.get('signFromTo').fromDate) {
      return {
        title: '未到报名时间',
        isCanSign: false
      }
    }
    if (activity.get('signFromTo')?.toDate && now >= activity.get('signFromTo').toDate) {
      return {
        title: '报名时间已过',
        isCanSign: false,
      }
    }
    query.equalTo('user', uid)
    let aRegister = await query.first()
    if (aRegister?.id) {
      return {
        title: '已报名',
        isCanSign: false,
        isCanRevise: true
      }
    }
    if (activity.get('merber') >= 0 && count >= activity.get('merber')) {
      return {
        title: '人数已满',
        isCanSign: false
      }
    }
    return {
      title: '可报名',
      isCanSign: true
    }
  },
  /** 获取前五个活动*/
  async getActivity() {
    let {
      activtList,
      count
    } = this.data
    if (count && activtList.length >= count) {
      wx.showToast({
        title: '已加载全部',
        icon: 'none',
        duration: 500
      })
      return
    }
    let query = new Parse.Query('Activity')
    query.equalTo('company', company)
    query.notEqualTo('isDeleted', true)
    query.notEqualTo('isHidden', true)
    query.ascending('index')
    count = await query.count()
    query.limit(20)
    query.skip(activtList?.length)
    let list = await query.find()
    let aList = []
    if (Array.isArray(list)) {
      for (let i in list) {
        let query = new Parse.Query('ActivityRegister')
        query.notEqualTo('isDeleted', true)
        query.equalTo('activity', list[i].id)
        query.equalTo('isPay', true)
        query.equalTo('isChecked', true)
        let count = await query.count()
        let tip = await this.getActStatus(list[i], query, count)
        let obj = list[i].toJSON()
        obj.tip = tip
        obj['resgisterCount'] = count
        obj.startDate = dateF.formatTime("YYYY.mm.dd HH:MM", obj.startDate?.iso)
        obj.endDate = dateF.formatTime("YYYY.mm.dd HH:MM", obj.endDate?.iso)
        if (obj.signFromTo?.from && obj.signFromTo?.to) {
          obj.signFromTo.from = dateF.formatTime("YYYY.mm.dd HH:MM", obj.signFromTo?.from?.iso)
          obj.signFromTo.to = dateF.formatTime("mm.dd HH:MM", obj.signFromTo?.to?.iso)
        }
        activtList.push(obj)
      }
    }
    this.setData({
      activtList,
      count
    })
  },


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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
   
  gourl(e) {
    const url = e.currentTarget.dataset.url
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `${url}?id=` + id // 目标页面的路径
    });
  },
})