const app = getApp()

import {
    getCustomerTicketRq
} from "../../../../api/meeting/meetingRoom.js"

import {
    selfFormatTimeYMD
} from "../../../../utils/util.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        IMG_NAME: app.IMG_NAME,
        couponId: null,
        dataList: [],
        userDetail: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        console.log('onLoad options', typeof options.couponId);
        _this.setData({
            type: options.type,
            couponId: parseInt(options.couponId),
            userDetail: wx.getStorageSync('user')
        })
        // 获取优惠卷
        _this.getDataList()
    },

    // 获取优惠卷
    getDataList() {
        let _this = this;
        getCustomerTicketRq({
            "userId": _this.data.userDetail.id,
            "customerId": _this.data.userDetail.icsCustomerId,
            "type": _this.data.type
        }).then(res => {
            let nowTime = new Date().getTime();
            let dataList = res.data.map(item => {
                item.startTime = selfFormatTimeYMD(item.startTime)
                item.endDate = selfFormatTimeYMD(item.endDate)
                let kaishiTime = new Date(item.startTime + " 00:00:00")
                let jieshuTime = new Date(item.endDate + " 23:59:59")
                if (nowTime >= kaishiTime && nowTime <= jieshuTime) {
                    item.isDisable = false
                } else {
                    item.isDisable = true
                }
                return item;
            })

            // 可用
            let useList = res.data.filter(item => !item.isDisable)
            // 不可用
            let notUseList = res.data.filter(item => item.isDisable)
            dataList = useList.concat(notUseList)
            _this.setData({
                dataList,
            })
        })
    },

    // 点击-选择优惠卷
    couponSelect(e) {
        console.log('couponSelect', e);
        let _this = this;
        if (!e.currentTarget.dataset.isdisable) {
            let couponId = e.currentTarget.dataset.id;
            if(_this.data.couponId == couponId){
                couponId = ''
            }
            _this.setData({
                couponId
            });
        }
    },

    // 确定
    submit() {
        let _this = this;
        let pages = getCurrentPages(); //获取page
        let prevPage = pages[pages.length - 2]; //上一个页面（父页面）
        prevPage.setData({
            couponId: _this.data.couponId
        })
        wx.navigateBack();
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})