<template>
	<view :class="['qn-page-' + theme]">
		<view class="now-address">
			<view class="a-title">当前配送至</view>
			<view class="a-sel">
				<text class="ibonfont ibondizhi2"></text>
				<text>{{ now_address.provinceName || '' }}-{{ now_address.cityName || '' }}-{{ now_address.districtName || '' }}</text>
			</view>
		</view>
		<view class="address-view">
			<view class="a-title">从我的收货地址选择</view>
			<ul class="address-ul">
				<li class="address-li" v-for="(item, index) in addressList" :key="index" @click="selAddress(item)">
					<view class="user-info clearfix">
						<text class="float_left">{{ item.name }}</text>
						<text class="float_right">{{ item.mobile }}</text>
					</view>
					<view class="address-de">
						<text v-if="item.defaultStatus === 5" class="default-tag">[默认地址]</text>
						{{ item.area.provinceName }} {{ item.area.cityName }} {{ item.area.districtName }} {{ item.address }}
					</view>
				</li>
			</ul>
		</view>
		<view class="fixed-bottom">
			<RegionSel @pickerRegionChange="pickerRegionChange">
				<view class="bottom-btn">选择其他地区</view>
			</RegionSel>
		</view>
	</view>
</template>

<script>
import RegionSel from '../components/region/RegionSel.vue';
export default {
	components: {
		RegionSel
	},
	data() {
		return {
			now_address: {},
			addressList: [],
			pageTotal: 0,
			page: 1,
			pageSize: 10,
			loading_status: 'loadmore'
		};
	},
	onLoad(options) {
		this.now_address = JSON.parse(options.address_data);
		this.getAllShippingAddress();
	},
	onPullDownRefresh() {
		if (this.pageTotal / this.pageSize > this.page) {
			this.page += 1;
			this.getAllShippingAddress();
		}
	},
	methods: {
		pickerRegionChange(row) {
			this.now_address = {
				provinceCode: row[0].value,
				cityCode: row[1].value || '',
				districtCode: row[2].value || '',
				provinceName: row[0].label || '',
				cityName: row[1].label || '',
				districtName: row[2].label || ''
			};
			this.$api.prePage().now_sel_address = this.now_address;
			uni.navigateBack();
		},
		selAddress(row) {
			this.now_address = {
				provinceCode: row.provinceCode,
				cityCode: row.cityCode,
				districtCode: row.districtCode,
				provinceName: row.area.provinceName,
				cityName: row.area.cityName,
				districtName: row.area.districtName
			};
			this.$api.prePage().now_sel_address = this.now_address;
			uni.navigateBack();
		},
		// 获取客户地址列表 getAllShippingAddress
		getAllShippingAddress() {
			this.loading_status = 'more';
			this.$u.api.getAllShippingAddress({
				page: this.page,
				pageSize: this.pageSize
			}).then(data=>{
				this.loading_status = 'noMore';
				uni.stopPullDownRefresh();
				if (this.page === 1) {
					this.addressList = data.data;
				} else {
					this.addressList = this.addressList.concat(data.data);
				}
				if (this.addressList.length) {
					if(!this.now_address.provinceCode){
						const address = this.addressList.find(item => item.defaultStatus === 5) || this.addressList[0];
						this.now_address = {
							provinceCode: address.provinceCode,
							cityCode: address.cityCode,
							districtCode: address.districtCode,
							provinceName: address.area.provinceName,
							cityName: address.area.cityName,
							districtName: address.area.districtName
						};
					}
				}
				this.pageTotal = data.pageTotal;
			});
		}
	}
};
</script>

<style lang="scss">
body,
pages {
	background-color: #f7f7f7;
}
.a-title {
	font-size: 24upx;
	color: #999999;
	line-height: 66upx;
	padding: 0 24upx;
}
.now-address {
	.a-sel {
		background-color: #ffffff;
		line-height: 80upx;
		padding: 0 24upx;
		.ibonfont {
			font-size: 28upx;
			color: #666666;
			margin-right: 20upx;
		}
	}
}
.address-view {
	.address-ul {
		.address-li {
			background-color: #ffffff;
			padding: 20upx 24upx;
			border-bottom: 1px solid #f7f7f7;
			.user-info {
				font-size: 28upx;
				padding-bottom: 10upx;
			}
			.address-de {
				font-size: 24upx;
				.default-tag {
					margin-right: 10upx;
				}
			}
		}
	}
}
.fixed-bottom {
	position: fixed;
	bottom: 30upx;
	left: 0;
	width: 100%;
}
.bottom-btn {
	width: 680upx;
	line-height: 78upx;
	font-size: 28upx;
	border-radius: 8upx;
	text-align: center;
	color: #ffffff;
	margin: 0 auto;
}
</style>
