<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: {},
				timestamp: 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
			console.log(this.list)
		},
		methods: {
			//购买
			tobuy() {
				var that = this
				var obj = {
					"trainingItemId": undefined,
					"nurseStationId": undefined,
					"nurseStationPersonId": undefined,
					"trainingOrderAmount": undefined,
					"nursePersonName": undefined,
				}
				obj.trainingItemId = this.list.trainingItemId
				obj.nurseStationId = uni.getStorageSync('nurseStationId');
				obj.nurseStationPersonId = uni.getStorageSync('nursePersonId');
				obj.nursePersonName = uni.getStorageSync('nursePersonName');
				obj.trainingOrderAmount = this.list.trainingItemPrice
				trainingItemOrder(obj).then(res => {
					if (res.code == 200) {
						if (res.data.trainingOrderAmount > 0) {
							let objs = {
								"openid": uni.getStorageSync('openid'),
								"nurseStationPersonId": res.data.nurseStationPersonId,
								"orderNo": res.data.trainingOrderNo,
								"payType": "WECHAT_PAY",
								"orderChannel": "WECHAT_APPLET",
								"buySource": "TRAINING",
								"paymentPrice": res.data.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) {
											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>
