<template>
	<view class="address">
		<u-radio-group v-model="current" @change="changeDefault" size="29" active-color="#FF780F">
			<u-empty text="暂无收货地址" mode="address" color="#000000" img-width="120" margin-top="300" v-if="!addressList.length"></u-empty>
			<view v-for="(item, index) in addressList" :key="index" class="address-item">
				<AddressItem :item="item" :current='current' @getAddressList="getAddressList" :state="state"></AddressItem>
			</view>
		</u-radio-group>
		<view class="address-btn" @click="addAddress">添加地址</view>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
import AddressItem from '@/components/mine/address-block/address-item'
export default {
	data() {
		return {
			current: -1, // radio 标记
			addressList: [],
			state: '', // 页面状态 是否进页面选择地址
		}
	},
	components: {
		AddressItem
	},
	onLoad(option) {
		if(option.type) this.state = option.type;
	},
	onShow() {
		this.getAddressList();
	},
	methods: {
		getAddressList() {
			this.$u.api.getAddressList().then((res)=>{
				if (res.errCode == 0) {
					this.addressList = res.data.addressList;
					let defaultAddress = this.addressList.filter(item => {
						return item.address_is_default == '1';
					})
					if(defaultAddress[0]) this.current = defaultAddress[0].address_id;
				}
			})
		},
		addAddress() {
			uni.navigateTo({
				url: '/pageE/more/AddAddress'
			});
		},
		// 设置默认地址
		changeDefault(id) {
			// console.log(id)
			this.$u.api.setDefaultAddress({
				address_id: id
			}).then(res => {
				if(res.errCode == 0) {
					this.getAddressList();
				} else {
					this.$refs.uToast.show({
						title: res.message,
						type: 'warning',
					})
				}
			})
		},
	},
}
</script>
<style lang="scss" scoped>
.address {
	min-height: calc(100vh - var(--window-top));
	background-color: #ECECEC;
	padding: 1rpx 0 40rpx;
	box-sizing: border-box;
	.u-radio-group {
		display: block;
		.address-item {
			background-color: #ffffff;
			margin-bottom: 20rpx;
			padding: 30rpx;
		}
	}
	.address-btn {
		margin: 260rpx auto 0;
		width: 690rpx;
		height: 98rpx;
		background: rgba(255,119,15,1);
		border-radius: 49rpx;
		font-size: 36rpx;
		color: rgba(255,255,255,1);
		line-height: 98rpx;
		text-align: center;
	}
}
</style>