// index.js
let myPro = require("../../../utils/wxRequest.js");
let util = require("../../../utils/util.js");
Page({
    data: {
		money: "",
		topBg: "/statics/img/cash_bg.png",
		loading: false // 全局loading
    },
    onLoad: function (options) {
        // Do some initialize when page load.
		let that = this;
		that.setData({
			topBg: "data:image/png;base64," + wx.getFileSystemManager().readFileSync(that.data.topBg, "base64")
		})
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		// 店铺
		wx.getStorage({
			key: "store",
			success: function(res){
				that.setData({
					store: JSON.parse(res.data)
				})
			}
		});
		that.getUserInfo();
		that.getRechargeCard();
    },
    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
    },
	// 用户信息
	getUserInfo(){
		let that = this;
		myPro.wxRequest("user/v2/userinfo","GET",{}).then(res=>{
			that.setData({
				money: res.result.money
			});
			// 同步会员信息 is_member
			getApp().globalData.is_member = res.result.is_member;
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: 'none'
			});
		});
	},
	// 获取充值卡
	getRechargeCard(){
		let that = this;
		let params = {
			type: 2,
			card_type: 1 // 1普通 2年卡(会员卡)
		};
		myPro.wxRequest("user/v2/rechargecardlist","GET",params).then(res=>{
			let list = res.result;
			for(let i in list){
				list[i].checked = false;
				// 卡+券的总金额 (都按代金券走,折扣券不考虑)
				if(list[i].coupon_rule){
					list[i].priceAddCoupons = ((list[i].coupon_rule.coupon.price * list[i].coupon_rule.num) + parseFloat(list[i].price)).toFixed(2)
				}else{
					list[i].priceAddCoupons = list[i].price
				}
			};
			that.setData({
				cardList: list
			});
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		})
	},
	// 选择充值卡
	clickCard(event){
		let that = this;
		let id = event.currentTarget.dataset.id;
		let list = that.data.cardList;
		for(let i in list){
			if(list[i].id == id){
				list[i].checked = !list[i].checked
				if(list[i].checked){
					that.nextFun(list[i].id)
				};
			}else{
				list[i].checked = false
			}
		};
		that.setData({
			cardList: list
		});
	},
	// 充值
	nextFun(id){
		let that = this;
		let params = {
			recharge_card_id: id,
			store_id: that.data.store ? that.data.store.id : 1 // 门店id
		};
		myPro.wxRequest("user/recharge","POST",params).then(res=>{
			let config = res.result.config;
			that.wxPayCard(JSON.parse(config));
		}).catch(err=>{
			console.log('报错信息',err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		})
	},
	// 微信付款
	wxPayCard: function(result) {
		let that = this;
		wx.requestPayment({
			timeStamp: result.timeStamp,
			nonceStr: result.nonceStr,
			package: result.package,
			signType: result.signType,
			paySign: result.paySign,
			success(res) {
				wx.showToast({
					title: "充值成功",
					icon: "none"
				});
				// 拉取余额和会员角色
				that.getUserInfo();
			},
			fail(res) {
				wx.showToast({
					title: "充值失败",
					icon: "none"
				});
				console.log("报错信息",res)
			}
		});
	},
	// 充值记录
	goCashLog(){
		let that = this;
		wx.navigateTo({
			url: "/pages/user/cashlog/index"
		})
	}
});
