<template>
	<view class="content">
		<view class="inner">
			<view class="top">
				<view  class="del"  @click="delAllAddress">清空地址</view>
			</view>
			<view v-for="(item,index) in address" :key="index" class="address-item" >
				<view class="info" @click="selectAddress(item)">
					<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">{{utils.parsePhone(item.phone)}}</text>
					</view>
				</view>
				<view class="operat-box">
					<image src="https://zkzh-2025.oss-cn-beijing.aliyuncs.com/orangeShop/del1.png" mode="" @click="delAddress(item)"></image>
					<image src="https://zkzh-2025.oss-cn-beijing.aliyuncs.com/orangeShop/edit.png" mode="" @click="editAddress(item)"></image>
				</view>
			</view>
			<view v-if="address.length == 0" class="no-data-box" @click="getAddressList()">
				<image src="https://zkzh-2025.oss-cn-beijing.aliyuncs.com/orangeShop/no_data.png" mode="aspectFit"></image>
				<view class="empty-title">暂无数据</view>
			</view>
			
		</view>
		<view class="btn-box">
			
			<view class="sub-btn" @click="addAdress">新建收货地址</view>
		</view>
	</view>
</template>

<script>
	import {getAddressList,delAddress,delAllAddress} from '@/api/address'
	export default {
		data() {
			return {
				address:[],
			}
		},
		onLoad() {
			this.getAddressList()
			uni.$on('refreshAddress', () => {
				this.getAddressList()
			})
		 
		},
		methods: {
			selectAddress(item){
			  uni.$emit('updateAddress',item);
			  uni.navigateBack({
			  		delta: 1
			  })
			},
			editAddress(item){
				uni.navigateTo({
					url: './addAddress?type=edit&id='+item.id
				})
			},
			delAllAddress(item){
				uni.showModal({
					title:"提示",
					content:"确认删除所有地址吗?",
					showCancel:true,
					cancelText:'取消',
					confirmText:'确定',
					success:res=>{
						if(res.confirm){
							var data={ }
							delAllAddress(data).then(
								res => {
									if(res.code==200){
										 uni.showToast({
										 	icon:'success',
										 	title: "操作成功",
										 });
										 this.getAddressList()
									}else{
										uni.showToast({
											icon:'none',
											title: "请求失败",
										});
									}
								},
								rej => {}
							);
														
						}else{
							// 否则点击了取消  
						}
					}
				})
			},
			delAddress(item){
				uni.showModal({
					title:"提示",
					content:"确认删除此地址吗?",
					showCancel:true,
					cancelText:'取消',
					confirmText:'确定',
					success:res=>{
						if(res.confirm){
							// 用户点击确定
							var data={id:item.id}
							delAddress(data).then(
								res => {
									if(res.code==200){
										 uni.showToast({
										 	icon:'success',
										 	title: "操作成功",
										 });
										 this.getAddressList()
									}else{
										uni.showToast({
											icon:'none',
											title: "请求失败",
										});
									}
								},
								rej => {}
							);
														
						}else{
							// 否则点击了取消  
						}
					}
				})
			},
			getAddressList(){
				uni.showLoading({
					title:"正在加载中"
				})
				getAddressList().then(
					res => {
						uni.hideLoading()
						if(res.code==200){
							this.address=res.data;
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
						}
					},
					rej => {}
				);
			},
			// 新建收货地址
			addAdress() {
				uni.navigateTo({
					url: './addAddress?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: #FC581C;
							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{
						width: 30upx;
						height: 30upx;
						margin-left: 38upx;
						&: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: #FC581C;
				border-radius: 44upx;
			}
		}
	}
</style>
