<template>
	<view class="content">
		<view class="inner">
			<view v-for="(item,index) in address" :key="index" @click.stop="selectAddress(item)" class="address-item" >
				<view class="info" >
					<view class="title">
						<view v-if="item.isDefault == 1" class="tag">默认</view>
						{{item.province}}{{item.city}}{{item.district}}{{item.detail}} 
					</view>
					<view class="name-phone">
						<text class="text">{{item.realName}}</text>
						<text class="text">{{$parsePhone(item.phone)}}</text>
					</view>
				</view>
				<view class="operat-box">
					<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/del.png" mode="" @click.stop="delAddress(item)"></image>
					<image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/edit.png" mode="" @click.stop="editAddress(item)"></image>
				</view>
			</view>
			<view v-if="address.length == 0" class="no-data-box" @click="getAddressList()">
				<image src="https://cos.his.cdwjyyh.com/fs/20240423/cf4a86b913a04341bb44e34bb4d37aa2.png" mode="aspectFit"></image>
				<view class="empty-title">暂无数据</view>
			</view>
			
		</view>
		<view class="btn-box">
			
			<view class="sub-btn" @click="addAddress()">新建收货地址</view>
		</view>
	</view>
</template>

<script>
	import {getAddressList,delAddress} from '@/api/userAddress'
	export default {
		data() {
			return {
				address:[],
			}
		},
		//发送给朋友
		onShareAppMessage(res) {
			return {
				title: '新建收货地址',
				path: '/pages_user/address',
			}
			
		},
		onLoad() {
			var that=this;
			uni.$on('refreshAddress', () => {
				that.getAddressList()
			})
		},
		onShow() {
			this.getAddressList()
		},
		methods: {
			selectAddress(item){
				uni.$emit('updateAddress',item);
				uni.navigateBack({
			  		delta: 1
				})
			},
			editAddress(item){
				uni.navigateTo({
					url: './addEditAddress?type=edit&addressId='+item.addressId
				})
			},
			delAddress(item){
				uni.showModal({
					title:"提示",
					content:"确认删除此地址吗?",
					showCancel:true,
					cancelText:'取消',
					confirmText:'确定',
					success:res=>{
						if(res.confirm){
							// 用户点击确定
							var data={addressId:item.addressId}
							delAddress(data).then(
								res => {
									if(res.code==200){
										 uni.showToast({
										 	icon:'success',
										 	title: "操作成功",
										 });
										 this.getAddressList()
									}else{
										uni.showToast({
											icon:'none',
											title: "请求失败",
										});
									}
								},
								rej => {}
							);
														
						}else{
							// 否则点击了取消  
						}
					}
				})
			},
			getAddressList(){
				getAddressList().then(
					res => {
						if(res.code==200){
							this.address=res.data;
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			// 新建地址
			addAddress() {
				uni.navigateTo({
					url: './addEditAddress?type=add'
				})
			}
		}
	}
</script>

 
 <style lang="scss">
 	page{
 		height: 100%;
 	}
 	.content{
 		height: 100%;
 		display: flex;
 		flex-direction: column;
 		justify-content: space-between;
 		.inner{
 			flex: 1;
 			padding: 20upx 20upx 160upx;
 			.top{
 				padding: 0upx 20upx 20upx;
 				text-align: right;
 				.del{
 					font-size: 28upx;
 					font-family: PingFang SC;
 					color: #999999;
 				}
 			}
 			.address-item{
 				background: #FFFFFF;
 				border-radius: 16upx;
 				padding: 46upx 40upx 50upx 24upx;
 				display: flex;
 				align-items: center;
 				justify-content: space-between;
 				margin-bottom: 20upx;
 				.info{
 					width: 75%;
 					.title{
 						font-size: 30upx;
 						font-family: PingFang SC;
 						font-weight: bold;
 						color: #111111;
 						line-height: 42upx;
 						word-break: break-all;
 						.tag{
 							padding: 0 6upx;
 							height: 32upx;
 							line-height: 32upx;
 							font-size: 22upx;
 							font-family: PingFang SC;
 							font-weight: 500;
 							color: #FFFFFF;
 							background: #C39A58;
 							border-radius: 4upx;
 							float: left;
 							margin-right: 10upx;
 							margin-top: 5upx;
 						}
 					}
 					.name-phone{
 						margin-top: 30upx;
 						display: flex;
 						align-items: center;
 						.text{
 							font-size: 26upx;
 							font-family: PingFang SC;
 							font-weight: 500;
 							color: #999999;
 							margin-right: 22upx;
 							&:last-child{
 								margin-right: 0;
 							}
 						}
 					}
 				}
 				
 				.operat-box{
 					display: flex;
 					align-items: center;
 					image{
 						padding: 15rpx;
 						width: 30upx;
 						height: 30upx;
 						margin-left: 10upx;
 						&:first-child{
 							margin-left: 0;
 						}
 					}
 				}
 			}
 		}
 		.btn-box{
 			z-index: 9999;
 			width: 100%;
 			padding: 30upx;
 			position: fixed;
 			bottom: 0;
 			left: 0;
 			box-sizing: border-box;
 			background: #FFFFFF;
 			 
 			.sub-btn{
 				 
 				height: 88upx;
 				line-height: 88upx;
 				text-align: center;
 				font-size: 30upx;
 				font-family: PingFang SC;
 				font-weight: bold;
 				color: #FFFFFF;
 				background: #C39A58;
 				border-radius: 44upx;
 			}
 		}
 	}
 </style>
 