<template>
	<view class="uni-container">
		<u-navbar :autoBack="true" :placeholder="true" :bgColor="bgColor">
			<view slot='center' style="font-size: 36rpx; font-weight: bold;">
				收货地址
			</view>
		</u-navbar>
		<view class="form-box">
			<view class="form-item">
				<view class="label-box">
					<view class="label">收货人</view>
					<view class="line"></view>
				</view>
				<input type="text" v-model="siteData.consignee" class="weui-input" placeholder-class="input-placeholder" placeholder="请输入收货人姓名" />
			</view>
			<view class="form-item">
				<view class="label-box">
					<view class="label">联系电话</view>
					<view class="line"></view>
				</view>
				<input type="number" v-model="siteData.phone" class="weui-input" placeholder-class="input-placeholder" placeholder="请输入联系电话" />
			</view>
			<view class="form-item form-item2">
				<view class="label-box">
					<view class="label">所在地区</view>
					<view class="line"></view>
				</view>
				<!-- <image class="img" @click="getLocation" src="../../static/shop/address.png" mode=""></image> -->
				<picker mode="multiSelector" @change="bindMultiPickerChange" @columnchange="bindMultiPickerColumnChange" :value="multiIndex" :range="communityListArray" range-key="name">
					
					<text class="choose-text" v-if="!addressByPcrs">省、市、区、街道</text>
					<text class="choose-text1" v-if="addressByPcrs">{{addressByPcrs}}</text>
				</picker>
			</view>
			<view class="form-item form-item1">
				<view class="label-box">
					<view class="label">详细地址</view>
					<view class="line"></view>
				</view>
				<uni-easyinput type="textarea" autoHeight v-model="siteData.address" placeholder-class="input-easyinput" placeholder="如街道、门牌号、小区、楼栋号、单元室等"></uni-easyinput>
			</view>
		</view>
		<view class="set-box">
			<view class="label">设为默认收货地址</view>
			<u-switch v-model="siteData.isDefault" space="14rpx" activeValue="1" inactiveValue="0" activeColor="#01BE69" @change="changeSwitch"></u-switch>
		</view>
		<view class="btn" v-if="!id" @click="saveAddress">确认</view>
		<view class="btn-box1" v-if="id">
			<view class="btn1 delete" @click="deleteAddress">删除</view>
			<view class="btn1 affirm" @click="saveAddress">确认</view>
		</view>
		<uni-popup class="popupRefund" ref="popup" background-color="#fff">
			<view class="popup-content">
				<image class="bgImg" src="https://common/refundPopup.png" mode=""></image>
				<view class="con">
					<view class="title">温馨提示</view>
					<view class="text">确认删除该收货地址吗？</view>
					<view class="btn-box">
						<view class="close" @click="close">我再想想</view>
						<view class="save" @click="onDelete">确认</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>
