<template>
	<ax-body blank="0">
		<view class="page-background">
			<image :src="pageBg" mode="widthFix"></image>
		</view>
		<view style="height: 324rpx;"></view>
		<view class="body">
			<!-- 统计卡 -->
			<view class="app-flex c-between" style="padding-top: 10rpx;">
				<view class="sta-card">
					<view class="value">{{ userinfo.integral }}</view>
					<view class="name">我的积分</view>
					<image src="@/static/img/my-sta-bg01.png" mode="heightFix" class="bg"></image>
				</view>
				<view class="sta-card">
					<view class="value">{{ userinfo.accountBalance }}</view>
					<view class="name">可抵扣余量</view>
					<image src="@/static/img/my-sta-bg03.png" mode="heightFix" class="bg"></image>
					<view class="overflow-payment" v-if="order_info != null" @click="topage_order">
						<text v-if="order_info.maspStatus === 1">超{{ (order_info.maspAmount +
							order_info.maspRealAmount).toFixed(2) || '0.00' }}元</text>
						<text class="ax ax-iconline i-arrow-right icon"></text>
					</view>
				</view>
			</view>
			<!-- 购券 -->
			<view class="card">
				<view class="title">
					<view>请选择抵扣券</view>
					<template v-if="order_info != null">
						<view class="title-text" v-if="order_info.maspStatus === 1">
							至少充值{{ (3 + ((order_info.maspAmount + order_info.maspRealAmount))).toFixed(2) || '0.00'
							}}元(超充{{ (order_info.maspAmount + order_info.maspRealAmount).toFixed(2) || '0.00'
							}}元，余量需大于3元才能启动)
						</view>
					</template>
				</view>
				<view class="new-coupons-box">
					<view v-for="(item, index) in coupons.data" :key="index"
						:class="{ newCouponsActive: coupons.index == index }" class="new-coupons"
						@click="choose(item, index)">
						<!-- :style="{ opacity: `${(3 + (order_info.maspAmount + order_info.maspRealAmount)).toFixed(2) > item.levelMoney ? '0.4' : ''}` }" -->
						<view class="new-coupons-price">{{ item.money }}<text style="font-size: 22rpx;">元</text>
						</view>
						<view class="new-coupons-text">充电券</view>
					</view>
				</view>
			</view>
			<!-- 提示 -->
			<view class="card tips">
				<view class="title">温馨提示</view>
				<view class="li">1. 抵扣券仅用于充电结算中抵扣资费，未使用完的余量，可手动发起退还</view>
				<view class="li">2. 抵扣券为专属专用不可转赠和出售</view>
				<view class="li">3. 抵扣券金额未抵扣完结,可累计到下次继续抵扣</view>
				<view class="li">4. 使用时可抵扣按照当前电价进行预估计算</view>
				<view class="li">5. 实际结算价以具体充电时段为准</view>
			</view>
			<view style="height: 100rpx;"></view>
			<view class="footer">
				<template v-if="order_info != null">
					<view class="payment-tips" v-if="coupons.index >= 0">
						购买{{ select_num }}元，补缴{{ (order_info.maspAmount + order_info.maspRealAmount).toFixed(2) }}元，剩余{{
							(select_num - (order_info.maspAmount + order_info.maspRealAmount)).toFixed(2) }}元
					</view>
				</template>
				<button @click="pay()" class="pay" :disabled="coupons.index < 0">立即支付购买</button>
				<ax-ios-indicator></ax-ios-indicator>
			</view>
		</view>
	</ax-body>
</template>

<script>
export default {
	data() {
		return {
			pageBg: 'https://national-motion.oss-cn-beijing.aliyuncs.com/20260106/900829d6fae247979389f8d75967cf21.png',
			userinfo: {},
			select_num: null,
			payment: null,
			order_info: null,
			coupons: {
				index: -1,
				data: []
			}
		}
	},
	onShow() {
		this.getMyAccount()
		this.getLevel()
		// this.select_orderstatus()
		// this.get_orderPayment()
	},
	onLoad(options) {
		this.payment = options.payment
	},
	methods: {
		getLevel() {
			this.$api.base("get", "/applet/v1/order/getReChargeLevel", {}, {}).then(res => {
				this.coupons.data = res.data;
			})
		},
		choose(item, index) {
			if (this.order_info != null) {
				let num = ((this.order_info.maspAmount + this.order_info.maspRealAmount) + 3).toFixed(2)
				if (num > item.levelMoney) {
					this.$app.popup.toast(`购买金额必须大于${num}元`)
					return
				}
			}
			this.coupons.index = this.coupons.index != index ? index : -1;
			this.select_num = item.levelMoney
		},
		pay() {
			if (!this.userinfo.appletUserId) {
				this.$app.url.goto('/pages/login/login')
				return;
			}
			// this.$app.popup.toast('充值入口正在维护，敬请期待！')
			// return
			this.$api.base("post", "/applet/v1/wft/order/createOrder", {
				levelId: this.coupons.data[this.coupons.index].id
			}, {}).then(addRes => {
				console.log(addRes, '创建订单返回');
				if (addRes.data.orderId) {
					this.$api.base("put", `/applet/v1/wft/order/payOrder/${addRes.data.orderId}`, {}, {}).then(res => {
						var payInfo = JSON.parse(res.data.params.pay_info)
						console.log(payInfo, '11');
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: payInfo.timeStamp,
							nonceStr: payInfo.nonceStr,
							package: payInfo.package,
							signType: payInfo.signType,
							paySign: payInfo.paySign,
							success: (res) => {
								console.log('success:', res);
								// 查询支付状态
								this.$api.base("get", `/applet/v1/wft/order/query/${addRes.data.orderNo}`, {}, {}).then(queryRes => {
									console.log(queryRes, '查询订单返回');
									if (queryRes.data) {
										setTimeout(() => {
											this.$app.popup.alert('支付成功', '温馨提示', {
												showCancel: false
											}).then(() => {
												this.$app.url.back()
											});
										}, 1000)
									} else {
										this.$app.popup.alert('支付失败，请稍后重试', '温馨提示', {
											showCancel: false
										}).then(() => {
											this.$app.url.back()
										});
									}
								})
							},
							fail: (err) => {
								console.log('fail:', err);
								this.$api.base("put", `/applet/v1/wft/order/closeOrder/${addRes.data.orderNo}`, {}, {}).then(cancelRes => {
									console.log(cancelRes, '取消订单返回');
									if (cancelRes.data) {
										this.$app.popup.alert('支付已取消', '温馨提示', {
											showCancel: false
										});
									}
								});
								//注册一个用户取消支付的事件
							}
						});

					})
				}
			})

		},
		get_orderPayment() {
			// maspStatus（1:待补缴，2：已补缴）realPredictServiceCost平台；maspAmount第三方
			this.$api.base("post", "/chargeApi/queryOrderList-arrearage", {}, {}).then(res => {
				this.order_info = res.data
			})
		},
		topage_order() {
			this.$app.url.goto('/subPackages/order/order-detail/order-detail?orderId=' + this.order_info.id);
		},
		getMyAccount() {
			this.$api.base("get", "/applet/v1/user/getUserInfo", {}, {}).then(res => {
				this.userinfo = res.data
			})
		},
	}
}
</script>

<style scoped>
@import url("coupon-buy.css");
</style>