<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+'Topping.png'"></image>
			</view>
		</view>
		
		
		<view>
			<view v-if="true" class="selectBox">
				<view class="title">
					选择职位
				</view>
				
				<view class="selectItem" v-for="(item ,index) in jobList" :class="item.recruit_id == selectType ? 'sType' : 'dType'"
				 :key="index" @click="clickItem(item.recruit_id)">
					{{ item.recruit_title }}
				</view>
				
				<view style="margin-bottom: 40upx;align-items: center;" class="flex">
					<view style="margin-right: 20upx;font-size: 28upx;">使用天数:</view>
					<input class="inputNum" type="number" v-model="useDay" placeholder-style="color: #CACACA"  placeholder="请输入使用天数" />
				</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" type="number" @input="changeOrderPrice" v-model="diySelect" placeholder-style="color: #CACACA"  placeholder="请输入购买天数" />
			</view>
			
			<view class="selectDay flex jcsb">
				<view class="dayItem" :class="item == selectOption ? 'sType' : 'dType'" v-for="(item) in selectOptLs" :key="item" @click="clickOption(item)">
					{{item}}
				</view>
			</view>
		</view>
		
		<view class="submitBtn" @click="buy">
			立即支付   ¥{{selectOptPrice}}/{{selectOptTxt}}
		</view>
		
		<view style="height: 50upx;"></view>
	</view>
</template>

<script>
	let _this;
	import App from '@/common/js/app.js';
	export default {
		data() {
			return {
				jobList: [],
				selectType: 1,
				useDay: '',
				selectOption: 1,
				selectOptTxt: '一天',
				selectOptPrice: '100.00',
				selectOptLs: {},
				diySelect: '',
				diyUnit: '',
				diyPrice: '',
				dayNum: 0,
				rights_id: null,
				lock_1: false,
				lock_2: false
			};
		},
		onLoad(e){
			_this = this;
			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)
			},
			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){
				this.selectType = val
			},
			clickOption(val){
				if(typeof val == 'string'){
					this.selectOptTxt = val;
					this.selectOption = val;
					for(let key in this.selectOptLs){
						if(this.selectOptLs[key]==val){
							this.selectOptPrice = key * this.diyPrice;
						}
					}
					return;
				}
			},
			getData(){
				App._post_form(
					`&p=rights&do=getRightsTypeInfo`, { rights_id: this.rights_id },
					res => {
						// console.log(res);
						this.dayNum = res.data.remaining_amount;
						this.diyUnit = res.data.amount_unit;
						this.diyPrice = res.data.price;
						
						this.jobList = res.data.filter_data.recruit;
						
						this.selectOptLs = res.data.num_data;
						for(let key in this.selectOptLs){
							this.selectOptPrice = key * res.data.price;
							this.selectOption = this.selectOptLs[key];
							this.selectOptTxt = this.selectOptLs[key];
							return;
						}
					},
					() => {},() => {}
				);
			},
			changeOrderPrice(e){
				// console.log(e.detail.value, this.diySelect,  +(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.selectOption = '';
				}else{
					for(let key in this.selectOptLs){
						this.selectOptPrice = (key * this.diyPrice).toFixed(2);
						this.selectOption = this.selectOptLs[key];
						this.selectOptTxt = this.selectOptLs[key];
						return;
					}
				}
			},
			useEquity(){
				let data = {
					rights_id: this.rights_id,
					position: JSON.stringify([{recruit_id: this.selectType,num:this.useDay}])
				}
				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.selectOptLs){
					if(this.selectOptLs[key]==this.selectOption){
						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" scoped>
.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: 50%;
		height: 300upx;
		// border: 1px solid red;
		align-items: center;
		.txt{
			width: 100%;
			// padding: 0% ;
			padding-left: 40upx;
			line-height: 55upx;
		}
		.txtItem{
			font-weight: bold;
			font-size: 40upx;
			font-family: 'PingFangSC-semiBold';
			color: #444444;
			margin-bottom: 10upx;
		}
	}
	.rightBox{
		width: 42%;
		height: 300upx;
		// border: 1px solid blue;
		display: flex;
		align-items: center;
		image{
			width: 240upx;
			height: 150upx;
			// margin-top: -30upx;
		}
	}
}

.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;
		margin-bottom: 40upx;
		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%;
}
.sType{
		color: #6DADFD;
		background-color: #F2F8FF;
	}
	.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;
	text-align: center;
	line-height: 90upx;
	color: #64A8FD;
	font-size: 30upx;
}
</style>
