<template>
	<view class="default_page adffc" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='PERILL评估' bgColor="transparent"></cus-header>
		<view class="info adfac">
			<view class="info-left">
				<image :src="userInfo.headUrl||avatar"></image>
			</view>
			<view class="info-right">
				<view class="info-right-name adfac">
					<view class="text">{{userInfo.realName||'游客'}}</view>
					<view class="version">{{typeCfg[headData.type]||'基础版'}}</view>
				</view>
				<view class="info-right-tip">可用次数：基础版{{headData.basicCount||0}}次，专业版{{headData.proCount||0}}次</view>
			</view>
		</view>
		<view class="box adffc">
			<view class="box-tab adfacjb">
				<view class="box-tab-pre" :class="{'active':tindex===index}" 
					v-for="(item,index) in tabList" :key="index" @click="changeTab(index)">{{item}}</view>
			</view>
			<template v-if="tindex===0">
				<template v-if="priceList.length">
					<view style="flex: 1;overflow-y: auto;">
						<view class="box-price adf">
							<view class="box-price-pre adffcac" :class="{'active':pindex===index}"
								v-for="(item,index) in priceList" :key="index" @click="changePrice(item,index)">
								<view class="box-price-pre-once" v-if="index===0">限时优惠</view>
								<view class="box-price-pre-times">{{item.frequency<11?(item.frequency+'次'):'超过10次'}}</view>
								<view class="box-price-pre-money"><span>￥</span>{{item.price}}<span v-if="item.frequency>10"></span><span style="font-size: 36rpx;">/次</span></view>
								<view class="box-price-pre-bottom">
									{{item.frequency==1?'首次登录赠送1次':(item.frequency>10?('超过10次，￥'+item.price+'/次'):('￥'+(item.price/item.frequency)+'/次'))}}
								</view>
							</view>
						</view>
					</view>
					<view class="box-other adfacjb" v-if="frequency>10">
						<view class="box-other-left">其他次数</view>
						<view class="box-other-right">
							<cus-number-box :min="min" :number="otherTimes" @valChange="valueChange"></cus-number-box>
						</view>
					</view>
				</template>
				<template v-else>
					<view class="box-empty adffcacjc">
						<image :src="imgBase+'recharge_empty.png'"></image>
						<p>暂无定价，请等待相关人员配置~</p>
					</view>
				</template>
			</template>
			<template v-if="tindex===1">
				<template v-if="priceList2.length">
					<view style="flex: 1;overflow-y: auto;">
						<view class="box-price adf">
							<view class="box-price-pre adffcac" :class="{'active':pindex===index}"
								v-for="(item,index) in priceList2" :key="index" @click="changePrice(item,index)">
								<view class="box-price-pre-times">{{item.frequency<11?(item.frequency+'次'):'超过10次'}}</view>
								<view class="box-price-pre-money"><span>￥</span>{{item.price}}<span v-if="item.frequency>10">/次</span></view>
								<view class="box-price-pre-bottom">
									{{item.frequency==1?'首次登录赠送1次':(item.frequency>10?('超过10次，￥'+item.price+'/次'):('￥'+(item.price/item.frequency)+'/次'))}}
								</view>
							</view>
						</view>
					</view>
					<view class="box-other adfacjb" v-if="frequency>10">
						<view class="box-other-left">其他次数</view>
						<view class="box-other-right">
							<cus-number-box :min="min" :number="otherTimes" @valChange="valueChange"></cus-number-box>
						</view>
					</view>
				</template>
				<template v-else>
					<view class="box-empty adffcacjc">
						<image :src="imgBase+'recharge_empty.png'"></image>
						<p>暂无定价，请等待相关人员配置~</p>
					</view>
				</template>
			</template>
			<template v-if="tindex<2">
				<view class="box-memo">
					<view class="box-memo-title">服务条款说明</view>
					<view class="box-memo-p">
						1、未激活的问卷：付费后使用有效期1年，过期将不能使用。<br/>
						2、已进行中问卷：创建者可以设置答题截止时间，修改时间，以支持少数用户延期做问卷。<br/>
						3、其他次数不能低于10次。
					</view>
				</view>
				<view class="box-btn adfacjb">
					<view class="box-btn-left adfac">
						<view class="box-btn-left-text">实付</view>
						<view class="box-btn-left-money adfac"><span>￥</span>{{sumPrice}}<span style="margin-left: 9rpx;">/{{times}}次</span></view>
					</view>
					<view class="box-btn-right" @click="confirmBuy">确认协议并购买</view>
				</view>
			</template>
			<template v-else>
				<view class="box-empty adffcacjc">
					<image :src="imgBase+'recharge_empty.png'"></image>
					<p>专家版正在开发中</p>
					<text>敬请期待</text>
				</view>
			</template>
		</view>
		<WechatPay ref="wxPay" @confirmPay="toPay" @cancelPay="cancelPay"></WechatPay>
	</view>
