<template>
	<view class="content">
		<view class="listBox">
			<view class="list">
				<view class="flex listItem" >
					<view class="flex titleBox">
						<text class="title">联系人</text>
					</view>
					<view class="right flex">
						<input class="input" type="text" v-model="addressData.name" placeholder="收货人姓名"
							placeholder-class="placeholder" />
					</view>
				</view>
				<view class="flex listItem" >
					<view class="flex titleBox">
						<text class="title">手机号</text>
					</view>
					<view class="right flex">
						<input class="input" type="number" v-model="addressData.mobile" placeholder="收货人手机号码"
							placeholder-class="placeholder" />
					</view>
				</view>
				<view class="flex listItem" >
					<view class="flex titleBox">
						<text class="title">地址</text>
					</view>
					<view class="right flex">
						<pickerAddress class="input" @change="onCityClick">{{addressDetail||'请选择地址'}}</pickerAddress>
					</view>
				</view>
				<view class="flex listItem" >
					<view class="flex titleBox">
						<text class="title">门牌号</text>
					</view>
					<view class="right flex">
						<input class="input" type="text" v-model="addressData.area" placeholder="楼号、门牌"
							placeholder-class="placeholder" />
					</view>
				</view>
			</view>

		</view>
		<uni-list class="margin-t-20 margin-l-30 margin-r-30" style="border-radius: 20rpx;">
			<uni-list-item
				title="设为默认"
				:switch-checked="addressData.default"
				:show-switch="true"
				:show-arrow="false"
				switch-color="#5dbc7c"
				@switchChange="switchChange"
			></uni-list-item>
		</uni-list>

		<view class="base-buttom" @click="confirm">提交</view>
	</view>
</template>

<script>
	import pickerAddress from '@/components/wangding-pickerAddress/wangding-pickerAddress.vue';
	import {
		addressEdit
	} from '@/api/user.js';
	export default {
		components: {
			pickerAddress,
		},
		data() {
			return {
				code: '',
				addressDetail: '',
				addressData: {
					name: '',
					mobile: '',
					address: {
						province: '',
						city: '',
						district: ''
					},
					area: '',
					default: false
				},
			};
		},
		onLoad(option) {
			let title = '新增收货地址';
			if (option.type === 'edit') {
				title = '编辑收货地址';
				let data = JSON.parse(option.data);
				console.log(data);

				this.addressData = {
					name: data.real_name,
					mobile: data.phone,
					address: {
						province: data.province,
						city: data.city,
						district: data.district
					},
					area: data.detail,
					default: data.is_default == 1,
					id: data.id
				};
				this.addressDetail = data.province + data.city + data.district;
			}
			this.manageType = option.type;
			uni.setNavigationBarTitle({
				title
			});
		},
		methods: {
			// 选中城市切换
			onCityClick({
				data
			}) {
				let address = this.addressData.address;
				address.province = data[0];
				address.city = data[1];
				address.district = data[2];
				this.addressDetail = data.join('');
			},
			//地图选择地址
			chooseLocation() {
				uni.chooseLocation({
					success: data => {
						console.log(data);
						this.addressData.addressName = data.name;
						this.addressData.address = data.name;
					}
				});
			},
			// 设置是否为默认地址
			switchChange(e) {
				this.addressData.default = e.value;
			},
			//提交
			confirm() {
				let obj = this;
				let data = this.addressData;
				if (!data.name) {
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.mobile)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (!data.address) {
					this.$api.msg('请在地图选择所在位置');
					return;
				}
				if (!data.area) {
					this.$api.msg('请填写门牌号信息');
					return;
				}

				//this.$api.prePage()获取上一页实例，可直接调用上页所有数据和方法，在App.vue定义
				addressEdit({
					real_name: data.name,
					phone: data.mobile,
					address: {
						province: data.address.province,
						city: data.address.city,
						district: data.address.district
					},
					detail: data.area,
					is_default: data.default,
					id: data.id || "",
					type: 1
				}).then(function(e) {
					obj.$api.prePage().refreshList();
					uni.showToast({
						title: '提交成功',
						duration: 2000
					});
					setTimeout(function() {
						uni.navigateBack();
					}, 800);
				});
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: $page-color-base;
	}
	.content{
		padding-top: 30rpx;
	}
	.listBox {
		margin: 0 $page-row-spacing;
		border-radius: 20rpx;
		overflow: hidden;
		background-color: #FFFFFF;
	}

	.list {
		.input {
			text-align: right;
			font-size: $font-base;
			color: $color-gray;
			width: 100%;
			flex-grow: 1;
		}

		.listItem {
			padding: 35rpx 40rpx;
			border-bottom: 1px solid $page-color-light;
		}

		.listIconImg {
			width: 36rpx;
		}

		.right {
			color: $font-color-light;
			font-size: $font-base;
			flex-grow: 1;

			.img {
				width: 26rpx;
			}

			.code {}
		}

		.titleBox {
			.title {
				color: $font-color-base;
				font-size: $font-base;
			}
		}
	}
</style>
