// pages/dateLsit/dateLsit.js
const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
const Http = require("../../utils/HttpBasics");
const config = require("../../config/config");
const util = require("../../utils/util");
let app = getApp();
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    navigationBarHeight,
    activityStyle: '', //选中当前日历样式
    pageNum: 1,
    pageSize: 6,
    list: [],
    childUrl: config.api.activityDey,
    startDate:'',
    endDate:'',
    mouldType:0,
    mouldConfig:{},
  },
  goActivity(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/radetail/index?id=${id}`,
    })
  },
  getDays(year, month) { //获取每月有多少天
    let tempDateArr = []
    let thenDate = new Date(year, month, 0).getDate()
    return thenDate
  },
  getDate(e) {
    let startDate=""
    let endDate=""
    let {
      y,
      m,
      value
    } = e.detail
    let tempDey = this.getDays(y, m) //获取每月有多少天
    if (m < 10) {
      m = "0" + (m + "")
    }
    this.setData({
      pageNum: 1,
      list: []
    })
    if (value){
      if(value<10){
        value="0"+(value+"")
      }
       startDate = y + "-" + m + "-" + value + " 00:00:00"
       endDate = y + "-" + m + "-" + value + " 23:59:59"
    }else{
       startDate = y + "-" + m + "-" + "01" + " 00:00:00"
       endDate = y + "-" + m + "-" + tempDey + " 23:59:59"
    }
    
    this.setData({
      startDate: startDate,
      endDate: endDate
    })
    this.setActivityCalendar(this.data.startDate, this.data.endDate)
  },
  setActivityCalendar(startDate = '', endDate = '') { //活动列表
    Http.get({
      url: config.api.activityCalendar,
      data: {
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
        startDate: startDate,
        endDate: endDate
      }
    }).then(res => {
      if (res.code == 200) {
        //util.formatTime(res.data.validStartDate, "yyyy-MM-dd"),
        if (res.data.list.length == 0) {
          return
        } else {
          let tempArr = this.data.list
          tempArr.push(...res.data.list)
          tempArr.map(item => {
            item.activityEndTime = util.formatTime(item.activityEndTime, "yyyy-MM-dd hh:mm")
            item.activityStartTime = util.formatTime(item.activityStartTime, "yyyy-MM-dd hh:mm")
            item.startTime = util.formatTime(item.startTime, "yyyy-MM-dd hh:mm")
            item.endTime = util.formatTime(item.endTime, "yyyy-MM-dd hh:mm")
          })
          this.setData({
            list: tempArr,
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      mouldType: app.globalData.mouldType,
      mouldConfig: app.globalData.mouldConfig,
    })
    this.setActivityCalendar(this.data.startDate, this.data.endDate)
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.setData({
      pageNum: this.data.pageNum+1
    })
    this.setActivityCalendar(this.data.startDate, this.data.endDate)
  },

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

  // }
})