<template>
	<view class="content">
		<u-form :model="form" ref="uForm" :label-width="140" :error-type="['toast']">
			<u-form-item label="姓名" required prop="name">
				<u-input placeholder="请输入姓名" :maxlength="30" v-model="form.name" />
			</u-form-item>
			<u-form-item label="电话" required prop="mobile">
				<u-input placeholder="请输入电话" :maxlength="11" type="number" v-model="form.mobile" />
			</u-form-item>
			<u-form-item label="省市区" required prop="area">
				<u-input v-model="form.area" placeholder="请选择省市区" @click="show = true" type="select" />
				<u-picker v-model="show" @confirm="chooseArea" mode="region"></u-picker>
			</u-form-item>
			<u-form-item label="详细地址" required prop="address">
				<u-input placeholder="请输入收货详细地址" :maxlength="100" v-model="form.address" />
			</u-form-item>
		</u-form>
		<view class="buttons">
			<u-button type="primary" @click="submit">提交</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				form: {
					name: '',
					mobile: '',
					area: '',
					address: ''
				},
				rules: {
					name: [
						{ 
							required: true, 
							message: '请输入姓名', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					],
					mobile: [
						{ 
							required: true, 
							message: '请输入电话', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					],
					area: [
						{
							required: true, 
							message: '请选择省市区', 
							trigger: ['change','blur']
						}
					],
					address: [
						{
							required: true, 
							max: 100, 
							message: '请输入详细地址，最多100个字', 
							trigger: ['change','blur']
						}
					]
				}
			};
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
					} else {
						console.log('验证失败');
					}
				});
			},
			// 选择地址
			chooseArea(e){
				console.log(e)
				this.form.area = e.province.label + '-' + e.city.label + '-' + e.area.label
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	};
</script>

<style lang="scss">
	.content{
		padding: 0 50upx;
		background: #FFFFFF;
		width: 100%;
		.buttons{
			padding: 80upx 100upx;
		}
	}
</style>
