<template>
	<view class="personalData">
		<view class="form">
			<uni-forms :modelValue="formData" label-position="top">
				<uni-forms-item label="昵称" name="nickname">
					<uni-easyinput type="text" v-model="formData.nickname" placeholder="请输入姓名" />
				</uni-forms-item>
				<uni-forms-item label="地区" name="region">
					<uni-easyinput type="text" v-model="formData.region" placeholder="选择地区" />
				</uni-forms-item>
				<uni-forms-item label="性别" name="sex">
					<view class="sex">
						<picker mode="selector" :value="formData.sex" :range='sexSelect'
							@change="bindSexChange">
							<view class="uni-input" >
								<text v-if="formData.sex==''||formData.sex==null"
									style="color: #999999 ;">选择性别</text>
								<text v-if="formData.sex!=''">{{formData.sex}}</text>
							</view>
							<image src="/static/icon/right.png"
								style="width: 14rpx;height: 24rpx;position: absolute;top:22rpx;right: 30rpx;"></image>
						</picker>
					</view>
				</uni-forms-item>
				<uni-forms-item label="手机号" name="phone">
					<uni-easyinput type="number" v-model="formData.phone" placeholder="输入手机号" />
				</uni-forms-item>
				<uni-forms-item label="生日" name="birthday" >
					<view class="birthday">
						<picker mode="date" :value="formData.birthday" :start="startDate" :end="endDate"
							@change="bindDateChange">
							<view class="uni-input" >
								<text v-if="formData.birthday==''||formData.birthday==null"
									style="color: #999999 ;">选择生日</text>
								<text v-if="formData.birthday!=''">{{formData.birthday}}</text>
							</view>
							<image src="/static/icon/right.png"
								style="width: 14rpx;height: 24rpx;position: absolute;top:22rpx;right: 30rpx;"></image>
						</picker>
					</view>
				</uni-forms-item>
			</uni-forms>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				formData: {
					nickname:'',
					sex: '',
					birthday: '',
					region:'',
					phone:'',
				},
				sexSelect:['男','女','其他']
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods: {
			bindDateChange: function(e) {
				this.formData.birthday=e.detail.value
			},
			bindSexChange:function(e){
				if(e.detail.value==0){
					this.formData.sex='男'
				}else if(e.detail.value==1){
					this.formData.sex='女'
				}else{
					this.formData.sex='其他'
				}
			},
			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" scoped>
	$pageColor:#F9F9F9;
	$bgColor:#FFFFFF;
	
	.personalData {
		height: 100%;
		background: $pageColor;
	}

	.form {
		padding: 24rpx 48rpx 0 50rpx;
		.birthday,
		.sex{
			width: 652rpx;
			height: 74rpx;
			background: #FFFFFF;
			border-radius: 8rpx;
			border: 1px solid #EAEAEA;
			display: flex;
			align-items:center;
			padding-left: 20rpx; 
			box-sizing: border-box;
			position: relative;
		}
	}
</style>
