<template>
	<view class="main">
		<view class="topCard flex jcsb">
			<view class="leftBox flex">
				<view class="txt">
					<view class="txtItem">精准人才卡</view>
					<view style="color: #999999;font-size: 28upx;">根据职位条件，推送牛人</view>
					<view style="color: #999999;font-size: 28upx;">向企业精准推送符合条件人员简历</view>
				</view>
			</view>
			<view class="rightBox">
				<image :src="imageRootNew+'jingz.png'"></image>
			</view>
		</view>
		
		
		<view>
			<view v-if="show" class="selectBox">
				<view class="title">
					选择职位
				</view>
				
				<view class="flex jcsb mb40" v-for="(item ,index) in positionList" :key="index">
					<view class="selectItem" :class="item.recruit_id == selectType0 ? 'sType' : 'dType'"
					  @click="clickItem(item, 1)">
						{{ item.recruit_title }}
					</view>
					<input class="inputSel" v-model="useDay" v-if="item.recruit_id == selectType0" style="border: 1px solid #85BAFC"
						type="number" placeholder-style="color: #CACACA"  placeholder="请输入使用份数" />
					<input class="inputSel" value="" :disabled="true" v-else style="border: 1px solid #ffffff"
						type="number" placeholder-style="color: #CACACA"  placeholder="请输入使用份数" />
				</view>
				
				
				<image style="width: 50%;height: 45upx;margin: 0 auto;display: block;" :src="imageRootNew+'shaix.png'"></image>
				
				
				<view class="gaijBox mb40">
					<view class="title">
						毕业院校
					</view>
					<view style="flex-wrap: wrap;" class="flex">
						<view class="gaojItem" :class="item == selectType2 ? 'sType' : 'dType'" v-for="(item, index) in perData.filter_data.graduated_school" :key="index" @click="clickItem(item, 3)">
							{{ item }}
						</view>
					</view>
				</view>
				
				
				<view class="gaijBox mb40">
					<view class="title">
						优先选项
					</view>
					<view style="flex-wrap: wrap;" class="flex">
						<view class="gaojItem" :class="item == selectType3 ? 'sType' : 'dType'" :key="index" @click="clickItem(item, 4)" v-for="(item, index) in perData.filter_data.experience">
							{{ item }}
						</view>
					</view>
				</view>
				
				
				<view style="font-size: 32upx;" class="selectBtn" @click="useEquity">确认使用</view>
			</view>
			
			<view v-else style="text-align: center;font-size: 30upx;margin: 60upx; 0;color: #999;">暂无职位</view>
		</view>
		
		
		
		
		
		
		<view class="buyBox">
			<view class="flex jcsb mb40">
				<view class="title">购买份数</view>
				<view style="color: #BBBBBB;font-size: 30upx;">剩余: {{dayNum}}{{diyUnit}}</view>
			</view>
			
			<view style="margin-bottom: 40upx;align-items: center;" class="flex">
				<view style="margin-right: 20upx;font-size: 28upx;">自定义份数:</view>
				<input style="text-align: left;padding-left: 18upx;width: 72%;" class="inputNum" @input="changeOrderPrice" v-model="diySelect" type="number" placeholder-style="color: #CACACA"  placeholder="请输入购买份数" />
			</view>
			
			<view class="selectDay flex jcsb">
				<view class="dayItem" :class="item == selectType1 ? 'sType' : 'dType'" v-for="(item, index) in perData.num_data" :key="index"  @click="clickItem(item, 2)">
					{{ item }}
				</view>
			</view>
		</view>
		
		<view class="submitBtn" @click="buy">
			立即支付   ¥{{selectOptPrice}}/{{selectOptTxt}}
		</view>
		
		<view style="height: 50upx;"></view>
	</view>
</template>

