<template>
	<!-- 新增地址页面 -->
	<view class="container">
		<view class="wapper">
			<!-- 表单 -->
			<view class="form-box">
				<view class="cu-form-group">
					<view class="title">收货人</view>
					<u-input type="text" placeholder="请输入收货人姓名" border="none"
						v-model="addressData.name">
					</u-input>

				</view>
				<view class="cu-form-group">
					<view class="title">手机号码</view>
					<u-input type="number" placeholder="请输入11位手机号" border="none"
						maxlength="11" v-model="addressData.mobile"></u-input>
				</view>
				<view @click="selectAddressShow = true" class="cu-form-group">
					<view class="title">所在地区</view>
					<u-input :value="addressData.province + addressData.city + addressData.district" readonly border="none" placeholder="选择省市区"
						>
						<u-icon slot="suffix" name="arrow-right"></u-icon>
					</u-input>
				</view>

				<view class="detail-wapper">
					<view class="title">详细地址</view>
					<textarea class="h-100" style="background-color: #FFF;padding: 10px;border:1px solid #CCCCCC;"
						 :adjust-position="false" placeholder="如道路、门牌号、小区、楼栋号、单元室等"
						v-model="addressData.address">
							</textarea>
				</view>
				<view class="cu-form-group" style="justify-content: space-between;">
					<view class="title">设置默认收货地址</view>
					<image v-if="addressData.default === '0'" @click="addressData.default = '1'"
						style="width: 88rpx;height:44rpx;" src="@/static/my/no_switch.png"></image>
					<image v-else @click="addressData.default = '0'" style="width: 88rpx;height: 44rpx;"
						src="@/static/my/yes_switch.png"></image>
				</view>
			</view>

			<view @click="onSubmit" class="btn-wapper">
				<text>{{ type === 'add'? '确定' : '保存' }}</text>
			</view>
		</view>
		
		<!-- 地址选择 前端定义 -->
		<address-picker :show="selectAddressShow" @confirm="onConfirmAddress" @close="selectAddressShow = false" @cancel="selectAddressShow = false" :closeOnClickOverlay="true" confirmText="确定"></address-picker>
		
		<!-- 地址选择 后端定义全国城市的地址选择 -->
		<!-- <addressPicker v-if="showAddressSelector" :areaInfoSelected="areaInfoObj" @cancel="handleCancel"
			@confirm="handleConfirm">
		</addressPicker> -->
	</view>
</template>

