// index.js
let myPro = require("../../utils/wxRequest.js");
let util = require("../../utils/util.js");
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({
    data: {
		couponBg: "/statics/img/quan_bg_tu.png",
		loading: false, // 全局loading
		dataList: []
    },
    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")
		});
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		that.getData();
    },
	// 获取领券列表
	getData(){
		let that = this;
		myPro.wxRequest("user/v2/coupon/list","GET",{}).then(res=>{
			let list = res.result;
			// 无数据
			if(list.length == 0){
				wx.showToast({
					title: '暂无更多数据',
					icon: "none"
				});
				that.setData({
					dataList: []
				});
			}else{
				that.setData({
					dataList: list
				});
			};
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		})
	},
	// 领取优惠券
	onTakeCoupon(event){
		let that = this;
		let id = event.currentTarget.dataset.id;
		let params = {
			coupon_rule_id: id
		};
		myPro.wxRequest("user/v2/coupon/receive","POST",params).then(res=>{
			Toast(res.msg); // 避免成功时的toast会别wx的loading冲突
			that.getData();
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		});
	},
    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
    },
});
