<template>
	<page-container :title="$config.getAppSetting('app_name') + '-支付成功'" :showBack="false">
		<view style="height: 20vh;"></view>
		<view class="pay-success-content">
			系统处理中，您可以返回'{{ $config.getAppSetting('app_name') }}'小程序，在'消费记录'中查看订单。
		</view>
		<view style="height: 30vw;"></view>
		<view class="pay-success-button-container" :class="{ 'single-button': isIOS }">
			<button v-if="!isIOS" type="warn" class="pay-success-button" @click="downloadAPP">下载APP</button>
			<button type="primary" class="pay-success-button" @click="backMP">返回小程序</button>
		</view>
	</page-container>

</template>

<script>
	import PageContainer from '@/components/page-container/page-container.vue'
	import {
		getOrderUrlLink
	} from '@/common/server/order'
	import {
		msg
	} from '@/common/util'
	import {
		sleep
	} from '../../common/util'
	export default {
		components: {
			PageContainer
		},
		data() {
			return {
				order_num: '',
				isIOS: false,
				res: null
			}
		},
		onLoad(options) {

			console.log(options, 'options');
			// 检查是否是iOS环境
			const userAgent = navigator.userAgent;
			this.isIOS = userAgent.indexOf('iPhone') > -1 || userAgent.indexOf('iPad') > -1;
			this.load();
		},
		methods: {
			load() {
				this.$platform.getOrderNo(this).then(order_num => {
					console.log("order_num", order_num);
					if (order_num != null && order_num != "") {
						this.order_num = order_num;
						this.backMP(true);
					}
				});
			},
			async backMP(isLoad = false) {
				if (this.order_num == '') {
					return;
				}
				if (this.res != null) {
					if (this.res.status == 1) {
						window.location.href = this.res.data;
						return;
					}
				}
				// console.log(this.order_num, 'this.order_num');
				const res = await getOrderUrlLink(this.order_num);
				// console.log(res, 'res');
				this.res = res;
				if (res.status == 1) {
					console.log(res.data, "res.datares.datares.datares.data");
					if (isLoad) {
						msg('支付成功，2秒后自动返回小程序');
						await sleep(2000);
					}
					window.location.href = res.data;
				}
			},
			downloadAPP() {
				//通过浏览器标识判断是否是ios环境
				const userAgent = navigator.userAgent;
				if (userAgent.indexOf('iPhone') > -1 || userAgent.indexOf('iPad') > -1) {
					this.$c.msg('IOS正在开发中。。。。');
				} else {
					window.location.href = 'https://youdas-1308826010.cos.ap-shanghai.myqcloud.com/apk/app.apk';
				}
			}


		}
	}
</script>

<style lang="scss" scoped>
	.pay-success-content {
		margin: 0 auto;
		width: 80vw;
		height: 100%;
		text-align: center;
		color: #e6473f;
		font-size: 38rpx;
	}

	.pay-success-button-container {
		display: flex;
		justify-content: center;
		gap: 20px;
		margin-top: 30px;

		&.single-button {
			.pay-success-button {
				width: 440rpx;
			}
		}
	}

	.pay-success-button {
		width: 220rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;

		border-radius: 8rpx;
		font-size: 28rpx;
	}
</style>