<template>
	<view class="pages">
		<u-navbar
			title="收货地址"
			:autoBack="true"
			 :safeAreaInsetTop="true"
		>
		</u-navbar>
		<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="receiveName" borderBottom ref="receiveName">
						<u--input v-model="model.receiveName" placeholder="请输入收货人姓名" border="none"></u--input>
					</u-form-item>
					<u-form-item label="手机号码" prop="receivePhone" borderBottom ref="receivePhone">
						<u--input v-model="model.receivePhone" placeholder="请输入收货人手机号码" border="none"></u--input>
					</u-form-item>
					<u-form-item label="所在地区" prop="receiveAdress" borderBottom ref="receiveAdress">
						<view class="1" @tap="showRegionPicker">
							<u--input v-model="model.receiveAdress" placeholder="省市区县、乡镇等" border="none"></u--input>
						</view>
					</u-form-item>
					<u-form-item label="详细地址" prop="address" borderBottom ref="address">
						<u--textarea v-model="model.address" 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="model.ifDefault" :activeValue="Number(1)" :inactiveValue="Number(0)" size="24" activeColor="#5ac725" inactiveColor="#ddd" @change="defaultChange"></u-switch>
				</view>
			</view>
			<view class="full-btn" @click="submit">保存</view>
		</view>
		<map v-if="showMap" />
	</view>
</template>

<script>
	import map from "./map.vue";
	export default {
		components:{
			map
		},
		data() {
			return {
				fromPage:'',
				saveType:'',
				id:'',
				showMap:false,
				model: {
					receiveName:'',
					receivePhone:'',
					receiveAdress:'',
					address:'',
					ifDefault:0,
				},
				rules:{
					'receiveName': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					receivePhone: [
						{
							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'],
						}
					],
					'receiveAdress': {
						type: 'string',
						required: true,
						message: '请选择所在地区',
						trigger: ['blur', 'change']
					},
					'address': {
						type: 'string',
						required: true,
						message: '请填详细地址',
						trigger: ['blur', 'change']
					},
				}
			}
		},
		onShow() {		
		},
		onLoad(page) {
			console.log('page',page);
			this.saveType = page.type;
			this.id = page.id;
			if(this.id){
				this.getAddrById(this.id)
			}
			this.fromPage = page.from;
			this.backUrl = page.backUrl;
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			closePicker(){
				this.show = false
			},
			defaultChange(e){
				console.log('defaultChange',e)
			},
			showRegionPicker() {
				// this.showMap = true;
				uni.$u.route('/center/map');
			},
			getAddrById(id){
				this.$u.api.getAddrById({id:id}).then(res=>{
					console.log('getAddrByIdres',res.data);
					const { receiveName, receivePhone, receiveAdress, address, ifDefault } = res.data;
					Object.assign(this.model, { receiveName, receivePhone, receiveAdress, address, ifDefault });
				}).catch(err=>{
					console.log('getAddrById',err);
				})
			},
			submit() {
				this.$refs.uForm.validate().then(res => {
					console.log('this.model',this.model);
					// uni.$u.toast('校验通过')
					if(this.saveType=='edit'){
						this.editAddr()
					}else{
						this.addAddr()
					}
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			addAddr(){
				this.$u.api.addAddr(this.model).then(res=>{
					// console.log('res',res);
					if(this.backUrl){
						uni.$u.route(this.backUrl, {
							// type: 'redirectTo'
						});
					}else{
						uni.$u.route('/center/addrlist', {
							type: 'redirectTo'
						});
					}
					
				}).catch(err=>{
					console.log('addAddr',err.data);
				})
			},
			editAddr(){
				this.model.id = this.id;
				this.$u.api.editAddr(this.model).then(res=>{
					// console.log('res',res);
					uni.$u.route('/center/addrlist', {
						type: 'redirectTo'
					});
				}).catch(err=>{
					console.log('addAddr',err.data);
				})
			}
		}
	}
</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>