<template>
	<view class="pages">
		<view class="page-wrap">
			<view class="view-wrap">
				<u--form
					labelPosition="left"
					:model="model"
					:rules="rules"
					ref="uForm"
					labelWidth="80"
					>
					<u-form-item label="姓名" prop="name" borderBottom ref="name">
						<u--input v-model="model.name" placeholder="请输入收货人姓名" border="none"></u--input>
					</u-form-item>
					<u-form-item label="手机号码" prop="mobile" borderBottom ref="mobile">
						<u--input v-model="model.mobile" placeholder="请输入收货人手机号码" border="none"></u--input>
					</u-form-item>
					<u-form-item label="所在地区" prop="phone" borderBottom ref="phone">
						<view class="1" @tap="showRegionPicker">
							<u--input v-model="model.phone" placeholder="省市区县、乡镇等" border="none"></u--input>
						</view>
					</u-form-item>
					<u-form-item label="详细地址" prop="phone" borderBottom ref="phone">
						<u--textarea v-model="model.phone" placeholder="街道楼牌号等" ></u--textarea>
					</u-form-item>
				</u--form>
			</view>
			<view class="set-default view-wrap u-flex u-row-between">
				<view class="left">
					<view class="til">设置默认地址</view>
					<view class="tip">提醒：每次下单会默认推荐该地址</view>
				</view>
				<view class="right">
					<u-switch v-model="defaultValue" size="24" inactiveColor="#ddd" @change="defaultChange"></u-switch>
				</view>
			</view>
			<view class="full-btn" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				defaultValue:false,
				model: {
					name:'',
					phone:''
				},
				rules:{
					'name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					mobile: [
						{
							required: true, 
							message: '请输入手机号',
							trigger: ['change','blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change','blur'],
						}
					]
					
				}
			}
		},
		onShow() {		
		},
		onLoad() {

		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			closePicker(){
				this.show = false
			},
			defaultChange(e){
				console.log('defaultChange',e)
			},
			showRegionPicker() {
				// this.show = true;
				uni.$u.route('/center/map');
			},
			submit() {
				this.$refs.uForm.validate().then(res => {
					uni.$u.toast('校验通过')
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			}
		}
	}
</script>
<style>
	page{
		background-color: #F5F5F5;
	}
</style>
<style lang="scss" scoped>
.set-default{
	margin-top: 20rpx;
	.til{
		font-size: 30rpx;
		font-weight: 600;
		margin-bottom: 8rpx;
	}
	.tip{
		font-size: 24rpx;
		color: #999;
	}
}
.full-btn{
	margin-top: 44rpx;
}
</style>