<script>
	import App from '@/common/js/app.js';
	export default {
		data() {
			return {
				selectType0: 1,
				selectType2: 0,
				selectType3: 0,
				dayNum: 0,
				perData: {},
				positionList: [],
				useDay: '',
				show: true,
				diySelect: '',
				diyUnit: '',
				diyPrice: '',
				selectOptPrice: '',
				selectOptTxt: '',
				selectType1: 1,
				rights_id: null,
				lock_1: false,
				lock_2: false
			};
		},
		onLoad(e){
			this.rights_id = e.rights_id;
		},
		onShow() {
			this.diySelect = '';
			this.getData();
		},
		watch: {
			useDay(nVal){
				clearTimeout(this.lock_1);
				this.lock_1 = setTimeout(()=>{
					nVal = Math.floor(nVal);
					// console.log(nVal);
					this.useDay = nVal || '';
				}, 500)
			},
			// positionList: {
			// 	deep:true,// vue2无深层侦听
			// 	handler (nPVal) {
			// 		console.log(nPVal,this.selectType0, 111);
			// 		clearTimeout(this.lock_1);
			// 		this.lock_1 = setTimeout(()=>{
			// 			console.log(nPVal,this.selectType0);
			// 			for(let i=0;i<this.positionList.length;i++){
			// 				// this.positionList[i].num = '';// 数据层处理了，视图层未刷新
			// 			}
			// 		}, 500)
			// 	}
			// },
			diySelect(nVal){
				clearTimeout(this.lock_2);
				this.lock_2 = setTimeout(()=>{
					nVal = Math.floor(nVal);
					console.log(nVal, 1111);
					this.diySelect = nVal || '';
				}, 500)
			},
		},
		methods: {
			clickItem(val, type){
				// console.log(val, type);
				if(type == 1){
					// 点击职位选项
					this.selectType0 = val.recruit_id;
					this.useDay = '';
					// console.log(val, this.positionList);
				}
				if(type == 2){
					// 点击购买选项
					this.selectType1 = val
					this.selectOptTxt = val
					for(let key in this.perData.num_data){
						if(this.perData.num_data[key]==val){
							this.selectOptPrice = key * this.diyPrice;
						}
					}
					return;
				}
				if(type == 3){
					// 点击院校选项
					this.selectType2 = val
				}
				if(type == 4){
					// 点击优先选项
					this.selectType3 = val
				}
			},
			getData(){
				let data = {
					rights_id: 5
				};
				App._post_form('&p=rights&do=getRightsTypeInfo', data, res => {
					this.dayNum = res.data.remaining_amount;
					this.diyUnit = res.data.amount_unit;
					this.diyPrice = res.data.price;
					this.perData = res.data;
					console.log(this.perData)
					this.positionList = res.data.filter_data.recruit;
					this.show = this.positionList.length>0 ? true : false;
					
					for(let key in this.perData.num_data){
						// 初始化购买选项
						this.selectOptPrice = key * res.data.price;
						this.selectType1 = this.perData.num_data[key];
						this.selectOptTxt = this.perData.num_data[key];
						return;
					}
				})
			},
			changeOrderPrice(e){
				// console.log(e.detail.value, +(this.diyPrice));
				if(!!e.detail.value){
					this.selectOptPrice = (Math.floor(e.detail.value) * +(this.diyPrice)).toFixed(2);
					this.selectOptTxt = Math.floor(e.detail.value) + '份';
					this.selectType1 = '';
				}else{
					for(let key in this.perData.num_data){
						this.selectOptPrice = (key * this.diyPrice).toFixed(2);
						this.selectType1 = this.perData.num_data[key];
						this.selectOptTxt = this.perData.num_data[key];
						return;
					}
				}
			},
			useEquity(){
				let num,school,experience;
				// num = this.positionList.filter(item => {
				// 	return item.recruit_id == this.selectType0
				// });
				for(let key in this.perData.filter_data.graduated_school){
					if(this.perData.filter_data.graduated_school[key]==this.selectType2){
						school = key;
					}
				}
				for(let key in this.perData.filter_data.experience){
					if(this.perData.filter_data.experience[key]==this.selectType3){
						experience = key;
					}
				}
				let data = {
					rights_id: this.rights_id,
					position: JSON.stringify([{recruit_id: this.selectType0,num:this.useDay}]),
					graduated_school: school,
					experience: experience
				}
				console.log(data);
				App._post_form('&p=rights&do=memberUseRights', data, res => {
					console.log(res);
					uni.showToast({
						title: res.message
					})
					this.getData();
				},(err)=>{},(com)=>{})
			},
			buy(){
				let user = uni.getStorageSync('userinfo');
				let num;
				for(let key in this.perData.num_data){
					if(this.perData.num_data[key]==this.selectType1){
						num = key;
					}
				}
				
				let data = {
					rights_id: this.rights_id,
					type: 1,
					num: num || this.diySelect,
					contacts: user.nickname,
					contact_phone: user.truemobile
				};
				console.log(data)
				App._post_form('&p=rights&do=createRightsPay', data, res => {
					uni.navigateTo({
						url: '/pages/mainPages/payment/payment?orderid=' + res.data.orderid + '&plugin=' + res.data.type
					})
					console.log(res.data)
				})
			}
		}
	}
