<template>
	<view v-if="isShowOrder">
		<view class="orderWrapper">
		    <!-- 头部区域：订单号、头像 -->
		    <view class="orderHead">
		        <view class="orderHeadTitle">
		            <view>
		                <text>订单号</text>
		                <text class="orderDetail">{{info.orderId}}</text>
		            </view>
		            <view>
		                <text>对方</text>
		                <text class="orderDetail">{{info.studentName}}学员</text>
		            </view>
		        </view>
		    </view>
		    <!-- 中部区域：订单详情 -->
		    <view class="orderMid">
		        <view>
		            <text>对方ID</text>
		            <text class="orderDetail">{{info.studentId}}</text>
		        </view>
		        <view>
		            <text>需求号</text>
		            <text class="courseNum" @click="toNeedDetail">{{info.requireId}}</text>
		        </view>
		        <view>
		            <text>课程号</text>
		            <text class="courseNum" @click="toCourseDetail">{{info.courseId}}</text>
		        </view>
		        <view>
		            <text>课时费</text>
		            <text class="orderDetail">{{info.requireSalary/100}} (元/节)</text>
		        </view>
				<view>
				    <text>平台服务费</text>
				    <text class="orderDetail">{{info.requireSalary/100}} (元/订单)</text>
				</view>
		    </view>
		    <view class="orderBottom">
		        <view>
		            <text>订单内容</text>
		            <text class="orderDetail">{{info.requireDetail}}</text>
		        </view>
		        <view>
		            <text>下单日期</text>
		            <text class="orderDetail">{{info.datetime}}</text>
		        </view>
		        <view>
		            <text>订单状态</text>
		            <text class="orderPayStatus">{{orderstatus}}</text>
		        </view>
		    </view>
			
			<view class="countdown" v-if="isShowCountDown">
				<uni-countdown :font-size="20" :showDay="false" :hour="0" :minute="minute" :second="second" @timeup="timeover" color="#FFFFFF" background-color="red" />
			</view>
		    

			
			<uni-popup ref="popup" type="dialog">
				<uni-popup-dialog type="success" title="联系方式" message="成功消息" cancelText="复制手机号" confirmText="复制微信号" :content="content" @close="close" @confirm="confirm"></uni-popup-dialog>
			</uni-popup>
			
			<uni-popup ref="popup1" type="message">
				<uni-popup-message type="success" message="复制成功" :duration="1500"></uni-popup-message>
			</uni-popup>

		</view>
		
		<view class="submitBtnWrapper">
		    <view class="paidSucceed" v-if="isShowPay">
				<button class="submitBtn" :disabled="disabled" type="primary"  @click="paymentButton">{{payment}}</button>
		    </view>
			<view class="pay" v-if="observed">
				<view class="paidSucceed">
				    <button class="submitBtn" @click="getPhone" type="primary">联系方式</button>
				    <button class="submitBtn" :disabled="buttonDisabled" @click="applyForRefund" >{{refundButton}}</button>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: '',
				weixin: '',
				content: '',
				isShowOrder: false,
				time: -1,
				observed: false,
				buttonDisabled: false,
				timer: '',
				refundButton: '申请退款',
				isShowCountDown: false,
				payment: '教员支付',
				disabled: true,
				minute: 0,
				second: 9,
				info: {},
				isShowPay: false ,//是否支付
				orderstatus:'',
				secCount: 0,
				
			};
		},
		onLoad(option) {	
			if (option.orderId !== undefined) {
			  //  this.getCountDown(option.orderId)
				this.getOne(option.orderId)
			}
		},
		methods: {
			timeover(){
								uni.showToast({
									title: '时间到'
								})
								this.isShowPay = false
			},
			close() {
				wx.setClipboardData({
					data: this.phone,
					success: res => {
						wx.hideToast()
						this.$refs.popup1.open()
					}
				})
			},
			confirm() {
				wx.setClipboardData({
					data: this.weixin,
					success: res => {
						wx.hideToast()
						this.$refs.popup1.open()
					}
				})
			},
			toNeedDetail() {
				uni.navigateTo({
					url: '/subpkg/student/require/student_require_all_detail?requireId=' + encodeURIComponent(this.info.requireId)
				})
			},
			toCourseDetail() {
				uni.navigateTo({
					url: '/subpkg/teacher/course/teacher_course_all_detail?courseId=' + encodeURIComponent(this.info.courseId)
				})
			},
			// 发送申请退款消息
			async applyForRefund() {
				this.buttonDisabled = true
				const queryObj = {
					studentId: this.info.studentId,
					orderId: this.info.orderId
				}
				const {data: result} = await uni.$http.get('/education/mp-inner-msg/notify/refund', queryObj)
				uni.$showMsg(result.message)
			},
			// 获取手机号
			async getPhone() {
				const {data: result} = await uni.$http.get('/education/student-requirements/getStuNeedPhoneAndWeixin', {uid: this.info.studentId})
				this.phone = result.data.phone
				this.weixin = result.data.weixin
				this.$refs.popup.open()
				this.content = '手机号：'+ this.phone + '\n\n微信号：' + this.weixin
			},
			// 获取订单详情信息
			async getOne(id) {
				const {data: result} = await uni.$http.get('/payment/order-info/getOrderInfoByOrderId', {orderId: id})
				this.info = result.data.one
			//	console.log("1 -> " + this.info)
			    this.getSECcount()
			
			},
			getSECcount(){
				//console.log("2 -> "  + this.info)
				//下面的 new Date().getTime()，后面也要改造从服务器取，不用本机时间
				this.secCount = Math.floor((new Date().getTime() - new Date(this.info.datetime).getTime()) / 1000)
				//console.log(this.secCount)
				// 判断支付状态, 15分钟 = 900 秒
				if (this.info.status == '未支付' && this.secCount < 900) {
					this.minute = (900 - this.secCount) / 60
					this.second = (900 - this.secCount)  % 60
					this.isShowCountDown = true
					this.isShowPay = true
					this.disabled = false
				}
				
				if (this.info.status == '支付成功') {
					this.observed = true
				}
				// 5天后，不可退款
				if (this.info.isRefund == 1 || this.secCount >= 432000) {
					this.buttonDisabled = true
				}
				if (this.info.status == '未支付' && this.secCount > 900){
					this.orderstatus = "超时关闭支付"
				}else{
					this.orderstatus = this.info.status
				}

				this.isShowOrder = true
			},
			
			// // 倒计时
			// async getCountDown(id) {
			// 	const queryObj = {
			// 		orderId: id
			// 	}
			// 	const { data: result } = await uni.$http.get('/payment/order-info/getCountDown',queryObj)
			// 	this.time = result.data.time
			// 	if (result.data.time > 0) {
			// 		this.minute = this.time / 60
			// 		this.second = this.time % 60
			// 	} 
			// },
			
			// 支付
			async paymentButton() {
				const queryObj = {
					inviteId: this.info.inviteId
				}
				const {data: result} = await uni.$http.get('/payment/wx-pay/jsapi', queryObj)
				
				// 微信官方调起支付
				wx.requestPayment({
					timeStamp: result.data.timeStamp,
					nonceStr: result.data.nonceStr,
					package: 'prepay_id=' + result.data.prepay_id,
					signType: 'RSA',
					paySign: result.data.paySign,
					success: res => {
						this.timer = setInterval(() => {
							this.queryOrderStatus()
						}, 1000)
					},
					fail: res => {
					  wx.showToast({
						title: '支付失败',
						icon: 'none',
						duration: 2000
					  })
					}
				})
			},
			queryOrderStatus() {
				// 验证本地订单状态，并推送消息
				uni.request({
				    url: `${uni.$http.baseUrl}/payment/order-info/queryOrderStatus?orderId=` + this.info.orderId, 
				    success: (res) => {
				        if (res.data.message == '支付成功') {
							clearInterval(this.timer)
							wx.showToast({
								title: '支付成功',
								icon: 'none',
								duration: 2000
							})
							
							this.orderstatus = '支付成功'
							this.isShowCountDown = false
							this.isShowPay = false
							this.disabled = true
						}
				    }
				});
			},
		}
	}
