<template>
	<view>
		<!-- 顶部操作条 -->
		<cu-custom :bgColor="'bg-main-color'" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">编辑地址</block>
		</cu-custom>
		<!-- 地址信息form -->
		<view class="bg-white margin-top-sm">
			<form @submit="submit">
				<view class="cu-form-group">
					<view class="title">联系人</view>
					<input name="person2Contact" :value="formData.person2Contact"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">手机号码</view>
					<input name="phone" :value="formData.phone"></input>
				</view>
				<!-- #ifndef H5 || APP-PLUS || MP-ALIPAY -->
				<view class="cu-form-group">
					<view class="title">地址选择</view>
					<picker :mode="'multiSelector'" @change="regionChange" :value="multiIndex" :range-key="'name'" :range="areaList">
						<view class="picker">
							{{areaList[0][multiIndex[0]].name}}，{{areaList[1][multiIndex[1]].name}}，{{areaList[2][multiIndex[2]].name}}
						</view>
					</picker>
				</view>
				<!-- #endif -->
				<view class="cu-form-group">
					<view class="title">详细地址</view>
					<input name="address" :value="formData.address"></input>
				</view>
				<view class="cu-form-group margin-top margin-bottom-with-bar">
					<view class="title">默认地址</view>
					<switch class="main-color radius" @change="isDefaultChange" :class="formData.isDefault?'checked':''"
						:checked="formData.isDefault?true:false" name="isDefault" :value="formData.isDefault"></switch>
				</view>
				<!-- 底部操作栏 -->
				<view class="cu-bar tabbar border shop fixed-bottom-bar">
					<button class="cu-btn bg-main-color long-btn margin-lr-sm shadow-blur" form-type="submit">保存</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				areaList: [],
				multiIndex: [0, 0, 0],
				formData: {
				},
				mode: 1 // 1为新增，0
			}
		},
		onLoad(options) {
			let params = JSON.parse(decodeURIComponent(options.params));
			this.mode = params ? params.mode : this.mode;
			console.log(this.mode)
			if (this.mode === 0) {
				// 修改
				this.fillForm(params.addressInfo);
			}
			this.loadData();
		},
		methods: {
			async loadData() {
				this.areaList = await this.$api.data('areaList');
			},
			fillForm(addressInfo) {
				this.formData = addressInfo && Object.keys(addressInfo).length > 0 ? addressInfo : this.formData;
			},
			regionChange(e) {
				this.multiIndex = e.detail.value;
				let chosenArea = [];
				for(let i = 0; i < this.areaList.length; i++) {
					chosenArea.push(this.areaList[i][this.multiIndex[i]]);
				}
				this.formData.area = chosenArea;
			},
			isDefaultChange(e) {
				this.formData.isDefault = e.detail.value;
			},
			validateForm(addressInfo) {
				let valid = Boolean(addressInfo.person2Contact) &&
					Boolean(addressInfo.phone) &&
					Boolean(addressInfo.address);
				
				if (!valid) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none',
						mask: true
					})
				} else if (!this.$validate.validContactNum(addressInfo.phone)) {
					valid = false;
					uni.showToast({
						title: '联系号码格式错误',
						icon: 'none',
						mask: true
					})
				}
				return valid;
			},
			submit(e) {
				const confirmFormData = Object.assign({}, this.formData, e.detail.value)
				let formValid = this.validateForm(confirmFormData);
				if (formValid) {
					uni.showToast({
						title: '保存成功',
						icon: 'success',
						mask: true
					})
				}
			}
		}
	}
</script>

<style>
</style>
