<template>
	<view class="content">
		<view class="row b-b" >
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.real_name" placeholder="收货人姓名"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">手机号</text>
			<input class="input" type="number" v-model="addressData.phone" placeholder="收货人手机号码"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">地址</text>
			<pickerAddress class="input" @change="onCityClick">{{addressDetail||'请选择地址'}}</pickerAddress>
			<text class="iconfont iconlocation"></text>
		</view>
		<view class="row b-b">
			<text class="tit">门牌号</text>
			<input class="input" type="text" v-model="addressData.detail" placeholder="楼号、门牌"
				placeholder-class="placeholder" />
		</view>
		<view class="row b-b" style="display: flex;justify-content: space-between;" @click="addressData.is_default = !addressData.is_default">
			<text class="tit">设为默认</text>
			<view class="yuan">
				<view class="yuan-wai">
					
				</view>
				<view class="yuan-yuan" v-show="addressData.is_default">

				</view>
			</view>
			<!-- <input class="input" type="text" v-model="addressData.area" placeholder="楼号、门牌" placeholder-class="placeholder" /> -->
		</view>
		<!-- <uni-list class="margin-t-20">
			<uni-list-item
				title="设为默认"
				:switch-checked="addressData.default"
				:show-switch="true"
				:show-arrow="false"
				switch-color="#5dbc7c"
				@switchChange="switchChange"
			></uni-list-item>
		</uni-list> -->

		<button class="add-btn" @click="confirm">提交</button>
	</view>
</template>

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

				this.addressData = {
					real_name: data.real_name,
					phone: data.phone,
					province: data.province,
					city: data.city,
					district: data.district,
					detail: data.detail,
					is_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.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 that = this;
				let data = this.addressData;
				if (!data.real_name) {
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if (!/(^1[0-9]{10}$)/.test(data.phone)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (!data.province || !data.city || !data.district) {
					this.$api.msg('请选择省市区');
					return;
				}
				if (!data.detail) {
					this.$api.msg('请填写门牌号信息');
					return;
				}
				let setAddress;
				if(data.id) {
					setAddress = editAddress
				}else {
					setAddress = addAddress
				}
				//this.$api.prePage()获取上一页实例，可直接调用上页所有数据和方法，在App.vue定义
				setAddress(data).then(function(e) {
					that.$api.prePage().refreshList();
					uni.showToast({
						title: '提交成功',
						duration: 2000
					});
					setTimeout(function() {
						uni.navigateBack();
					}, 800);
				});
			}
		}
	};
</script>

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

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

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

		.input {
			flex: 1;
			font-size: 30rpx;
			color: $font-color-dark;
			text-align: right;
		}

		.iconlocation {
			font-size: 36rpx;
			color: $font-color-light;
		}
	}

	.default-row {
		margin-top: 16rpx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16rpx) scale(0.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690rpx;
		height: 80rpx;
		margin: 60rpx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10rpx;
	}

	.alert-box {
		background-color: #ffffff;
	}

	.yuan {
		height: 40rpx;
		width: 40rpx;
		position: relative;
		&-wai {
			border: 1rpx solid #999;
			height: 38rpx;
			width: 38rpx;
			border-radius: 50%;
			position: absolute;
			top: 0;
			left: 0;
			bottom: 0;
			right: 0;
			margin: auto;
		}
		&-yuan {
			position: absolute;
			top: 0;
			left: 0;
			bottom: 0;
			right: 0;
			margin: auto;
			background-color: $base-color;
			width: 30rpx;
			height:30rpx;
			border-radius: 50%;
		}
	}
</style>