<template>
	<view class="user-address backcor9 ">


		<scroll-view scroll-y="true" class="address-contain paading10">
			<radio-group @change="radioChange">
				<view v-for="(item,index) in addressList" @click="chooseAddress(item)" :key="item.id"
					class="border-8r paading10 backcolorfff marb10">
					<view class="user-msg border-b paddbotm5">
						<view class=" mart3 ">
							<text class="address-name">{{item.consignee}}</text>
							<text class=" margle10 ">{{item.phone}}</text>
						</view>
						<view class="color999 mart5">
							地址:{{item.regionName +" " +item.address}}
						</view>

					</view>
					<view class="user-handle mart10">
						<view class="handle-left">
							<!-- <radio  :checked="item.whetherDefault" style="transform:scale(0.7)"  :value="item.id" color="#FF1A34" /> -->
							<radio @click.stop :checked="selectId == item.id" style="transform:scale(1.1)" :value="item.id"
								color="#FF1A34" />
							<text class=" margle">默认地址</text>
						</view>
						<view class="handle-right">
							<button class="marRight10 btn edit" 
								@click.stop="editAddress(item.id)">编辑</button>
							<button   class="del btn"
								@click.stop="deleteAddress(item.id)">删除</button>
						</view>
					</view>

				</view>
			</radio-group>
			<noMore></noMore>
		</scroll-view>


		<view class="address-btn  fcorfff font16">
			<view class="add-btn" @click="addNewAddress">添加新地址</view>
			<!-- <view class="add-btn" @click="daoRu">导入系统地址</view> -->
		</view>
	</view>
</template>

<script>
	import noMore from '@/components/no-more/no-more.vue'
	import {
		getDeliveryAddressList,
		deleteAddress,
		updateDeliveryAddress
	} from '@/Utils/physicalObject.js'


	export default {
		components: {
			noMore,

		},
		data() {
			return {
				
				selectId: 0,
				addressList: [],
				status: 0, //其他页面调过来的状态码。
			}
		},
		onLoad(options) {
			if (options.status) { //1:从订单确认页面跳过来的
				this.status = options.status;
			}
			
		},
		onShow(){
			this.initData()
		},
		methods: {
			initData() {
				this.selectId = 0;
				uni.showLoading({
						title: '加载中',
						mask:true,
					});
					this.addressList=[];
				getDeliveryAddressList().then(res => {
					uni.hideLoading();
					if (res.return_code == '000000') {

						if (res.return_data.length > 0) {
							// console.log(res.return_data)
							this.addressList = res.return_data;
							this.addressList.map(item => {
								if (item.whetherDefault) {
									this.selectId = item.id;
								}
							})
						}

					} else {
						uni.showToast({
							title: res.return_msg,
							icon: 'none',
							duration: 2000
						})
					}

				})
			},

			// 修改默认地址
			radioChange(e) {
				console.log(e)
				let select = e.detail.value
				let content = null;
				this.addressList.map(item => {
					if (item.id == select) {
						content = item;
					}
				})
				let params = {
					"id": content.id,
					"address": content.address,
					"consignee": content.consignee,
					"phone": content.phone,
					"regionId": content.regionId,
					"regionName": content.regionName,
					"whetherDefault": true
				}
				updateDeliveryAddress(params).then(res => {
					let title;

					if (res.return_code == '000000') {
						title = res.return_data;
						this.selectId = e.detail.value;
					} else {
						title = res.return_msg;
					}
					uni.showToast({
						title: title,
						icon: 'none',
						duration: 2000
					})
				})
			},
			editAddress(id) {
				uni.navigateTo({
					url: "./addNewAddress/addNewAddress?id=" + id
				})
			},
			deleteAddress(id) {
				uni
				let _this = this;
				uni.showModal({
					title: '提示',
					content: '确定删除该地址吗?',
					success: function(res) {
						if (res.confirm) {
							let params = {
								id: id
							}
							deleteAddress(params).then(res => {
								let title;
								if (res.return_code == '000000') {
									title = res.return_data;
								} else {
									title = res.return_msg;
								}
								uni.showToast({
									title: title,
									icon: 'none',
									duration: 2000
								})

								_this.initData();

							})

						} else if (res.cancel) {

							// console.log('用户点击取消');
						}
					}
				});


			},
			// 新增地址
			addNewAddress() {
				uni.navigateTo({
					url: "./addNewAddress/addNewAddress"
				})
			},
			// 导入系统地址
			daoRu() {
				uni.chooseAddress({
					success: (res) => {
						console.log(res)
						var data = {
							"name": res.userName || '',
							"tel": res.telNumber || '',
							"province": res.provinceName || '',
							"city": res.cityName || '',
							"county": res.countyName || '',
							"address": res.detailInfo || ''
						};

						// 之后再发起一个添加地址的请求

					},
					fail: (err) => {

					}
				})


			},
			chooseAddress(addressItem) {

				if (this.status == 1) {
					// let pages = getCurrentPages(); // 当前页面
					// let beforePage = pages[pages.length - 2]; // 上一页        
					// uni.navigateBack({
					// 	success: function() {
					// 		beforePage.$vm.getRegiFreight(addressItem);
							
							
					// 	}
					// })
					
					uni.$emit('address',addressItem);
					uni.navigateBack({})
					
				}



			}
		}
	}
</script>

<style lang="scss" scoped>
	.border-b {
		border-bottom: 1px solid #f6f6f6;
	}

	.user-address {
		height: 100vh;
		overflow: hidden;

		.address-contain {
			height: calc(100vh - 80px);
			width: calc(100% - 20px);
		}

		.user-handle {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.handle-right {
			vertical-align: middle;
			
			display: flex;
			align-items: center;
			.edit{
				background-color: #1aad19;
				
			}
			.del{	
				background-color: #e64340;	
			}
			.btn{
				box-sizing: content-box;
				font-size: 15px;
				height: 40px;
				line-height: 40px;
				width: 80rpx;
				border-radius: 10px;
				// padding: 6px 30rpx;
				color:#ffffff !important;
			}
		}
	}



	// 底部按钮
	.address-btn {
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		justify-content: space-around;
		align-items: center;
		margin-bottom: 15px;
		.add-btn {
			width: 80%;
			text-align: center;
			height: 60px;
			border-radius: 30px;
			background-color: #3da7e7;
			
			color: #ffffff;
			line-height: 60px;
			font-size: 18px;
			
			
			
			&:first-of-type {
				background-color: #F6C133;
			}

			&:last-of-type {
				background-color: #3da7e7;
			}

		}
	}
</style>
