// index.js
let myPro = require("../../../utils/wxRequest.js");
let util = require("../../../utils/util.js");
Page({
    data: {
		couponBg: "/statics/img/quan_bg_tu.png",
		couponUnBg: "/statics/img/quan_un_bg_tu.png",
		status: -1,
		page: 1,
		size: 10,
		dataList: [],
		finished: false,
		loading: false
    },
    onLoad: function (options) {
        // Do some initialize when page load.
		let that = this;
		that.setData({
			couponBg: "data:image/png;base64," + wx.getFileSystemManager().readFileSync(that.data.couponBg, "base64"),
			couponUnBg: "data:image/png;base64," + wx.getFileSystemManager().readFileSync(that.data.couponUnBg, "base64"),
		});
		// 获取列表
		that.getDataList();
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
    },
    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.
		let that = this;
		if(!that.data.finished){
			that.getDataList();
		};
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 切换tab
	onChange(event){
		let that = this;
		that.setData({
			status: event.detail.name,
			page: 1,
			dataList: [],
			finished: false
		});
		that.getDataList();
	},
	// 获取列表
	getDataList(){
		let that = this;
		let data = {
			status: that.data.status,
			page: that.data.page,
			size: that.data.size
		};
		myPro.wxRequest("user/v2/usercoupon/list","GET",data).then(res=>{
			let list = res.result;
			// 无数据
			if(list.length == 0){
				wx.showToast({
					title: "暂无更多数据",
					icon: "none",
				});
				that.setData({
					finished: true
				});
			}else{
				// 判断可用状态的券是否过期(在争取后台返回,尚未同意)
				for(let i in list){
					if(list[i].status == 1){
						let due_time = list[i].due_time;
						if(new Date().getTime() > new Date(due_time.replace(/-/g, "/")+" 24:00:00").getTime()){
							list[i].status = 0;
						}
					}
				};
				that.setData({
					dataList: that.data.dataList.concat(list),
					page: that.data.page + 1
				});
			}
		}).catch(err=>{
			console.log('错误信息',err)
			wx.showToast({
				title: err,
				icon: "none",
			});
		})
	},
	// 去买商品
	goGoodsIndex(){
		let that = this;
		wx.reLaunch({
			url: "/pages/goods/index"
		});
	}
});