<script>
	import addressPicker from '@/components/address-modal.vue';
	export default {
		data() {
			return {
				addressDataTree: [], // 全国位置
				addressData: {
					name: '',
					mobile: '',
					province: '',
					city: '',
					district: '',
					address: '', // 详细地址
					default: '0', // 是否默认地址 1是 0否
				},
				type: 'add', // 添加或编辑
				addressId: '',
				
				selectAddressShow: false,
				
				
				// 后端定义全国城市的地址选择
				showAddressSelector: false,
				areaInfoObj: {
					provinceObj: {
						id: '',
						name: '',
					},
					cityObj: {
						id: '',
						name: '',
					},
					cityObj: {
						id: '',
						name: '',
					},
					areaObj: {
						id: '',
						name: '',
					}
				},
			}
		},
		components: {
			// addressPicker
		},
		onLoad(e) {
			if (e.item) {
				const item = JSON.parse(e.item)
				uni.setNavigationBarTitle({
					title: '编辑收货地址'
				})
				this.type = 'edit' // 判断当前是添加还是编辑的
				this.addressId = item.id // 保存收货地址ID，请求的时候用

				this.addressData.name = item.name
				this.addressData.mobile = item.mobile
				this.addressData.province = item.province
				this.addressData.city = item.city
				this.addressData.district = item.district
				this.addressData.address = item.address
				this.addressData.default = item.default
			} else { // 没有参数就是新增地址
				this.type = 'add'
			}
		},
		methods: {
			// 地区选择
			onConfirmAddress(e){
				// console.log(e)
				this.addressData.province = e.value[0]
				this.addressData.city = e.value[1]
				this.addressData.area = e.value[2]
				
				this.selectAddressShow = false
			},
			// 添加/编辑收获地址保存
			onSubmit() {
				if (!this.addressData.name) {
					return this.$u.toast('请填写收货人姓名');
				}
				if (!this.addressData.mobile || !uni.$u.test.mobile(this.addressData.mobile)) {
					return this.$u.toast('请输入正确的手机号码');
				}
				if (!this.addressData.province) {
					return this.$u.toast('请选择地区');
				}
				if (!this.addressData.address) {
					return this.$u.toast('请填写详细地址');
				}
				// 添加/编辑收货地址请求
				if (this.type === 'add') {
					const params = {
						url: '/api/user/insertadd',
						loadingTip: '',
						data: {
							...this.addressData
						}
					}
					this.$request(params).then(res => {
						uni.hideLoading()
						// console.log(res)
						this.$u.toast('添加地址成功,稍等后自动返回');
						// 初始化data的值
						Object.assign(this.$data.addressData, this.$options.data().addressData)
						// 添加/编辑成功跳转到收货地址页面
						setTimeout(() => {
							uni.navigateBack({
								 delta: 1
							})
						}, 800)
					}).catch(err => {
						console.log(err)
						uni.hideLoading()
						this.$u.toast('添加失败');
					})
				} else { // 编辑
					const params = {
						url: '/api/user/editadd',
						loadingTip: '',
						data: {
							id: this.addressId, // 修改地址时必须传的ID
							...this.addressData
						}
					}
					this.$request(params).then(res => {
						// console.log(res)
						this.$u.toast('编辑地址成功,稍等后自动返回');
						// 初始化data的值
						Object.assign(this.$data.addressData, this.$options.data().addressData)
						// 添加/编辑成功跳转到收货地址页面
						setTimeout(() => {
							uni.navigateBack({
								 delta: 1
							})
						}, 800)
					}).catch(err => {
						console.log(err)
						this.$u.toast('编辑失败');
					})
				}
			},
			handleCancel() {
				this.showAddressSelector = false;
			},
			handleConfirm(areaInfoObj) {
				// console.log(areaInfoObj)
				this.addressData.province = areaInfoObj.provinceObj.label
				this.addressData.city = areaInfoObj.cityObj.label
				this.addressData.area_name = areaInfoObj.areaObj.label
				this.addressData.area_id = areaInfoObj.areaObj.value
				
				this.showAddressSelector = false;
				this.areaInfoObj = areaInfoObj;
			},
		}
	}
</script>

<style scoped>
	.container {
		height: 100%;
	}

	.wapper {
		height: 100%;
	}



	.title-box {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.title-box>image {
		width: 45rpx;
		height: 45rpx;
		position: absolute;
		top: 30rpx;
		left: 20rpx;
	}



	/* 表单内容 */
	.form-box {
		background-color: #FFF;
		margin-top: -20rpx;
		border-radius: 30rpx;
		padding: 20rpx 0rpx;
	}

	.cu-form-group {
		padding: 30rpx 20rpx;
		display: flex;
		align-items: center;
		border-bottom: 0.5px solid #F5F5F5;
	}

	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	.detail-wapper {
		background-color: #FFF;
		padding: 10px 13px;
	}

	.detail-wapper>textarea {
		width: 100%;
		height: 80px;
		border-radius: 10rpx;
		margin-top: 10px;
	}

	.cu-btn.lg {
		font-size: 32rpx;
		height: 86rpx;
	}

	.tipsScroll {
		width: 100%;
		height: 500rpx;
		padding: 0 50rpx;
	}

	.tipsScroll .item {
		margin: 25rpx 0;
	}

	.tipsScroll .item view {
		color: #B8B8B8;
		font-size: 22rpx;
	}

	.tipsScroll .item text {
		color: #333;
	}

	.btn-wapper {
		width: 90%;
		height: 80rpx;
		position: fixed;
		bottom: 100rpx;
		left: 50%;
		margin-left: -45%;
		text-align: center;
		line-height: 80rpx;
		background-color: #EB7318;
		border-radius: 8rpx;
		color: #FFF;
	}
</style>