<template>
	<view class="content">
		<view class="bgi-box">
			<image src="../../static/images/bgitop.png" mode="widthFix" class="bgi-img"></image>
		</view>
		<view class="center-box">
			报名参与
		</view>
		<view class="list-box">
			<view class="list">
				<view class="list-left">
					姓名：
				</view>
				<input type="text" value="" class="list-input" placeholder="请填写您的姓名" v-model="name" />
			</view>
			<view class="list">
				<view class="list-left">
					性别：
				</view>
				<picker @change="bindPickerSex" :value="index" :range="array" class="list-input">
					<text>{{ sex ||'请选择您的性别'}}</text>
				</picker>
			</view>
			<view class="list">
				<view class="list-left">
					出生年月：
				</view>
				<picker mode="date" :value="birthday"  :start="startDate" :end="endDate" @change="bindDateChange">
					<view class="uni-input">{{ birthday ||'请选择您的出生日期'}}</view>
				</picker>
			</view>
			<view class="list">
				<view class="list-left">
					联系方式：
				</view>
				<input type="text" value="" class="list-input" placeholder="请填写您的联系方式" v-model="phone" />
			</view>
		</view>
		<view class="sub" :class="{ action: loding }" @click="!loding?join():''">
			马上报名
		</view>
	</view>
</template>

<script>
	import {
		getList
	} from '@/api/index.js';
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				loding:false,
				index: 0,
				name: '',
				sex: '',
				birthday: '',
				phone: '',
				// date: '',
				// currentDate
				getList: [],
				array:['男','女']
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			bindPickerSex: function(e) {
				this.sex = this.array[e.target.value];
				// this.index = e.target.value + 1;
			},
			async join() {
				let obj = this;
				if (obj.name == '') {
					obj.$api.msg('请输入您的姓名');
					return;
				}
				if (obj.sex == '') {
					obj.$api.msg('请填写您的性别');
					return;
				}
				if (obj.birthday == '') {
					obj.$api.msg('请填写您的年月日');
					return;
				}
				const reg = /^(\+?0?86-?)?1[\d]\d{9}$/;
				if (obj.phone == !reg.test(obj.phone)) {
					obj.$api.msg('请填写正确的手机号码');
					return;
				}
				obj.loding = true;
				getList({
					name: obj.name,
					sex: obj.sex,
					birthday: obj.birthday,
					phone: obj.phone,
				}).then(e => {
					console.log(e)
					uni.navigateTo({
						url: '../joinSuc/joinSuc'
					});
				})
			},
			// 选择日期
			bindDateChange: function(e) {
				let obj = this;
				
			let  time1	= new Date(e.target.value).getTime();
			let time2 = new Date().getTime();
			let time3 = time2 - time1;
		    let time4 = 1000*60*60*24*365*18;
			let time5 = 1000*60*60*24*365*45;
			if(time3<time4||time3>time5){
				uni.showModal({
					content: '捐献年龄必须在18至45周岁!',
					success: res => {
						if (res.confirm) {
					     return
						}
					}
				});
			}else{
				obj.birthday = e.target.value;
			}
		
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			}
		}
	}
</script>

<style lang="scss">
	.content {
		line-height: 1;
		background-color: #FFE1E1;
		padding-bottom: 40rpx;

		.bgi-box {
			width: 750rpx;

			.bgi-img {
				width: 750rpx;
			}
		}

		.center-box {
			width: 405rpx;
			background: linear-gradient(0deg, #E73931 0%, #FF4F4F 100%);
			border-radius: 10rpx;
			margin: -250rpx auto 0;
			z-index: 1;
			position: relative;
			line-height: 66rpx;
			text-align: center;
			font-size: 30rpx;
			font-weight: 500;
			color: #FFFFFF;
		}

		.list-box {
			width: 685rpx;
			position: relative;
			background: #FFFFFF;
			border-radius: 29px;
			margin: -33rpx auto 0;
			font-size: 30rpx;
			font-weight: 400;
			color: #E63931;
			padding: 60rpx 0rpx 30rpx;

			.list {
				display: flex;
				align-items: center;
				width: 542rpx;
				height: 83rpx;
				border: 1px solid #E63931;
				margin: 0 auto 30rpx;
				padding: 0 40rpx;
				border-radius: 83rpx;
                .uni-input{
					color: #FF9797;
				}
				.list-left {
					width: 150rpx;
				}

				.list-input {
					flex: 1;
                    color: #FF9797;
					.input-placeholder {
						color: #FF9797;
					}
				}
			}

		}

		.sub {
			width: 660rpx;
			line-height: 100rpx;
			text-align: center;
			background: linear-gradient(0deg, #C90F1B, #F14D33);
			border-radius: 50rpx;
			font-size: 36rpx;
			font-weight: 400;
			color: #FFFFFF;
			margin: 40rpx auto 0;
			&.action{
				background-color: #999999;
			}
		}
	}
</style>
