<template>
	<view class="app-container">
		<view class="list-container">
			<block v-for="(item, index) in addressList" :key="index">
				<view class="item" @tap="chooseAddress(index)">
					<view class="top">
						<view class="left">
							<view class="name">{{ item.name }}</view>
							<view class="phone">{{ item.phone }}</view>
							<view class="default" v-if="item.defaultAddr">默认</view>
						</view>
						<image class="right" src="@/static/icon/edit.png" @tap.stop="toEditAddress(item.userAddressId)">
						</image>
					</view>
					<view class="bottom">{{ item.province }}{{ item.city }}{{ item.area }}{{ item.street
            }}{{ item.address }}{{ item.houseNo }}</view>
				</view>
			</block>
		</view>
		<no-data v-if="!addressList.length" :showText="'暂无收货地址'"></no-data>

		<view class="bottom-container">
			<view class="button red" @tap="toAddAddress">新增收货地址</view>
			<view class="button white" @tap="getWxAddress">获取收货地址</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import EventBus from '@/utils/eventbus.js';

	export default {
		data() {
			return {
				addressList: [], // 收货地址列表
				isChoose: false, // 是否进来选择地址
			};
		},
		computed: {
			...mapState(['userInfo', 'isLogin', 'userId']),
		},
		onShow() {
			this.getAddressList();
		},
		onLoad({
			isChoose
		}) {
			this.isChoose = isChoose;
		},
		methods: {
			// 获取收货地址列表
			getAddressList() {
				this.$axios({
					url: '/user/address/list',
					method: 'get',
					params: {
						pageNum: 1,
						pageSize: 100,
						userId: this.userId,
					},
				}).then((res) => {
					this.addressList = res.data.records;
				});
			},

			// 选择地址
			chooseAddress(index) {
				if (!this.isChoose) {
					return false;
				}
				EventBus.$emit('chooseAddress', this.addressList[index]);
				uni.navigateBack({
					delta: 1,
				});
			},

			// 获取微信收货地址
			getWxAddress() {
				let that = this;
				uni.chooseAddress({
					success(res) {
						let params = {
							userId: that.userId,
							name: res.userName,
							phone: res.telNumber,
							province: res.provinceName,
							city: res.cityName,
							area: res.countyName,
							address: res.detailInfo,
							houseNo: '',
						};
						uni.navigateTo({
							url: '/pages/mine/address/form?addressData=' + JSON.stringify(params),
						});

						// that.$axios({
						// 	url: '/user/address/save',
						// 	type: 'application/json',
						// 	params,
						// 	isLoading: 1,
						// }).then(res => {
						// 	that.getAddressList();
						// 	that.$successToast('添加成功');
						// })
					},
				});
			},

			// 去新增收货地址
			toAddAddress() {
				uni.navigateTo({
					url: '/pages/mine/address/form',
				});
			},

			// 去编辑收货地址
			toEditAddress(id) {
				uni.navigateTo({
					url: '/pages/mine/address/form?id=' + id,
				});
			},
		},
	};
</script>

<style lang="scss">
	.app-container {
		background: #f4f2f2;
		padding: 20rpx;
		padding-bottom: 250rpx;
		box-sizing: border-box;
	}

	.list-container {
		.item {
			background: #ffffff;
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			padding: 20rpx;

			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.left {
					display: flex;
					align-items: flex-end;

					.name {
						font-size: 32rpx;
						color: #333333;
						line-height: 32rpx;
					}

					.phone {
						font-size: 28rpx;
						color: #666666;
						margin-left: 20rpx;
						line-height: 28rpx;
					}

					.default {
						font-size: 22rpx;
						color: #ffffff;
						background: #fe781f;
						padding: 6rpx 8rpx;
						line-height: 20rpx;
						margin-left: 20rpx;
					}
				}

				.right {
					width: 32rpx;
					height: 32rpx;
				}
			}

			.bottom {
				font-size: 28rpx;
				color: #333333;
				line-height: 38rpx;
				margin-top: 20rpx;
			}
		}
	}

	.bottom-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;
		padding: 20rpx 20rpx 60rpx;
		background: #f4f2f2;

		.button {
			width: 100%;
			height: 72rpx;
			border-radius: 72rpx;
			text-align: center;
			line-height: 72rpx;

			&.red {
				background: #ff3f42;
				color: #ffffff;
				margin-bottom: 20rpx;
			}

			&.white {
				background: #ffffff;
				color: #333333;
			}
		}
	}
</style>