<template>
	<view class="address">
		<view class="main">
			<view class="content" v-for="(item,index) in  addressList" :key="index">
				<view class="list">
					<div class="left">
						<van-checkbox checked-color="#f00" @change="onChange" :data-param2="item" :value="item.commonAddr">
						</van-checkbox>
					</div>
					<div class="mid">
						<div class="info">
							{{item.receiver + " " + item.mobile}}
							<van-tag type="danger" v-if="item.commonAddr == 1" round class="tags">默认</van-tag>
						</div>
						<div class="site  van-ellipsis">{{item.province + "" + item.city + "" + item.area + "" + item.addr}}
						</div>
					</div>
					<div class="right">
						<van-icon name="edit" @click="editAddress(item)" />
					</div>
				</view>
			</view>
			
		</view>
		<van-button type="danger" class="btn" round size="large" @click="addAddress">新增地址</van-button>
	</view>
</template>

<script>
	import {
		addressList,
		defaultAddr
	} from '@/api/address.js'
	export default {
		data() {
			return {
				addressList: []
			}
		},
		onShow() {
			this.init();
		},
		methods: {
			init() {
				console.log("走进来")
				addressList().then(res => {
					this.addressList = res;
				})
			},
			onChange(event) {
				let item = event.currentTarget.dataset.param2;
				defaultAddr(item.addrId).then(res => {
					this.init()
				})
			},
			addAddress() {
				uni.navigateTo({
					url: "/pages/addressList/addressList"
				})
			},
			editAddress(event) {
				uni.navigateTo({
					url:"/pages/addressList/addressList?data=" + JSON.stringify(event)
				})
			}
		}
	}
</script>

<style lang="scss">
	.address {
		width: 100%;

		.content {
			.list {
				display: flex;
				height: 150rpx;
				align-items: center;

				.left {
					flex: 1;
					display: flex;
					justify-content: center;
				}

				.van-ellipsis {
					width: 330px;
				}

				.mid {
					flex: 8;

					.tags {
						margin-left: 13rpx;
					}

					.site {
						margin-top: 13px;
					}
				}

				.right {
					flex: 1;
				}
			}
		}

		.btn {
			width: 90%;
			position: fixed;
			bottom: 10rpx;
			left: 50%;
			margin-left: -45%;
		}
	}
	.main {
		padding-bottom: 100rpx;
	}
</style>
