<template>
	<view class="content">
		<kevy-result-page v-if="type" :type="type" :title="title" :primarColor="primarColor" :details="details"
			:primaryBtnText="primaryBtnText" secondaryBtnText="返回" @primaryBtnClick="primaryBtnClick"
			@secondaryBtnClick="secondaryBtnClick" :codeUrlFailureTime="orderData.codeUrlFailureTime" @finish="finish">
		</kevy-result-page>
	</view>
</template>

<script>
	import {
		getOrderDetail,
		getWxPay
	} from '@/request/api/workark.js'
	export default {
		data() {
			return {
				type: '',
				title: '',
				details: [],
				primarColor: '',
				primaryBtnText: '',
				orderId: '72',
				orderData: {},
				codeUrlFailureTime: ''
			}
		},
		onLoad(body) {
			// this.orderId = body.orderId;
			if (this.orderId) this.init();
		},
		methods: {
			async init() {
				let orderData = await getOrderDetail(this.orderId);
				if (!orderData.state) return;
				this.orderData = orderData.data;
				this.details = [{
					label: '订单编号',
					value: orderData.data.orderNo
				}, {
					label: '订单金额',
					value: orderData.data.totalFee
				}, {
					label: '支付状态',
					value: orderData.data.orderStatus
				}];
				this.initStatus();
			},
			initStatus() {
				if (this.orderData.orderStatus === '支付成功') {
					this.type = 'success';
					this.title = '支付成功';
					this.primarColor = '#4fc08d';
					this.primaryBtnText = '查看订单';
				} else if (this.orderData.orderStatus === '未支付') {
					this.type = 'warning';
					this.title = '暂未支付';
					this.primarColor = '#f9b04d';
					this.primaryBtnText = '立即支付';
				} else if (this.orderData.orderStatus === '超时已关闭') {
					this.type = 'error';
					this.title = '订单已失效';
					this.primarColor = '#ff3927';
					this.primaryBtnText = '查看订单';
				}
			},
			async primaryBtnClick() {
				if (this.type === 'success') {

				} else if (this.type === 'warning') {
					let wxConfig = await getWxPay(this.orderData.orderNo, uni.getStorageSync('openId'));
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: wxConfig.data.timeStamp,
						nonceStr: wxConfig.data.nonceStr,
						package: wxConfig.data.prepayId,
						signType: wxConfig.data.signType,
						paySign: wxConfig.data.paySign,
						success: res => {
							this.init();
						},
						fail: err => {}
					});
				}
			},
			secondaryBtnClick() {
				uni.navigateBack();
			},
			finish() {
				this.init();
			}
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .uv-count-down {
		.uv-count-down__text {
			color: #fff;
		}
	}

	.content {
		box-sizing: border-box;
	}
</style>