<template>
	<view :class="[AppTheme]" class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.name" placeholder="收货人姓名"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">电话</text>
			<input class="input" type="getPhoneNumber" v-model="addressData.mobile" placeholder="收货人手机号码"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">地址</text>
			<view style="width: 70%;">
				<u--input border="none"  v-model="addressData.address" placeholder="收货地址"
					suffixIconStyle="color: #909399"></u--input>
			</view>

			<view v-if='config.app.position==1' @click="chooseLocation"
				style="width: 14%;display: flex;justify-content: flex-end;">
				<u-icon name="map-fill" size="20"></u-icon>
			</view>

		</view>

		<view class="row b-b">
			<text class="tit">小区</text>
			<input class="input" type="text" v-model="addressData.community" placeholder="收货人小区"
				placeholder-class="placeholder" />
		</view>
		<view class="row default-row">
			<text class="tit">设为默认</text>
			<u-switch v-model="addressData.is_default" size="20" :activeColor="primary" asyncChange
				@change="switchChange" />
			</u-switch>
		</view>
		<button class="add-btn bg-linear-gradient" @click="confirm">提交</button>
	</view>
</template>

<script>
	import api from "../../../api/address/address.js"
	export default {
		data() {
			return {
				primary: this.$theme.primary,
				addressData: {
					id: '',
					name: '',
					mobile: '',
					address: '',
					community: '',
					is_default: false,
				},
				type: 0,
				settingFile: getApp().globalData.siteinfo,
				config: null,
			}
		},
		onLoad(option) {
			let title = '新增收货地址';
			this.config = this.$config;
			if (option.type == 1) {
				title = '编辑收货地址'
				let data = JSON.parse(option.data)
				this.addressData = data
				this.addressData.is_default = !!(data.is_default * 1)
			}
			this.type = option.type;
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			switchChange(e) {
				if (e) {
					this.addressData.is_default = true;
				} else {
					this.addressData.is_default = false;
				}
			},
			//地图选择地址
			chooseLocation() {
				let that = this
				// #ifdef MP-WEIXIN || APP-PLUS
				that.$until.chooseLocation(function(res) {
					that.addressData.address = res.address;
				});
				// #endif
			},

			//提交
			confirm() {
				let that = this;
				let data = that.addressData;
				if (!data.name) {
					that.$api.msg('请填写收货人姓名');
					return;
				}
				var isPhone = /^1[3456789]\d{9}$/;
				var isMob = /^((0\d{2,3})-)?(\d{7,8})$/;
				if (!isMob.test(data.mobile) && !isPhone.test(data.mobile)) {
					that.$api.msg('请输入正确的联系电话');
					return;
				}
				if (!data.address) {
					that.$api.msg('请填写收货地址');
					return;
				}
				const obj = {
					id: that.addressData.id,
					address: that.addressData.address,
					name: that.addressData.name,
					mobile: that.addressData.mobile,
					community: (that.addressData.community == '' || that.addressData.community == null) ? '' : that
						.addressData.community,
					type: that.type,
					is_default: +that.addressData.is_default
				}
				api.addressSet(obj).then(res => {
					console.log(res)
					if (res.status == 200) {
						that.$api.msg(`地址${that.type==1 ? '修改': '添加'}成功`);
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 120upx;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.icon-shouhuodizhi {
			font-size: 36upx;
			color: $font-color-light;
		}
	}

	.default-row {
		margin-bottom: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		border-radius: 10upx;
		// box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
