<template>
	<view class="personalData">
		<view class="form">
			<uni-forms :modelValue="formData" label-position="top">
				<uni-forms-item label="昵称" name="nickname">
					<uni-easyinput placeholderStyle="color: #999; font-size:30rpx " type="text"
						v-model="formData.nickname" placeholder="请输入姓名" />
				</uni-forms-item>

				<!--地区选择-->
				<uni-forms-item label="地区" name="region">
					<pickerAddress @change="change">
						<view class="selectType">
							<view class="uni-input">
								<text style="color: #999999 ; font-size: 30rpx;" v-if="region==''">所在地区</text>
								<text style="font-size: 30rpx;" v-if="region!=''">{{region}}</text>
							</view>
							<image src="/static/icon/right.png"
								style="width: 14rpx;height: 24rpx;position: absolute;top:24rpx;right: 24rpx;">
							</image>
						</view>
					</pickerAddress>
				</uni-forms-item>

				<uni-forms-item label="性别" name="sex">
					<view class="sex">
						<picker mode="selector" :value="sex_text" :range='sexSelect' @change="bindSexChange">
							<view class="uni-input" @change="bindSexChange">
								<text v-if="formData.sex==''||formData.sex==null"
									style="color: #999999; font-size: 30rpx; ">选择性别</text>
								<text v-if="formData.sex!=''">{{sex_text}}</text>
							</view>
							<image src="/static/icon/right.png" @change="bindSexChange"
								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 maxlength="11" placeholderStyle="color: #999; font-size:30rpx " type="number"
						v-model="formData.mobile" placeholder="输入手机号" @blur="getPhone" />
				</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" @change="bindDateChange">
								<text v-if="formData.birthday==''||formData.birthday==null"
									style="color: #999999; font-size: 30rpx; ">选择生日</text>
								<text v-if="formData.birthday!=''">{{formData.birthday}}</text>
							</view>
							<image src="/static/icon/right.png" @change="bindDateChange"
								style="width: 14rpx;height: 24rpx;position: absolute;top:22rpx;right: 30rpx;"></image>
						</picker>
					</view>
				</uni-forms-item>

				<view class="keep-msg" @click="saveBtn">
					<text>保存信息</text>
				</view>

			</uni-forms>
		</view>

	</view>
</template>

<script>
	import pickerAddress from '@/uni_modules/hu-pickerAddress/hu-pickerAddress.vue'
	export default {
		components: {
			pickerAddress
		},
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				formData: {
					nickname: '',
					sex: '',
					birthday: '',
					mobile: '',
					area_id: ''
				},
				sexSelect: ['男', '女', '其他'],
				sex_text: '',
				region: '',
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},

		onLoad() {
			this.formData.nickname = this.$store.getters.userInfo.nickname
			//获取用户信息
			this.getUserInfo()
		},

		methods: {
			//获取用户信息
			getUserInfo(){
				this.$api.my.userInfo().then(res=>{
					console.log(res.data,'---->用户信息');
					let obj ={}
					obj.nickname = res.data.nickname
					obj.mobile = res.data.mobile
					obj.sex = res.data.sex
					obj.birthday = res.data.birthday
					obj.area_id = res.data.area_id
					this.region = res.data.region
					this.formData = obj
					if(this.formData.sex == 0){
						this.sex_text = '未知'
					}else if(this.formData.sex == 1){
						this.sex_text = '男'
					}else if(this.formData.sex == 2){
						this.sex_text = '女'
					}
					this.$store.getters.userInfo.nickname = obj.nickname
				})
			},
		
		
			//获取手机号
			getPhone() {
				let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/
				if (!phoneReg.test(this.formData.mobile)) {
					uni.showToast({
						title: '请输入合法手机号',
						icon: "none"
					})
					return
				}
			},
			change(ret) {
				this.region = ret.data.join('-')
				this.formData.area_id = ret.code[2]

			},
			bindDateChange: function(e) {
				this.formData.birthday = e.detail.value
			},
			bindSexChange: function(e) {
				if (e.detail.value == 0) {
					this.sex_text = '男'
					this.formData.sex = 1
				} else if (e.detail.value == 1) {
					this.sex_text = '女'
					this.formData.sex = 2
				} else {
					this.sex_text = '其他'
					this.formData.sex = 0
				}
			},
			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;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},

			//保存信息
			saveBtn() {
				if (this.formData.nickname == '') {
					uni.showToast({
						title: '请输入昵称',
						icon: "none"
					})
					return
				}
				if (this.formData.area_id == '') {
					uni.showToast({
						title: '请输入地区',
						icon: "none"
					})
					return
				}
				if (this.formData.sex == '') {
					uni.showToast({
						title: '请输入性别',
						icon: "none"
					})
					return
				}
				if (this.formData.mobile == '') {
					uni.showToast({
						title: '请输入手机号',
						icon: "none"
					})
					return
				}
				if (this.formData.birthday == '') {
					uni.showToast({
						title: '请输入生日',
						icon: "none"
					})
					return
				}
				this.$api.my.refreshPersonData(this.formData).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: '保存成功',
							icon: "none"
						})
						setTimeout(()=>{
							uni.switchTab({
								url:'/pages/my/my'
							})
						},500)
						// let personData = JSON.parse(JSON.stringify(this.formData))
						// personData.region = this.region
						// personData.sex = this.sex_text
						// console.log(personData,'---->personDate');
						// this.$store.dispatch('user/person',personData)
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})

			},


		}
	}
</script>

<style lang="scss" scoped>
	$pageColor:#F9F9F9;
	$bgColor:#FFFFFF;

	.personalData {
		height: 100%;
		background: $pageColor;
	}

	.selectType {
		width: 100%;
		height: 74rpx;
		background: #FFFFFF;
		border-radius: 8rpx;
		border: 2rpx solid #EAEAEA;
		display: flex;
		align-items: center;
		padding-left: 20rpx;
		box-sizing: border-box;
		position: relative;
	}



	.keep-msg {
		width: 100%;
		height: 92rpx;
		background: linear-gradient(270deg, #FF6200 0%, #FF9342 100%);
		border-radius: 12rpx;
		display: flex;
		align-items: center;
		justify-content: center;

		text {
			font-weight: bold;
			color: #FFFFFF;
			font-size: 30rpx;
		}
	}

	.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;
		}
	}

	::v-deep .uni-forms-item__label {
		font-weight: bold;
		color: #333333;
		font-size: 32rpx;
	}

	::v-deep .uni-easyinput__content-input {
		color: #333333;
		font-size: 32rpx;
	}

	::v-deep .is-input-border {
		border-color: #e5e5e5 !important;
		background-color: #fff !important;
	}

	::v-deep .uni-icons {
		color: #c0c4cc !important;
		font-size: 24px !important;
	}
</style>