<script>
	// import amap from '../../common/amap-wx.130.js'
	export default {
		data() {
			return {
				id: '',
				bgColor: '#F6F9FF',
				siteData: {
					consignee: '',
					phone: '',
					provinceName: '',
					cityName: '',
					areaName: '',
					provinceId: '',
					cityId: '',
					areaId: '',
					address: '',
					isDefault: '0',
				},
				// amapPlugin: null,
				// key: '0ec68e7bc2363d69387c38f18f9de08f',
				
				addressByPcrs:"",
				
				communityListArray : [],
				multiIndex : [0, 0, 0],
				communityListData: []
			};
		},
		onLoad(options) {
			this.id = options.id
			// this.amapPlugin = new amap.AMapWX({  
			// 	key: this.key  
			// });
			if (this.id) {
				this.userAddressDetail()
			} else {
				this.listAll(0, 0)
			}
		},
		methods: {
			async listAll(params, index) {
				let info = await this.$shop.listAll(params)
				this.communityListData = info.data;
				if (index === 0) {
					this.communityListArray[0] = info.data
					this.listAll(this.communityListData[0].areaCode, 1)
				} else if (index === 1) {
					this.communityListArray[1] = info.data
					this.listAll(this.communityListData[0].areaCode, 2)
				} else {
					this.communityListArray[2] = info.data
				}
			},
			// value 改变时触发 change 事件
			bindMultiPickerChange(e) {
				this.multiIndex = e.detail.value
				this.addressByPcrs = ''
				this.multiIndex.forEach((m, i) => {
					this.addressByPcrs += this.communityListArray[i][m].name
					if (i == 0) {
						this.siteData.provinceName = this.communityListArray[i][m].name
						this.siteData.provinceId = this.communityListArray[i][m].areaCode
					} else if (i == 1) {
						this.siteData.cityName = this.communityListArray[i][m].name
						this.siteData.cityId = this.communityListArray[i][m].areaCode
					} else {
						this.siteData.areaName = this.communityListArray[i][m].name
						this.siteData.areaId = this.communityListArray[i][m].areaCode
					}
				})
			},
			// 某一列的值改变时触发 columnchange 事件
			async bindMultiPickerColumnChange(e) {
				if(!this.multiIndex || !this.multiIndex.length){
					this.multiIndex = [0,0,0]
				}
				// 第几列
				let column = e.detail.column;
				// 索引
				let value = e.detail.value;
				let multiIndex = this.multiIndex;
				multiIndex[column] = value;
				if(column == 0){
					multiIndex[1] = 0;
					multiIndex[2] = 0;
				}
				if(column == 1){
					multiIndex[2] = 0;
				}
				this.multiIndex = multiIndex;
				let communityListArray = [];
				let info;
				if (column === 0) {
					communityListArray[0] = this.communityListArray[0]
					info = await this.$shop.listAll(communityListArray[0][multiIndex[0]].areaCode)
					communityListArray[1] = info.data
					info = await this.$shop.listAll(communityListArray[1][multiIndex[1]].areaCode)
					communityListArray[2] = info.data
					this.communityListArray = communityListArray;
				} else if (column === 1) {
					communityListArray[0] = this.communityListArray[0]
					communityListArray[1] = this.communityListArray[1]
					info = await this.$shop.listAll(communityListArray[1][multiIndex[1]].areaCode)
					communityListArray[2] = info.data
					this.communityListArray = communityListArray;
				}
			},
			async userAddressDetail() {
				let info = await this.$shop.userAddressDetail(this.id)
				if (info.code === 200) {
					this.siteData = info.data
					this.addressByPcrs = this.siteData.provinceName + this.siteData.cityName + this.siteData.areaName
					let arr1 = [0, this.siteData.provinceId, this.siteData.cityId]
					let arr2 = [this.siteData.provinceId, this.siteData.cityId, this.siteData.areaId]
					this.editData(arr1, arr2)
				} else {
					uni.$u.toast(info.msg);
				}
			},
			async editData(arr1, arr2) {
				var communityListArray = []
				var multiIndex = []
				for (var i = 0; i < arr1.length; i ++) {
					let info = await this.$shop.listAll(arr1[i])
					communityListArray[i] = info.data
					info.data.map((m, n) => {
						if (m.areaCode === arr2[i]) {
							multiIndex[i] = n
						}
					})
				}
				this.communityListArray = communityListArray
				this.multiIndex = multiIndex
			},
			changeSwitch(e) {
				console.log('e', e)
			},
			getLocation() {
				uni.showLoading({  
					title: '获取信息中'  
				});
				this.amapPlugin.getRegeo({
					success: (data) => {
						console.log('11111111111', data)
						this.addressByPcrs = data[0].regeocodeData.addressComponent.province + data[0].regeocodeData.addressComponent.city + data[0].regeocodeData.addressComponent.district
						this.siteData.provinceName = data[0].regeocodeData.addressComponent.province
						this.siteData.cityName = data[0].regeocodeData.addressComponent.city
						this.siteData.areaName = data[0].regeocodeData.addressComponent.district
						uni.hideLoading();
					}  
				});
			},
			async saveAddress() {
				var phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
				if (!this.siteData.consignee) {
					uni.showToast({ mask: true, title: '请输入收货人姓名', icon: 'none' })
					return false;
				}
				if (!phoneReg.test(this.siteData.phone)) {
					uni.showToast({ mask: true, title: '请输入正确联系电话', icon: 'none' })
					return false;
				}
				if (!this.addressByPcrs) {
					uni.showToast({ mask: true, title: '请选择所在地区', icon: 'none' })
					return false;
				}
				if (!this.siteData.address) {
					uni.showToast({ mask: true, title: '请输入详细地址', icon: 'none' })
					return false;
				}
				const params = this.siteData
				let info;
				if (this.id) {
					params.id = this.id;
					info = await this.$shop.updateUserAddress(params)
				} else {
					info = await this.$shop.addUserAddress(params)
				}
				if (info.code === 200) {
					uni.showToast({
						title: '保存成功',
						mask: true,
						icon: 'success'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 500)
				} else {
					uni.$u.toast(info.msg);
				}
			},
			deleteAddress() {
				this.$refs.popup.open('center')
			},
			close() {
				this.$refs.popup.close()
			},
			async onDelete() {
				const params = {
					id: this.id
				}
				let info = await this.$shop.deleteUserAddress(params)
				if (info.code === 200) {
					uni.showToast({
						title: '删除成功',
						mask: true,
						icon: 'success'
					})
					this.close()
					setTimeout(() => {
						uni.navigateBack()
					}, 500)
				} else {
					uni.$u.toast(info.msg);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.uni-container {
		width: 100%;
		min-height: 100vh;
		box-sizing: border-box;
		background: #F6F9FF;
		.form-box {
			background: #FFFFFF;
			border-radius: 10rpx;
			padding: 0 32rpx;
			margin: 0 32rpx;
			.img-box {
				width: 148rpx;
				height: 148rpx;
				border-radius: 50%;
				overflow: hidden;
				margin: 0 auto 16rpx;
				position: relative;
				padding: 0;
				background-color: #fff;
				.userImg {
					width: 148rpx;
					height: 148rpx;
					filter: grayscale(60%);
				}
				.camera {
					position: absolute;
					width: 64rpx;
					height: 64rpx;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
				}
			}
			.form-item {
				display: flex;
				align-items: center;
				flex-direction: row;
				padding: 32rpx 0;
				border-bottom: 1rpx solid #E8E8E8;
				&:last-child {
					border: none;
				}
				.left-box {
					flex: 1;
					display: flex;
					align-items: flex-start;
					flex-direction: row;
				}
				.img {
					width: 37rpx;
					height: 50rpx;
				}
				.label-box {
					width: 152rpx;
					margin-right: 20rpx;
					display: flex;
					align-items: center;
					flex-direction: row;
					justify-content: space-between;
					.label {
						font-weight: 500;
						font-size: 28rpx;
						color: #333333;
					}
					.line {
						width: 1rpx;
						height: 26rpx;
						background-color: #E8E8E8;
					}
				}
				.code-box {
					flex: 1;
					display: flex;
					align-items: center;
					flex-direction: row;
					justify-content: space-between;
					.uni-list-cell-db {
						width: 100%;
						.picker {
							width: 100%;
							position: relative;
							.uni-input {
								flex: 1;
								display: inline-block;
							}
							.input-placeholder {
								flex: 1;
								display: inline-block;
							}
							.icon {
								position: absolute;
								right: 0;
								top: 50%;
								transform: translateY(-50%);
								/deep/.uniui-right {
									font-size: 34rpx !important;
									color: #999999 !important;
								}
							}
						}
					}
				}
				/deep/.input-placeholder {
					font-weight: 400;
					font-size: 28rpx;
					color: #999999!important;
				}
				/deep/.weui-input {
					font-weight: 500;
					font-size: 28rpx;
					color: #333333;
				}
				.uni-input {
					font-weight: 500;
					font-size: 28rpx;
					color: #333333;
				}
			}
			/deep/.uni-easyinput {
				.uni-easyinput__content {
					border: none;
					.uni-easyinput__content-textarea {
						min-height: 70rpx !important;
						margin: 0 !important;
						padding: 0 !important;
						font-weight: 500 !important;
						font-size: 28rpx !important;
						color: #333333 !important;
					}
					.uni-easyinput__placeholder-class {
						font-weight: 400 !important;
						font-size: 28rpx !important;
						color: #999999!important;
					}
				}
			}
			.form-item1 {
				align-items: flex-start;
			}
			.form-item2 {
				align-items: flex-start;
				/deep/.uni-easyinput {
					margin-right: 20rpx !important;
					.uni-easyinput__content {
						.uni-easyinput__content-textarea {
							min-height: 0 !important;
						}
					}
				}
				.choose-text {
					flex: 1;
					font-weight: 400;
					font-size: 28rpx;
					color: #999999;
				}
				.choose-text1 {
					flex: 1;
					font-weight: 500;
					font-size: 28rpx;
					color: #333333;
				}
			}
		}
		.set-box {
			background: #FFFFFF;
			border-radius: 10rpx;
			padding: 16rpx 24rpx;
			margin: 24rpx 32rpx 0;
			display: flex;
			align-items: center;
			flex-direction: row;
			justify-content: space-between;
			.label {
				font-weight: 400;
				font-size: 28rpx;
				color: #000000;
			}
			/deep/.u-switch {
				width: 104rpx !important;
				height: 64rpx !important;
				.u-switch__bg {
					width: 104rpx !important;
					height: 64rpx !important;
				}
				.u-switch__node {
					width: 36rpx !important;
					height: 36rpx !important;
					border: 1rpx solid rgba(0, 0, 0, 0.12);
					box-shadow: none !important;
				}
			}
		}
		.btn {
			margin: 134rpx 32rpx 0;
			width: 686rpx;
			height: 80rpx;
			background: #01BE69;
			border-radius: 66rpx;
			line-height: 80rpx;
			text-align: center;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
		}
		.btn-box1 {
			margin: 134rpx 32rpx 0;
			display: flex;
			align-items: center;
			flex-direction: row;
			justify-content: space-between;
			.btn1 {
				width: 320rpx;
				height: 80rpx;
				border-radius: 66rpx 66rpx 66rpx 66rpx;
				text-align: center;
				line-height: 80rpx;
				font-weight: 500;
				font-size: 30rpx;
			}
			.delete {
				color: #01BE69;
				background: rgba(1,190,105,0.2);
			}
			.affirm {
				color: #FFFFFF;
				background: #01BE69;
			}
		}
		.popupRefund {
			.btn-box {
				margin-top: 90rpx;
			}
		}
	}
</style>