<template>
	<view class="default_page adffc" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='我的订单'></cus-header>
		<view class="tab adfac">
			<view class="tab-pre" :class="{'active':tindex===index}" @click="changeTab(item,index)"
				v-for="(item,index) in statusList" :key="index">{{item.name}}</view>
		</view>
		<view class="list" v-if="list.length">
			<view class="list-pre" v-for="(item,index) in list" :key="index" @click.prevent="handleDetail(item)">
				<view class="list-pre-top adfacjb">
					<view class="list-pre-top-no">订单号：{{item.orderNo}}</view>
					<view class="list-pre-top-status" :class="statusColor[item.orderStatus]">{{statusCfg[item.orderStatus]}}</view>
				</view>
				<view class="list-pre-info adf">
					<view class="list-pre-info-left adfac">
						<image class="list-pre-info-left-img" :src="imgBase+'order_img.png'"></image>
						<view class="list-pre-info-left-texts">
							<view class="list-pre-info-left-texts-num">{{typeCfg[item.type]}}{{item.totalFrequency}}次，剩余{{item.useFrequency}}次可用</view>
							<view class="list-pre-info-left-texts-date">有效期至 {{item.expirationTime||' - '}}</view>
						</view>
					</view>
					<view class="list-pre-info-right">￥{{(+item.totalAmount||0).toFixed(2)}}</view>
				</view>
				<view class="list-pre-btns adf">
					<view class="list-pre-btns-pre cancel" v-if="item.orderStatus==0" @click.stop="orderCancel(item)">取消订单</view>
					<view class="list-pre-btns-pre" @click.stop="handlePublish" v-if="item.orderStatus==1">发布问卷</view>
					<view class="list-pre-btns-pre" @click.stop="handlePay(item)" v-if="item.orderStatus==0">立即支付</view>
					<view class="list-pre-btns-pre" @click.stop="buyAgain" v-else>再次购买</view>
				</view>
			</view>
		</view>
		<view class="empty adffcacjc" v-else>
			<page-empty text="暂无订单"></page-empty>
		</view>
		<WechatPay ref="wxPay" @confirmPay="toPay" @cancelPay="cancelPay"></WechatPay>
	</view>
</template>

