<template>
	<view class="default_page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='我的订单' bgColor="transparent"></cus-header>
		<image class="top_bg" :src="imgBase+'order_detail_bg.png'" mode="widthFix"></image>
		<view class="top adf">
			<view class="top-left adfac">
				<image :src="imgBase+'order_detail_success.png'"></image>
				<view class="top-left-texts adffc">
					<view class="text">{{statusCfg[orderInfo.orderStatus]}}</view>
					<view class="text tip adfac" v-if="orderInfo.orderStatus===0">
						剩余支付时间<text style="margin: 0 4rpx;"></text>
						<u-count-down :time="countDownTime" format="HH:mm:ss" autoStart @finish="finish"></u-count-down>，
						超时将自动取消
					</view>
					<view class="text tip" v-else>{{statusTip[orderInfo.orderStatus]||''}}</view>
				</view>
			</view>
			<view class="top-right">￥{{orderInfo.payAmount||0}}</view>
		</view>
		<view class="form">
			<view class="form-item adfacjb">
				<view class="form-item-title">订单号</view>
				<view class="form-item-value">{{orderInfo.orderNo||''}}</view>
			</view>
			<view class="form-item adfacjb">
				<view class="form-item-title">订单类型</view>
				<view class="form-item-value">{{typeCfg[orderInfo.type]}} {{orderInfo.totalFrequency}}次</view>
			</view>
			<view class="form-item adfacjb">
				<view class="form-item-title">订单金额</view>
				<view class="form-item-value">￥{{orderInfo.payAmount||0}}</view>
			</view>
			<view class="form-item adfacjb">
				<view class="form-item-title">下单时间</view>
				<view class="form-item-value">{{orderInfo.createDate||''}}</view>
			</view>
			<view class="form-item adfacjb">
				<view class="form-item-title">有效期至</view>
				<view class="form-item-value">{{orderInfo.expirationTime||''}}</view>
			</view>
		</view>
		<view class="bottom adfacjb">
			<template v-if="orderInfo.orderStatus==1">
				<view class="bottom-default half qx" @click="backHome">返回首页</view>
				<view class="bottom-default half" @click="handlePublish">发布问卷</view>
			</template>
			<template v-else>
				<template v-if="orderInfo.orderStatus!=0">
					<view class="bottom-default" @click="buyAgain">再次购买</view>
				</template>
				<template v-else>
					<view class="bottom-default half qx" @click="orderCancel">取消订单</view>
					<view class="bottom-default half" @click="orderPay">立即支付</view>
				</template>
			</template>
		</view>
		<WechatPay ref="wxPay" @confirmPay="toPay" @cancelPay="cancelPay"></WechatPay>
	</view>
</template>

<script>
	import WechatPay from '@/components/wechatPay/index.vue'
	export default {
		components:{ WechatPay },
		data(){
			return {
				id:'',
				orderInfo:null,
				countDown:15*60,//15分钟 单位秒
				countDownTime:0,
				statusCfg:{
					'-3':'已退款',
					'-2':'已取消',
					'-1':'已失效',
					'0':'待支付',
					'1':'已支付',
					'2':'已完成'
				},
				typeCfg:{
					'1':'个人版',
					'2':'团队版'
				},
				statusTip:{
					'1':'您的问卷已激活，开始PERILL团队发展动态评估',
					'2':'您的问卷已激活，开始PERILL团队发展动态评估',
					'-2':'您的订单已取消，可重新购买'
				}
			}
		},
		onLoad(options) {
			this.id = options.id;
			this.id&&this.getDetail()
		},
		methods:{
			getDetail(){
				this.$api.get(`/que/order/${this.id}`).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					this.orderInfo = res.data;
					if(this.orderInfo.orderStatus===0){
						let diff = new Date().getTime() - new Date(this.orderInfo.createDate).getTime();
						let diffS = Math.ceil(diff/1000)
						if(diffS<this.countDown) this.countDownTime = (this.countDown-diffS)*1000;
					}
				})
			},
			finish(){
				this.$api.put(`/que/order/closeOrder/${this.orderInfo.orderNo}`).then(({data:res})=>{
					this.$showToast('订单已取消')
					this.getDetail();
				})
			},
			backHome(){
				uni.reLaunch({
					url:'/pages/home'
				})
			},
			handlePublish(){
				uni.reLaunch({
					url:'/pages/publish'
				})
			},
			buyAgain(){
				uni.navigateTo({
					url:'/pagesPublish/rechargeCenter'
				})
			},
			orderCancel(){
				uni.showModal({
					title:'温馨提示',
					content:'确定取消该订单？',
					success: (res) => {
						if(res.confirm){
							this.$api.put(`/que/order/closeOrder/${this.orderInfo.orderNo}`).then(({data:res})=>{
								if(res.code!==0) return this.$showToast(res.msg)
								this.$showToast('取消成功')
								setTimeout(()=>{
									uni.navigateBack()
								},1500)
							})
						}
					}
				})
			},
			orderPay(){
				this.$refs.wxPay.payShow = true;
			},
			toPay(){
				this.$api.post('/pay/createOrder',{
					orderNo:this.orderInfo.orderNo,
					openId:JSON.parse(uni.getStorageSync('userInfo')).openId
				}).then(({data:res})=>{
					if(!res.hasOwnProperty('paySign')) return this.$showToast('支付失败')
					this.$refs.wxPay.payShow = false;
					this.$wxPay(res).then(result => {
						uni.navigateTo({
							url:'/pagesPublish/payResult'
						})
					})
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.default_page{
		padding: 0 24rpx 192rpx;
		box-sizing: border-box;
		background: #F7F7F7;
		
		.top_bg{
			width: 100%;
			position: absolute;
			left: 0;
			top: 0;
		}
		
		.top{
			margin-top: 54rpx;
			padding: 0 24rpx;
			position: relative;
			justify-content: space-between;
			&-left{
				image{
					width: 69rpx;
					height: 69rpx;
				}
				&-texts{
					margin-left: 20rpx;
					.text{
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						font-size: 40rpx;
						color: #002846;
						line-height: 40rpx;
						&.tip{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #667E90;
							line-height: 26rpx;
							margin-top: 24rpx;
						}
					}
				}
			}
			&-right{
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 36rpx;
				color: #002846;
				line-height: 30rpx;
				text-align: right;
			}
		}
		
		.form{
			background: #FFFFFF;
			border-radius: 25rpx;
			margin-top: 54rpx;
			padding: 0 24rpx;
			position: relative;
			&-item{
				box-shadow: inset 0rpx -1rpx 0rpx 0rpx #EFEFEF;
				padding: 34rpx 0;
				&-title{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #002846;
					line-height: 30rpx;
				}
				&-value{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #667E90;
					line-height: 30rpx;
					text-align: right;
				}
			}
		}
	
		.bottom{
			width: calc(100% - 88rpx);
			height: 88rpx;
			position: fixed;
			left: 44rpx;
			bottom: 54rpx;
			&-default{
				width: 100%;
				height: 88rpx;
				border: 1rpx solid #33A7A7;
				background: linear-gradient( 90deg, #33A7A7 0%, #64BBBB 100%);;
				border-radius: 44rpx;
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #FFFFFF;
				line-height: 88rpx;
				text-align: center;
				letter-spacing: 2rpx;
				&.half{
					width: calc(50% - 19rpx);
				}
				&.qx{
					color: #657588;
					background: #FFFFFF;
					border: 1rpx solid #CCD4DA;
				}
			}
		}
	}
</style>