</script>

<!-- 设置页面背景 -->
<style lang="scss">
	page{
		height: 100%;
		// background-color: #FFF;
	}
</style>

<style lang="scss" scoped>
/* 设置全局边距 */
.orderWrapper{
	width: 90%;
	padding: 20rpx;
	margin-left: 20rpx;
	border-radius: 30rpx;
	background-color: #FFF;
	font-size: 30rpx;
}

/* 头部区域 */
.orderHead{
    display: flex;
    position: relative;
    height: 200rpx;
    padding-top: 20rpx;
}


.orderHeadTitle{
    display: flex;
    flex-direction: column;
    height: 80px;
    justify-content: space-around;
}

/* 设置内容样式 */
.orderDetail,
.courseNum,
.orderPayStatus{
    margin-left: 20rpx;
    font-weight: bold;
}

.courseNum{
    color: #00B0F0;
    text-decoration: underline;
}

/* 支付状态字体样式 */
.orderPayStatus{
    color: red;
}

/* 中部区域 */
.orderMid{
    height: 220rpx;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
}

/* 底部区域 */
.orderBottom{
    margin-top: 40rpx;
    height: 260rpx;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
}

/* 支付倒计时 */
.orderPay{
    margin-top: 40rpx;
    display: flex;
    align-items: baseline;
}
/* 倒计时时间格式 */
.orderPayTime{
    padding-left: 20rpx;
    font-weight: bold;
    font-size: 40rpx;
    color: red;    
}

/* 提醒信息 */
.warning{
    display: flex;
    justify-content: center;    
    margin-top: 60rpx;
}
/* 提醒信息详情 */
.warningDetail{
    width: 80%;
    display: flex;
    flex-direction: column;
    font-size: 44rpx;
    font-weight: bold;
    color: red;
}

/* 支付及查看信息按钮居中排列 */
.countdown,
.payWrapper{
	margin-top: 40rpx;
    display: flex;
    justify-content: center;
}

.paidSucceed{
    display: flex;
    width: 100%;
    justify-content: space-around;
}

.teachPayMent{
	background-color: #35b882;
}

.payButton{
	border-radius: 30rpx;
}

.pay{
    /* display: flex; */
    width: 100%;
}

	// 提交按钮样式
	.submitBtnWrapper{
		margin-top: 60rpx;
		height: 140rpx;
		align-items: center;
	}
	.submitBtn{
		width: 300rpx;
		border-radius: 50rpx;
		// margin-bottom: 40rpx;
		background-color: #35b882;
		font-size: 30rpx;
		color: #fff;
	}

::v-deep .uni-button-color{
	color: #1aad19 !important;
}
</style>