<script>
	import PageEmpty from '@/components/pageEmpty/index.vue'
	import WechatPay from '@/components/wechatPay/index.vue'
	export default {
		components:{ PageEmpty, WechatPay },
		data(){
			return {
				tindex:0,
				statusList:[
					{state:'',name:'全部'},
					{state:'1',name:'已支付'},
					{state:'0',name:'待支付'},
					{state:'-2',name:'已取消'}
				],
				statusCfg:{
					'-3':'已退款',
					'-2':'已取消',
					'-1':'已失效',
					0:'待支付',
					1:'已支付',
					2:'已完成'
				},
				statusColor:{
					0:'dzf',
					1:'yzf',
					2:'yzf',
					'-2':'yqx'
				},
				typeCfg:{
					1:'基础版',
					2:'专业版'
				},
				query:{
					page:1,
					limit:10,
					orderStatus:'',
					userId:''
				},
				isOver:false,
				list:[],
				orderNo:''
			}
		},
		onReachBottom() {
			if(this.isOver) return
			this.getList()
		},
		onShow() {
			if(uni.getStorageSync('userInfo')){
				this.query.userId = JSON.parse(uni.getStorageSync('userInfo')).id;
				this.init()
			}
		},
		methods:{
			init(){
				this.query.page = 1;
				this.isOver = false;
				this.list = [];
				this.getList()
			},
			getList(){
				this.$api.get('/que/order/page',this.query).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					this.list = [...this.list,...res.data.list]
					if(this.list.length < res.data.total) this.query.page++
					else this.isOver = true
				})
			},
			changeTab(item,index){
				this.tindex = index;
				this.query.orderStatus = item.state;
				this.init()
			},
			handleDetail(item){
				uni.navigateTo({
					url:'/pagesMy/orderDetail?id='+item.id
				})
			},
			orderCancel(item){
				uni.showModal({
					title:'温馨提示',
					content:'确定取消该订单？',
					success: (res) => {
						if(res.confirm){
							this.$api.put(`/que/order/closeOrder/${item.orderNo}`).then(({data:res})=>{
								if(res.code!==0) return this.$showToast(res.msg)
								this.$showToast('取消成功')
								this.init();
							})
						}
					}
				})
			},
			handlePublish(){
				uni.reLaunch({
					url:'/pages/publish'
				})
			},
			handlePay(item){
				this.orderNo = item.orderNo;
				this.$refs.wxPay.payShow = true;
			},
			toPay(){
				this.$api.post('/pay/createOrder',{
					orderNo:this.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'
						})
					})
				})
			},
			buyAgain(){
				uni.navigateTo({
					url:'/pagesPublish/rechargeCenter'
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.default_page{
		padding: 0 24rpx 40rpx;
		box-sizing: border-box;
		background: #F7F7F7;
		
		.tab{
			position: fixed;
			left: 0;
			right: 0;
			height: 90rpx;
			background: #FFFFFF;
			&-pre{
				width: 25%;
				height: 90rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #667E90;
				line-height: 90rpx;
				text-align: center;
				position: relative;
				&.active{
					font-weight: bold;
					font-size: 32rpx;
					color: #002846;
					&::after{
						content: '';
						width: 48rpx;
						height: 6rpx;
						background: linear-gradient( 90deg, #33A7A7 0%, #64BBBB 100%);;
						border-radius: 5rpx;
						position: absolute;
						left: 50%;
						margin-left: -24rpx;
						bottom: 4rpx;
					}
				}
			}
		}
		
		.list{
			padding-top: 110rpx;
			&-pre{
				background: #FFFFFF;
				border-radius: 25rpx;
				margin-top: 20rpx;
				padding: 36rpx 24rpx 29rpx;
				&:first-child{
					margin-top: 0;
				}
				
				&-top{
					&-no{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 26rpx;
						color: #667E90;
						line-height: 30rpx;
					}
					&-status{
						padding: 6rpx 13rpx;
						border-radius: 6rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						line-height: 30rpx;
						&.yzf{
							color: #33A7A7;
							background: rgba(51,167,167,0.08);
						}
						&.dzf{
							color: #FD4F66;
							background: rgba(244,101,122,0.1);
						}
						&.yqx{
							color: #667E90;
							background: #F0F2F8;
						}
					}
				}
				
				&-info{
					margin-top: 16rpx;
					border-top: 1rpx solid #EFEFEF;
					padding-top: 32rpx;
					justify-content: space-between;
					&-left{
						&-img{
							width: 64rpx;
							height: 64rpx;
						}
						&-texts{
							margin-left: 20rpx;
							&-num{
								font-family: PingFang-SC, PingFang-SC;
								font-weight: bold;
								font-size: 30rpx;
								color: #002846;
								line-height: 30rpx;
							}
							&-date{
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 26rpx;
								color: #667E90;
								line-height: 30rpx;
								margin-top: 20rpx;
							}
						}
					}
					&-right{
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						font-size: 30rpx;
						color: #1D2129;
						line-height: 30rpx;
						text-align: right;
					}
				}
				
				&-btns{
					justify-content: flex-end;
					margin-top: 30rpx;
					&-pre{
						background: linear-gradient( 90deg, #33A7A7 0%, #64BBBB 100%);;
						border-radius: 27rpx;
						border: 1rpx solid #33A7A7;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #FFFFFF;
						line-height: 30rpx;
						padding: 12rpx 26rpx;
						letter-spacing: 2rpx;
						margin-left: 30rpx;
						&.cancel{
							background: #FFFFFF;
							border: 1rpx solid #E2E2E2;
							color: #95A5B1;
						}
					}
				}
			}
		}
	
		.empty{
			padding-top: 110rpx;
			flex: 1;
		}
	}
</style>