<template>
	<view>
		<lf-nav title="等待用户支付" :showIcon="true" bgColor="#fff"></lf-nav>
		<view style="height: 50rpx;"></view>
		<lf-nocontent src="/static/images/empty.png" text="等待用户支付…"></lf-nocontent>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				user_id: 0,
				amount: 0,
				timer: null,
				num: 5,
				count: 0,
				overtime: false
			}
		},
		onLoad(options){
			this.user_id = options.user_id;
			this.amount = options.amount;
			this.startTimer();
		},
		onUnload(){
			if(this.timer){
				clearInterval(this.timer);
				this.timer = null;
			}
		},
		methods: {
			startTimer(){
				if(this.timer){
					clearInterval(this.timer);
					this.timer = null;
				}
				this.count++;
				let max_date = 60 * 2; // 两分钟
				let current_date = this.count * 5; // 当前秒数
				if(current_date >= max_date){
					this.overtime = true;
				}
				this.checkOrderPay();
				this.timer = setInterval(() => {
					this.num--;
					if(this.num <= 0){
						clearInterval(this.timer);
						this.timer = null;
						this.num = 5;
						this.startTimer();
					}
				}, 1000);
			},
			checkOrderPay(){
				let token = this.$cookieStorage.get('store_token');
				this.$http.get({
					api: 'api/supplier/offline/check_order_pay',
					data: {
						user_id: this.user_id
					},
					header: {
						token: token
					}
				}).then(res => {
					if(this.$isRight(res.data.data) && res.data.data.status == 2){
						let url = '/pages/business/payment/paystate';
						url += '?payState=1';
						url += '&amount='+ this.amount;
						this.$url(url, {type: 'redirect'});
					}else if(this.overtime || (res.data.data && res.data.data.status == 3)){
						let url = '/pages/business/payment/paystate';
						url += '?payState=0';
						this.$url(url, {type: 'redirect'});
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped="scoped">
	
</style>
