<template>
	<view class="app">
		<view class="time" style="" v-if="list.trainingOrderStatus=='WAIT_PAY'">
			剩余付款时间：
			<u-count-down :timestamp="timestamp"></u-count-down>
		</view>
		<view class="trainingOrderStatus"
			v-if="list.trainingOrderStatus=='REFUNDED'||list.trainingOrderStatus=='CANCEL'">
			订单已关闭
		</view>
		<view class="content">
			<view class="details">
				<view class="detailslist">
					<image :src="baseurl+list.trainingItemCoverUrl" mode=""></image>
					<view class="model">
						<span>{{list.trainingItemTitle}}</span>
					</view>
				</view>
				<view class="payinfo">
					<text class="pay">实付款</text>
					<text class="price">￥{{list.trainingOrderAmount?list.trainingOrderAmount:0}}</text>
				</view>
			</view>
		</view>
		<view class="info">
			<span>护理人员：<text>{{list.nursePersonName}}</text></span>
			<!-- <span>联系电话：<text>{{list.phone}}</text></span> -->
			<span>订单编号：<text>{{list.trainingOrderNo}}</text></span>
			<!-- <span>下单时间：<text>{{list.orderTime}}</text></span> -->
		</view>
		<view class="buy" @tap='tobuy' v-if="list.trainingOrderStatus == 'WAIT_PAY'">
			<view class="pay">
				确认支付
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		trainingItemOrder,
		appletTrainingOrderPay
	} from '@/api/learning/index.js'
	import {
		handCloseOrder,
		getWaitPayTrainingOrderDetails
	} from '@/api/Orderdetails/index.js'
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				baseurl: undefined,
				trainingOrderNo: undefined,
				list: undefined,
				timestamp: 0,
				timecount: 0,
				Timer: null,
			};
		},
		onUnload() {
			clearInterval(this.Timer); //清除该函数
		},
		watch: {
			timecount() {
				if (this.timecount <= 0) {
					handCloseOrder().then(res => {
						clearInterval(this.Timer); //清除该函数
						this.list.trainingOrderStatus = 'CANCEL'
					})
				}
			}
		},
		onLoad(options) {
			this.baseurl = baseurl
			this.trainingOrderNo = options.trainingOrderNo
			this.info();
		},
		methods: {
			info() {
				getWaitPayTrainingOrderDetails(this.trainingOrderNo).then(res => {
					this.list = res.data
					var ordertimes = this.list.trainingOrderTime.replaceAll(/\-/gi, "/")
					var time = new Date(ordertimes).getTime() / 1000 + (60 * 60 * 2)
					var times = new Date().getTime() / 1000
					this.timestamp = time - times
					this.timecount = 10
					if (this.timestamp > 0) {
						this.timecount = this.timestamp
						this.Timer = setInterval(() => {
							this.timecount--;
						}, 1000)
					}
				})
			},
			//购买
			tobuy() {
				var that = this
				if (this.list.trainingOrderAmount > 0) {
					let objs = {
						"openid": uni.getStorageSync('openid'),
						"nurseStationPersonId": this.list.nurseStationPersonId,
						"orderNo": this.list.trainingOrderNo,
						"payType": "WECHAT_PAY",
						"orderChannel": "WECHAT_APPLET",
						"buySource": "TRAINING",
						"paymentPrice": this.list.trainingOrderAmount,
					}
					appletTrainingOrderPay(objs).then(response => {
						if (response.code == 200) {
							uni.requestPayment({
								timeStamp: response.data.timeStamp,
								nonceStr: response.data.nonceStr,
								package: response.data.prepayId,
								signType: response.data.signType,
								paySign: response.data.paySign,
								success: function(res) {
									uni.setStorageSync("Refresh", '1')
									that.$refs.uToast.show({
										title: '支付成功',
										type: 'success',
										duration: 1500,
									})
									setTimeout(e => {
										uni.navigateBack({
											delta: 1
										})
									}, 1500)
								},
								fail: function(err) {
									that.$refs.uToast.show({
										title: '取消支付',
										type: 'error',
										duration: 1500,
									})
								}
							});
						} else {
							this.$refs.uToast.show({
								title: response.msg,
								type: 'error',
								duration: 2000
							})
						}
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	@import "./Orderdetails.scss";
</style>