</template>

<script>
	import CusNumberBox from '@/components/CusNumberBox/index.vue'
	import WechatPay from '@/components/wechatPay/index.vue'
	export default {
		components:{ CusNumberBox, WechatPay },
		data(){
			return {
				userInfo:null,
				headData:null,
				avatar:this.$imgBase+'my_avatar.png',
				tabList:['基础版','专业版','专家版'],
				typeCfg:{
					1:'基础版',
					2:'专业版',
					3:'专家版',
				},
				tindex:0,
				pindex:'',
				frequency:0,
				priceList:[],
				priceList2:[],
				min:11,
				otherTimes:11,
				price:0,
				times:0,
				sumPrice:0,
				productId:'',
				orderNo:'',
			}
		},
		onLoad(options) {
			if(uni.getStorageSync('userInfo')){
				this.userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				this.getMyInfo(this.userInfo.id);
				this.getList(this.tindex+1)
			} 
		},
		methods:{
			getMyInfo(userId){
				this.$api.get(`/wx/myCount/${userId}`,this.params).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					this.headData = res.data;
				})
			},
			changeTab(index){
				this.tindex = index;
				this.pindex = '';
				this.otherTimes = 11;
				this.price = 0;
				this.times = 0;
				this.sumPrice = 0;
				this.getList(this.tindex+1)
			},
			getList(type){
				this.$api.get(`/core/v2/queproduct/listByType/${type}`).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					if(type==1) this.priceList = res.data
					else if(type==2) this.priceList2 = res.data
				})
			},
			changePrice(item,index){
				this.productId = item.id;
				this.pindex = index;
				this.frequency = item.frequency;
				this.price = item.price;
				this.times = item.frequency;
				if(this.frequency>10){
					this.otherTimes = 11;
					this.sumPrice = this.price*this.otherTimes;
				} else this.sumPrice = this.price;
			},
			valueChange(val){
				this.otherTimes = val;
				this.times = this.otherTimes;
				this.sumPrice = this.price*this.otherTimes;
			},
			confirmBuy(){
				this.$api.post('/que/order/createOrder',{
					payAmount:this.sumPrice,
					productId:this.productId,
					totalAmount:this.sumPrice,
					totalFrequency:this.times,
					type:this.tindex+1,
					userId:this.userInfo.id
				}).then(({data:res})=>{
					if(res.code!==0) return this.$showToast(res.msg)
					this.orderNo = res.data;
					this.$refs.wxPay.payShow = true;
				})
			},
			toPay(){
				this.$api.post('/pay/createOrder',{
					orderNo:this.orderNo,
					openId:this.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;
		background: #F9F6EE;
		box-sizing: border-box;
		
		.info{
			padding: 40rpx 36rpx 37rpx;
			&-left{
				width: 98rpx;
				height: 98rpx;
				image{
					width: 100%;
					height: 100%;
				}
			}
			&-right{
				width: calc(100% - 98rpx);
				padding-left: 20rpx;
				box-sizing: border-box;
				&-name{
					.text{
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						font-size: 36rpx;
						color: #795021;
						line-height: 32rpx;
					}
					.version{
						width: 129rpx;
						height: 40rpx;
						background: url('https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/my_version_bg.png') no-repeat;
						background-size: 100% 100%;
						padding-left: 48rpx;
						box-sizing: border-box;
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						font-size: 22rpx;
						color: #795021;
						line-height: 40rpx;
						margin-left: 12rpx;
					}
				}
				&-tip{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #667E90;
					line-height: 24rpx;
					margin-top: 22rpx;
				}
			}
		}
		
		.box{
			flex: 1;
			overflow-y: auto;
			background: #FFFFFF;
			border-radius: 36rpx 36rpx 3rpx 3rpx;
			padding: 32rpx 30rpx 60rpx;
			
			&-tab{
				&-pre{
					width: calc(100% / 3);
					position: relative;
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 32rpx;
					color: #667E90;
					line-height: 45rpx;
					text-align: center;
					&.active{
						font-weight: bold;
						color: #002846;
						&::after{
							content: '';
							width: 48rpx;
							height: 6rpx;
							background: #002846;
							border-radius: 3rpx;
							position: absolute;
							left: 50%;
							margin-left: -24rpx;
							bottom: -15rpx;
						}
					}
				}
			}
		
			&-price{
				margin-top: 34rpx;
				justify-content: space-between;
				flex-wrap: wrap;
				flex: 1;
				overflow-y: auto;
				&-pre{
					width: calc(50% - 10rpx);
					margin-top: 16rpx;
					background: #FFFFFF;
					border-radius: 24rpx;
					border: 2rpx solid #FBEBCB;
					position: relative;
					&-once{
						width: 121rpx;
						height: 35rpx;
						background: linear-gradient( 270deg, #EF923B 0%, #EA4F27 100%);
						border-radius: 10rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #FFFFFF;
						line-height: 35rpx;
						text-align: center;
						position: absolute;
						left: 0;
						top: 0;
					}
					&-times{
						font-family: PingFang-SC, PingFang-SC;
						font-weight: bold;
						font-size: 30rpx;
						color: #002846;
						line-height: 30rpx;
						text-align: center;
						margin-top: 36rpx;
					}
					&-money{
						font-family: D-DINCondensed, D-DINCondensed;
						font-weight: bold;
						font-size: 53rpx;
						color: #002846;
						line-height: 87rpx;
						margin-top: 20rpx;
						span{
							font-size: 54rpx;
							line-height: 59rpx;
						}
					}
					&-bottom{
						width: 100%;
						height: 56rpx;
						background: #FFF3CB;
						border-radius: 0rpx 0rpx 24rpx 24rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #95581C;
						line-height: 56rpx;
						text-align: center;
						margin-top: 31rpx;
					}
					
					&.active{
						background: url('https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/price_bg.png') no-repeat;
						background-size: 100% 100%;
						.box-price-pre-times,.box-price-pre-money{
							color: #955A1D;
						}
						.box-price-pre-bottom{
							background: url('https://gitee.com/hw_0302/chuang-heng-wechat-images/raw/master/price_bottom2.png') no-repeat;
							color: #795021;
						}
					}
				}
			}
		
			&-other{
				margin-top: 51rpx;
				&-left{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 30rpx;
					color: #002846;
					line-height: 42rpx;
				}
			}
		
			&-memo{
				margin-top: 48rpx;
				&-title{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 28rpx;
					color: #667E90;
					line-height: 40rpx;
				}
				&-p{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #95A5B1;
					line-height: 40rpx;
					margin-top: 16rpx;
				}
			}
		
			&-btn{
				margin-top: 40rpx;
				width: 100%;
				height: 100rpx;
				background: linear-gradient(to right, #242424 0%, #575757 50%);
				border-radius: 24rpx;
				&-left{
					padding-left: 30rpx;
					align-items: baseline;
					&-text{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #FFFFFF;
						line-height: 33rpx;
					}
					&-money{
						font-family: D-DINCondensed, D-DINCondensed;
						font-weight: bold;
						font-size: 36rpx;
						color: #FCEED2;
						line-height: 52rpx;
						margin-left: 9rpx;
						align-items: baseline;
						span{
							font-family: D-DINCondensed, D-DINCondensed;
							font-weight: bold;
							font-size: 24rpx;
							color: #FCEED2;
							line-height: 26rpx;
						}
					}
				}
				&-right{
					width: 365rpx;
					height: 100rpx;
					background: #11120F;
					border-radius: 24rpx;
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 32rpx;
					color: #FCEED2;
					line-height: 100rpx;
					text-align: center;
					letter-spacing: 2rpx;
				}
			}
		
			&-empty{
				flex: 1;
				image{
					width: 370rpx;
					height: 284rpx;
				}
				p{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 30rpx;
					color: #667E90;
					line-height: 40rpx;
					text-align: center;
					margin-top: 21rpx;
				}
				text{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 36rpx;
					color: #667E90;
					line-height: 40rpx;
					text-align: center;
					margin-top: 16rpx;
				}
			}
		}
	}
</style>