<template>
	<view>
		<view class="address_item" v-for="(item,index) in list" :key="index">
			<view class="address_info">
				<view class="address_info_title">
					<view class="info_title_span">{{item.consignee}}</view>
					<text>{{item.mobile}}</text>
				</view>
				<view class="address_info_content">
					<view class="info_content_left">
						<view>{{item.sign_building}} </view>
						<text>{{item.country_name}} {{item.province_name}} {{item.city_name}} {{item.address}} {{item.mobile}}</text>
					</view>
					<image src="../../static/img/update.png" mode="" @click="updateAddress(item)"></image>
				</view>
			</view>
			
			<view class="address_default">
				<view class="address_default_left">
					<image src="../../static/img/select4.png" mode="" v-if="item.default_address_id==item.address_id"></image>
					<image src="../../static/img/select2.png" mode="" v-else @click="setDefault(item.address_id)"></image>
					<text>Default address</text>
				</view>
				<text @click="deleteAddress(item.address_id)">Delete</text>
			</view>
			
		</view>
		<view class="address_foot">
			<navigator url="/pages/account/addAddress" >
				<button>ADD ADDRESS</button>
			</navigator>
		</view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="error" cancelText="Close" confirmText="Delete" title="Tips" content="Whether to delete the address?" 
			@confirm="dialogConfirm" ></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {defaultRequest2} from '../../api/index.js'
	export default {
		data() {
			return {
				list:[],
				address_id:''
			}
		},
		methods:{
			// 修改地址
			updateAddress(item){
				uni.navigateTo({
					url:'./addAddress?type=1&data='+JSON.stringify(item)
				})
			},
			// 删除地址
			deleteAddress(id){
				this.address_id=id
				this.$refs.alertDialog.open()
			},
			dialogConfirm(){
				let data={_action:'dropconsignee',address_id:this.address_id}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						uni.showToast({
							icon:'none',
							title:res.message
						})
						this.getList()
					}
				})
			},
			// 设置默认地址
			setDefault(id){
				let data={_action:'setdefaultconsignee',address_id:id}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						this.getList()
					}
				})
			},
			getList(){
				let data={_action:'getconsignees'}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						this.list=res.data
					}
				})
			}
		},
		onShow() {
			this.getList()
		}
	}
</script>
<style>
	page{
		background-color: #F6F5FA;
		padding-bottom: 160rpx;
	}
</style>
<style lang="scss" scoped>
	
	.address_foot{
		padding: 42rpx 24rpx;
		background-color: white;
		border-top: #E2E2E2 solid 1px;
		position: fixed;
		bottom: 0;
		width: 100%;
		box-sizing: border-box;
		button{
			color: white;
			background-color: black;
			font-size: 26.67rpx;
			font-weight: bold;
			margin: 0;
		}
	}
	.address_item{
		margin-top: 17.33rpx;
		width: 100%;
		background-color: white;
		padding-top: 32rpx;
		font-size: 24rpx;
		color: #666666;
		font-weight: bold;
		
		.address_info{
			padding: 0 26.67rpx;
			padding-bottom: 33.33rpx;
			border-bottom: 1px solid #E2E2E2;
			
			.address_info_title{
				display: flex;
				align-items: center;
				margin-bottom: 37.33rpx;
				.info_title_span{
					font-size: 28rpx;
					color: black;
					margin-right: 38.67rpx;
				}
			}
			.address_info_content{
				display: flex;
				align-items: center;
				width: 100%;
				.info_content_left{
					padding-right: 38.67rpx;
					margin-right: 34rpx;
					min-width: 80%;
					border-right: 1px solid #E2E2E2;
				}
				image{
					width: 34.67rpx;
					height: 34.67rpx;
				}
			}
		}
		.address_default{
			padding: 29.33rpx 0;
			padding-left: 26.67rpx;
			padding-right: 52.67rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			
			.address_default_left{
				display: flex;
				align-items: center;
				
				image{
					width: 25.33rpx;
					height: 25.33rpx;
					margin-right: 13.33rpx;
				}
			}
		}
	}
</style>