</script>

<style lang="less">
.main{
	padding-top: 40upx;	
}
page{
	background-color: white;
}
.flex{
	display: flex;
}
.jcsb{
	justify-content: space-between;
}
.mb40{
	margin-bottom: 40upx;
}
.topCard{
	width: 94%;
	height: 300upx;
	// border: 1px solid red;
	margin: 0 auto;
	border-radius: 20upx;
	box-shadow:#C9E4FF 0px 0px 5px;
	background-color: white;
	.leftBox{
		width: 66%;
		height: 300upx;
		// border: 1px solid red;
		align-items: center;
		.txt{
			width: 100%;
			// padding: 0% ;
			padding-left: 30upx;
			line-height: 55upx;
		}
		.txtItem{
			font-weight: bold;
			font-size: 40upx;
			font-family: 'PingFangSC-semiBold';
			color: #444444;
			margin-bottom: 10upx;
		}
	}
	.rightBox{
		width: 34%;
		height: 300upx;
		// border: 1px solid blue;
		display: flex;
		align-items: center;
		position: relative;
		image{
			width: 220upx;
			height: 210upx;
			position: absolute;
			left: -6upx;
		}
	}
}

.selectBox{
	width: 94%;
	margin: 0 auto;
	margin-top: 9%;
	// border: 1px solid red;
	.title{
		color: #484848;
		font-size: 32upx;
		margin-bottom: 40upx;
	}
	.selectItem{
		width: 60%;
		height: 80upx;
		border-radius: 10upx;
		text-align: center;
		line-height: 80upx;
		font-size: 26upx;
		
	}
	.selectBtn{
		width: 60%;
		height: 80upx;
		line-height: 80upx;
		border-radius: 10upx;
		color: white;
		text-align: center;
		background-color: #509DFD;
		margin: 0 auto;
	}
	
}

.inputNum{
	background-color: #FAFAFA;
	font-size: 26upx;
	height: 80upx;
	text-align: center;
	border-radius: 10upx;
	width: 80%;
}
.inputSel{
	background-color: #FAFAFA;
	font-size: 26upx;
	height: 80upx;
	text-align: center;
	border-radius: 10upx;
	width: 37%;
}
    .sType{
	    background-color: #F2F8FF;
		color: #6DADFD;
	}
	.dType{
		background-color: #FAFAFA;
		color: #CACACA;
	}
.buyBox{
	width: 94%;
	margin: 0 auto;
	margin-top: 9%;
	// border: 1px solid red;
	.title{
		color: #484848;
		font-size: 32upx;
		 
	}
	.selectDay{
		.dayItem{
			width: 22%;
			height: 70upx;
			border-radius: 10upx;
			text-align: center;
			line-height: 70upx;
			font-size: 30upx;
		}
	}
}

.submitBtn{
	width: 94%;
	height: 90upx;
	border: 1px solid #65A9FD;
	margin: 0 auto;
	margin-top: 40upx;
	border-radius: 50upx;
	margin-bottom: 50upx;
	text-align: center;
	line-height: 90upx;
	color: #64A8FD;
	font-size: 30upx;
}

.gaijBox{
	
	.title{
		color: #484848;
		font-size: 30upx;
		margin-bottom: 40upx;
	}
	.gaojItem{
		width: 30%;
		height: 80upx;
		background-color: #FAFAFA;
		text-align: center;
		line-height: 80upx;
		color: #999999;
		border-radius: 10upx;
		margin-right: 20upx;
		font-size: 26upx;
		margin-bottom: 15upx;
	}
	.sType{
	    background-color: #F2F8FF;
		color: #6DADFD;
	}
	.dType{
		background-color: #FAFAFA;
		color: #999999;
	}
}
</style>
