<template>
	<view class="add-address">
		<u-navbar :title="type === 'add' ? '新增地址' : '修改地址'" autoBack bg-color="#fff0e7" placeholder></u-navbar>
		<view class="add-content">
			<scroll-view :scroll-y="true" style="height: 100%;">
				<u--form labelPosition="left" errorType="toast" labelWidth="auto" :model="formData" :rules="rules" ref="uForm">
					<view class="v-card">
						<view class="v-card-title">
							<view class="v-radio">
								<u-radio-group v-model="formData.type" placement="row" inactiveColor="#E39B46" activeColor="#E39B46">
									<u-radio shape="circle" label="收件地址" :name="0"></u-radio>
									<u-radio shape="circle" label="寄件地址" :name="1"></u-radio>
								</u-radio-group>
							</view>
							<view class="v-card-title-right" @click="$Router.push('/pages/queryAddr/queryAddr')"
								v-if="formData.type === 0">
								看守所/监狱地址查询 <u-icon style="margin-left: 10rpx;" name="arrow-right" color="#E39B46" size="20rpx"></u-icon>
							</view>
						</view>
						<template v-if="formData.type === 0">
							<u-form-item label="收件人姓名" prop="name">
								<u--input v-model="formData.name" border="none" placeholder="请输入收件人姓名" inputAlign="right"></u--input>
							</u-form-item>
							<u-form-item label="收件人电话" prop="phone">
								<u--input v-model="formData.phone" border="none" placeholder="请输入收件人电话" inputAlign="right"></u--input>
							</u-form-item>
							<u-form-item label="详细地址" prop="address">
								<u--input v-model="formData.address" border="none" placeholder="请输入收邮件人详细地址"
									inputAlign="right"></u--input>
							</u-form-item>
						</template>
						<template v-else>
							<u-form-item label="寄件人姓名" prop="name">
								<u--input v-model="formData.name" border="none" placeholder="请输入寄件人姓名" inputAlign="right"></u--input>
							</u-form-item>
							<u-form-item label="寄件人电话" prop="phone">
								<u--input v-model="formData.phone" border="none" placeholder="请输入寄件人电话" inputAlign="right"></u--input>
							</u-form-item>
							<u-form-item label="详细地址" prop="address">
								<u--input v-model="formData.address" border="none" placeholder="请输入寄邮件人详细地址"
									inputAlign="right"></u--input>
							</u-form-item>
						</template>
					</view>
				</u--form>
			</scroll-view>
		</view>

		<view class="address-footer">
			<u-button class="u-button" :plain="false" @click="saveAddress">保存</u-button>
		</view>

	</view>
</template>

<script>
	import {
		addAddress,
		editAddress
	} from '@/request/yyf.js';
	export default {
		data() {
			return {
				checkboxValue: [],
				formData: {
					type: 0,
					phone: '',
					name: '',
					address: '',
					id: ''
				},
				
				type: 'add', // add edit
			}
		},
		computed: {
			rules() {
				let { type } = this.formData;
				return {
					phone: {
						required: true,
						pattern: /^1[3-9]\d{9}$/,
						message: type === 0 ? '请输入正确的收件人电话' : '请输入正确的寄件人电话',
						trigger: ['blur', 'change']
					},
					name: {
						required: true,
						message: type === 0 ? '请输入收件人姓名' : '请输入寄件人姓名',
						trigger: ['blur', 'change']
					},
					address: {
						required: true,
						message: type === 0 ? '请输入收邮件人详细地址' : '请输入寄邮件人详细地址',
						trigger: ['blur', 'change']
					},
				}
			}
		}, 
		onLoad() {
			// console.log(this.$Route.query);
			let query = this.$Route.query;
			if (query.id) {
				this.type = 'edit';
				Object.keys(this.formData).forEach(key => {
					this.formData[key] = query[key];
				});
			}
			uni.$on('addAddress', (data)=> {
				this.formData.address = data;
			})
		},
		methods: {
			saveAddress() {
				this.$refs['uForm'].validate().then(res => {
					console.log('校验通过');
					if(this.type === 'add') {
						this.submitAddAddress();
					} else {
						this.submitEditAddress();
					}
				}).catch(errors => {
					console.log(errors);
					// uni.$u.toast('校验失败');
				})
			},
			async submitAddAddress() {
				const {
					data
				} = await addAddress(this.formData);
				if (data.code === 1) {
					uni.$u.toast('添加成功');
					uni.$emit('refresh');
					this.$Router.back();
				}
			},
			async submitEditAddress() {
				const {
					data
				} = await editAddress(this.formData);
				if (data.code === 1) {
					uni.$u.toast('修改成功');
					uni.$emit('refresh');
					this.$Router.back();
				}
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules);
		},
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-radio {
		margin-right: 20rpx;
	}

	.add-address {
		box-sizing: border-box;
		height: 100vh;
		display: flex;
		flex-direction: column;
		padding: 30rpx 30rpx 40rpx;

		.add-content {
			flex: 1;
			overflow: hidden;

			.v-card {
				margin-bottom: 20rpx;
				padding: 30rpx;
				background: #FFFFFF;
				border-radius: 40rpx 40rpx 40rpx 40rpx;

				.v-card-title {
					padding-bottom: 50rpx;
					font-size: 34rpx;
					font-family: OPPOSans-bold, OPPOSans;
					font-weight: bold;
					color: #333333;

					.v-card-title-right {

						margin-top: 30rpx;
						display: flex;
						align-items: center;
						justify-content: flex-end;
						font-size: 30rpx;
						font-family: OPPOSans-Medium, OPPOSans;
						font-weight: 500;
						color: #E39B46;
					}
				}

				.checkbox {
					padding-top: 50rpx;
					display: flex;
					justify-content: flex-end;
				}
			}
		}

		.address-footer {
			padding-top: 30rpx;

			.u-button {
				height: 98rpx;
				border: 3rpx solid #FFE3D6;
				background: linear-gradient(178deg, #E8B648 0%, #E08745 100%);
				border-radius: 12rpx;
				font-size: 30rpx;
				line-height: 30rpx;
				font-family: OPPOSans-Medium, OPPOSans;
				font-weight: 500;
				color: #fff;
			}
		}
	}
</style>