// pages/luckDraw/index.js
import LuckDraw from '../../api/luck-draw'
import { parseTime } from '../../utils/util';
import Common from './common'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: 1,
        pageSize: 10,
        lock: false,
        noResult: false,
        noMore: false,
        activityList: []
    },

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

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

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.data.activityList = [];
        this.getActivityList();
        this.startCountdownTimer()
    },

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

    },

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

    },

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

    },

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

    },

    getActivityList: function() {
        LuckDraw.getActivityList({
            page: this.data.page,
            pageSize: this.data.pageSize
        }).then(res => {
            if (res.code == 200) {
                this.drawListView(res.data)
            }
            this.data.lock = false
        }).catch(_ => {
            this.data.lock = false
        })
    },

    drawListView: function(data) {
        if (!Array.isArray(data) || data.length == 0) {
            console.log("没有获取到数据");
            if (this.data.page == 1) {
                this.setData({
                    noResult: true
                })
            } else {
                this.setData({
                    noMore: true
                })
            }
            return
        }
        data.forEach(v => {
            let beginTime = v.beginTime
            let endTime = v.endTime
            if (v.timeType == 1 || beginTime == null) {
                beginTime = ""
            }
            if (v.timeType == 1 || endTime == null) {
                endTime = ""
            }
            beginTime && (v._beginTime = parseTime(beginTime, "{y}.{m}.{d}"))
            endTime && (v._endTime = parseTime(endTime, "{y}.{m}.{d}"))

            if (v.activityTitle && v.activityTitle.length > 20) {
                v.activityTitle = v.activityTitle.substr(0, 20) + "..."
            }
        })
        this.data.activityList = this.data.activityList.concat(...data)
        this.setData({
            activityList: this.data.activityList
        })
        this.calcActivityCountdown()
    },

    

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.lock || this.data.noMore) {
            return
        }
        this.data.lock = true
        this.data.page++
        this.getActivityList()
    },

    toDetail(e) {
        const url = "detail?id=" + e.currentTarget.dataset.id
        wx.navigateTo({
          url,
        })
    },

    startCountdownTimer() {
        setInterval(this.calcActivityCountdown, 1000)
    },

    calcActivityCountdown() {
        // 当前时间毫秒数
        if (this.data.activityList == null || this.data.activityList.length == 0) {
            return
        }
        const now = new Date();
        
        this.data.activityList.forEach(v => {
            this.setActivityCountdownTime(now, v)
        })
        this.setData({
            activityList: this.data.activityList
        })
    }
})