<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" v-if="list.trainingItemPrice">￥{{list.trainingItemPrice}}</text>
					<text class="price" v-else>￥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 baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				baseurl: undefined,
				list: undefined,
				timestamp: 0,
				timestamp2: 0,
				timer: null,
			};
		},
		onUnload() {
			clearInterval(this.Timer); //清除该函数
		},
		watch: {
			timestamp2() {
				if (this.timestamp2 <= 0) {}
			}
		},
		onLoad(options) {
			this.baseurl = baseurl
			this.list = JSON.parse(options.list)
			var ordertimes = this.list.trainingOrderTime.replaceAll(/\-/gi, "/")
			var time = new Date(this.list.trainingOrderTime).getTime() / 1000 + (60 * 60 * 2)
			var times = new Date().getTime() / 1000
			this.timestamp = time - times
			this.timestamp2 = this.timestamp
			this.timer = setInterval(() => {
				this.timestamp2--;
			}, 1000)
		},
		methods: {
			//购买
			tobuy() {
				var that = this
				if (this.list.trainingItemPrice > 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.trainingItemPrice,
					}
					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) {
									that.$refs.uToast.show({
										title: '支付成功',
										type: 'success',
										duration: 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>
