// index.js
let myPro = require("../../../utils/wxRequest.js");
let util = require("../../../utils/util.js");
Page({
    data: {
		begin_at: '',
		end_at: '',
		showBegin: false,
		showEnd: false,
		currentBegin: new Date().getTime(),
		currentEnd: new Date().getTime(),
		couponsNum: 0,
		couponsTotalPrice: 0,
		loading: false // 全局loading
    },
    onLoad: function (options) {
        // Do some initialize when page load.
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		that.setData({
			begin_at: util.formatTime(new Date(),1),
			end_at: util.formatTime(new Date(),1)
		});
		that.bindSearch();
    },
    onReady: function () {
        // Do something when page ready.
    },
    onHide: function () {
        // Do something when page hide.
    },
    onUnload: function () {
        // Do something when page close.
    },
    onPullDownRefresh: function () {
        // Do something when pull down.
		
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 开始时间
	beginTimeFun(){
		let that = this;
		that.setData({
			showBegin: true
		})
	},
	// 选择开始时间
	confirmBeginTime(event){
		console.log('开始时间',event);
		let that = this;
		let selectTime = new Date(event.detail);
		let begin_at = util.formatTime(selectTime,1);
		that.setData({
			begin_at: begin_at,
			currentBegin: event.detail,
			showBegin: false
		})
	},
	// 关闭开始时间
	closeBeginTimePop(){
		let that = this;
		that.setData({
			showBegin: false
		})
	},
	// 结束时间
	endTimeFun(){
		let that = this;
		that.setData({
			showEnd: true
		})
	},
	// 确认结束时间
	confirmEndTime(event){
		// console.log('结束时间',event)
		let that = this;
		let selectTime = new Date(event.detail);
		let end_at = util.formatTime(selectTime,1);
		// 检查结束时间
		if(event.detail < that.data.currentBegin){
			wx.showToast({
				title: "结束时间不可小于开始时间",
				icon: "none"
			});
			return;
		};
		that.setData({
			end_at: end_at,
			currentEnd: event.detail,
			showEnd: false
		});
	},
	// 关闭结束时间
	closeEndTimePop(){
		let that = this;
		that.setData({
			showEnd: false
		})
	},
	// 查询
	bindSearch(){
		let that = this;
		if(!that.data.begin_at){
			wx.showToast({
				title: "请选择开始时间",
				icon: "none"
			});
			return;
		};
		if(!that.data.end_at){
			wx.showToast({
				title: "请选择结束时间",
				icon: "none"
			});
			return;
		};
		if(that.data.currentEnd < that.data.currentBegin){
			wx.showToast({
				title: "结束时间不可小于开始时间",
				icon: "none"
			});
			return;
		};
		that.setData({
			loading: true
		});
		// 开始时间和结束时间比较
		let params = {
			begin_at: that.data.begin_at,
			end_at: that.data.end_at
		};
		myPro.wxRequest("store/tx/coupon","GET",params).then(res=>{
			that.setData({
				loading: false,
				couponsNum: res.result.couponCount ? res.result.couponCount : 0,
				couponsTotalPrice: res.result.coupon_price_count ? res.result.coupon_price_count : 0
			});
		}).catch(err=>{
			console.log('报错信息',err);
			that.setData({
				loading: false
			});
			wx.showToast({
				title: err,
				icon: "none"
			})
		})
	}
});
