<template>
	<view class="xkl-com-bg">
		<view style="height: 20rpx;"></view>
		<view style="background-color: #ffffff;border-radius: 10rpx;padding: 30rpx; text-align: center;">
			<view style="color: #fa3534;">
				<text style="font-size: 30rpx;">￥</text>
				<text style="font-size: 36rpx;font-weight: 700;">{{orderItem.pay_money}}</text>
			</view>
			<view style="color: #909399;font-size: 28rpx;">支付金额</view>
		</view>

		<view style="height: 20rpx;"></view>
		<view style="background-color: #fff; padding: 30rpx;border-radius: 10rpx;">
			<view style="font-size: 28rpx;#606266">支付方式</view>
			<view style="height: 20rpx;"></view>
			<u-radio-group v-model="form.pay_type" iconPlacement="right" :borderBottom="true" placement="column">
				<u-radio :name="3">
					<view class="u-flex u-flex-y-center">
						<u-icon name="/static/icon/money.png" size="50rpx" />
						<view style="width: 20rpx;"></view>
						<view>
							<view style="font-size: 30rpx;">余额支付</view>
							<view style="font-size: 28rpx;color: #909399;">当前余额：￥{{userinfo.money}}</view>
						</view>
					</view>
				</u-radio>
				<view style="height: 30rpx;"></view>
<!-- 				<u-radio :name="1">
					<view class="u-flex u-flex-y-center">
						<u-icon name="/static/icon/weixin.png" size="50rpx" />
						<view style="width: 20rpx;"></view>
						<view>
							<view style="font-size: 30rpx;">微信支付</view>
							<view style="font-size: 28rpx;color: #909399;">微信快捷支付</view>
						</view>
					</view>
				</u-radio> -->
			</u-radio-group>
		</view>
		<view style="height: 100rpx;"></view>
		<u-button type="primary" @click="payOrder()">立即支付</u-button>

		<u-modal :show="paynoticeshow" content='未检测到支付结果, 您可以查看订单' @confirm="tore('/pages/order/index')"
			@close="paynoticeshow = false" @cancel="paynoticeshow = false" showCancelButton confirmText="查看订单"
			cancelText="取消" closeOnClickOverlay></u-modal>
	</view>
</template>

<script>
	import {
		API_URL,
		STATIC_URL
	} from '@/env'
	let self;
	export default {
		data() {
			return {
				paynoticeshow: false,
				orderItem: {
					id: '',
					pay_money: '',

				},
				total_price: 0,
				form: {
					order_id: '',
					pay_type: 3,
				},
				code: '',
				userinfo: {
					money: '',
					openid: ''
				},
			}
		},
		mounted() {},
		onLoad(option) {
			self = this;
			if (option && option.order_id) {
				self.form.order_id = option.order_id;
			}
			self.getOrderItem();

		},
		onShow() {
			let options = uni.getEnterOptionsSync();
			const shop_order_pay_show = uni.getStorageSync('shop_order_pay_show');
			if (options.scene == '1038' &&
				options.referrerInfo.appId == 'wxef277996acc166c3' && shop_order_pay_show == 1) {
				uni.setStorageSync('shop_order_pay_show', 0);
				// 代表从收银台小程序返回
				let extraData = options.referrerInfo.extraData;
				if (!extraData) {
					// "当前通过物理按键返回，未接收到返参，建议自行查询交易结果";
					self.paynoticeshow = true;
				} else {
					if (extraData.code == 'success') {
						// "支付成功";
						uni.showToast({
							title: '支付成功',
							icon: 'none'
						});
						uni.redirectTo({
							url: `/pages/order/index`
						});
						
					} else if (extraData.code == 'cancel') {
						// "支付已取消";
						uni.showToast({
							title: '支付已取消',
							icon: 'none'
						})
					} else {
						// "支付失败：" + extraData.errmsg;
						uni.showToast({
							title: "支付失败：" + extraData.errmsg,
							icon: 'none'
						})
					}
				}
			}
		},
		methods: {
			tore(url) {
				uni.redirectTo({
					url: url
				})
			},
			getOrderItem() {
				self.$http.get('/shop/v10/order/item', {
					order_id: self.form.order_id
				}).then(({
					msg,
					data,
					success
				}) => {
					if (success) {
						self.orderItem = data.item;
						self.userinfo.money = data.money;
					}
				})
			},
			payOrder() {
				uni.showLoading({
					title: '',
					mask: true
				});
				self.$http.post('/shop/v10/order/pay', self.form).then(({
					data,
					success,
					msg
				}) => {
					uni.hideLoading();
					if (!success) {
						uni.showToast({
							icon: 'none',
							title: msg
						})
						return;
					}
					if (self.form.pay_type == 3) {
						uni.redirectTo({
							url: `/pages/order/index`
						});
						return;
					}
					
					uni.openEmbeddedMiniProgram({
						appId: data.appid,
						extraData: data.extraData,
						success(res) {
							// 打开成功
							uni.setStorageSync('shop_order_pay_show', 1);
						}
					})
				})
			},

		}
	}
</script>

<style lang="scss">

</style>