<template>
	<view class="container">
		<uni-nav-bar shadow left-icon="left" fixed leftText="返回" dark rightIcon="compose" rightText="编辑" @clickRight="editTrue" title="个人资料"/>
		<view class="example">
			<!-- 基础表单校验 -->
			<uni-forms ref="valiForm" :rules="rules" :modelValue="formData">

				<uni-forms-item label="签名">
					<uni-easyinput type="textarea"  :disabled="isDisabled" v-model="formData.signature"
						placeholder="个性签名" />
				</uni-forms-item>
				<uni-forms-item label="昵称" name="nickname">
					<uni-easyinput v-model="formData.nickname" placeholder="请输入昵称" :disabled="isDisabled" />
				</uni-forms-item>
				<uni-forms-item label="性别" name="sex">
					<uni-data-checkbox v-model="formData.sex" :localdata="sexs" :disabled="isDisabled" />
				</uni-forms-item>
				<uni-forms-item label="生日" name="birthday">
					<uni-datetime-picker type="date" return-type="timestamp" v-model="formData.birthday"
						:disabled="isDisabled" />
				</uni-forms-item>
				<uni-forms-item label="身高" name="height">
					<uni-easyinput v-model="formData.height" type="number" placeholder="身高" :disabled="isDisabled" />
				</uni-forms-item>

				<uni-forms-item label="所在地" name="location">
					<uni-data-picker v-model="formData.location" popup-title="请选所在地区" :readonly="isDisabled"></uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="学历" name="education">
					<uni-data-select v-model="formData.education" :localdata="educations" :disabled="isDisabled">
					</uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="职业" name="job">
					<uni-data-picker v-model="formData.job" popup-title="请选择班级" :readonly="isDisabled"></uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="企业" name="company">
					<uni-easyinput v-model="formData.company" type="text" placeholder="请输入姓名" :disabled="isDisabled" />
				</uni-forms-item>
				<uni-forms-item label="家乡" name="hometown">
					<uni-data-picker v-model="formData.hometown" popup-title="请选择班级" :readonly="isDisabled"></uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="简介" name="introduction">
					<uni-easyinput type="textarea" v-model="formData.introduction" placeholder="请输入自我介绍"
						:disabled="isDisabled" />
				</uni-forms-item>


				<uni-forms-item label="月收入" name="income">
					<uni-data-select v-model="formData.income" :localdata="educations" :disabled="isDisabled">
					</uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="婚姻" name="marriage" >
					<uni-data-select :localdata="educations" :disabled="isDisabled">
					</uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="房产" name="house">
					<uni-data-picker popup-title="请选择班级" :readonly="isDisabled"></uni-data-picker>
				</uni-forms-item>

			</uni-forms>
			<button type="primary" @click="submit('valiForm')">提交</button>
		</view>



	</view>
</template>
<script>
	export default {
		data() {
			return {
				isDisabled: true,
				// 基础表单数据
				formData: {
					signature:'',
					nickname: '',
					sex: 1,
					birthday: 0,
					height:178,
					location:"",
					education:"",
					job:"",
					company:"",
					hometown:"", 
					introduction: '',
					income:"",
					marriage:"",
					house:"",  
				},


				educations: [{
						value: 0,
						text: "篮球"
					},
					{
						value: 1,
						text: "足球"
					},
					{
						value: 2,
						text: "游泳"
					},
				],
				// 单选数据源
				sexs: [{
					text: '男',
					value: 0
				}, {
					text: '女',
					value: 1
				}, {
					text: '保密',
					value: 2
				}],
				// 多选数据源
				hobbys: [{
					text: '跑步',
					value: 0
				}, {
					text: '游泳',
					value: 1
				}, {
					text: '绘画',
					value: 2
				}, {
					text: '足球',
					value: 3
				}, {
					text: '篮球',
					value: 4
				}, {
					text: '其他',
					value: 5
				}],
				// 分段器数据
				current: 0,
				items: ['左对齐', '顶部对齐'],
				// 校验表单数据
				valiFormData: {
					name: '',
					age: '',
					introduction: '',
				},
				// 校验规则
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					age: {
						rules: [{
							required: true,
							errorMessage: '年龄不能为空'
						}, {
							format: 'number',
							errorMessage: '年龄只能输入数字'
						}]
					}
				},
				// 自定义表单数据
				customFormData: {
					name: '',
					age: '',
					hobby: []
				},
				// 自定义表单校验规则
				customRules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					age: {
						rules: [{
							required: true,
							errorMessage: '年龄不能为空'
						}]
					},
					hobby: {
						rules: [{
								format: 'array'
							},
							{
								validateFunction: function(rule, value, data, callback) {
									if (value.length < 2) {
										callback('请至少勾选两个兴趣爱好')
									}
									return true
								}
							}
						]
					}

				}
			}
		},
		onLoad() {},
		onReady() {
			// 设置自定义表单校验规则，必须在节点渲染完毕后执行
			//this.$refs.customForm.setRules(this.customRules)
		},
		methods: {
			editTrue(){
				this.isDisabled = false;
				console.log(this.isDisabled)
			},
			onClickItem(e) {
				console.log(e);
				this.current = e.currentIndex
			},
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					console.log('success', res);
					uni.showToast({
						title: `校验通过`
					})
				}).catch(err => {
					console.log('err', err);
				})
			},
		}
	}
</script>
<style lang="scss">
	.container {
		background-color: transparent;
	}

	.example {
		padding: 15px;
		background-color: #fff;
		background-color: transparent;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}

	.uni-forms-item button {
		line-height: 36rpx;
	}

	.uni-forms-item {
		position: relative;
		display: flex;
		margin-bottom: 10rpx;
	}
</style>
