<template>
	<view>
		<u-navbar title="我的地址" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		<view class="addres-list">
			<view class="address-item" v-for="(item, index) in list" :key="index">
				<view class="contacts">
					<text class="name">{{ item.receiver }}</text>
					<text class="phone">{{ item.mobile }}</text>
				</view>
				<view class="address">
					<text class="region">{{ `${ item.province }-${ item.city }-${ item.area }` }}</text>
					<text class="detail">{{ item.addr }}</text>
				</view>
				<view class="line"></view>
				<view class="item-option">
					<view class="_left">
						<label class="item-radio" @click.stop="handleSetDefault(item.addrId)">
							<radio class="radio" color="#fa2209" :checked="item.addrId == defaultId"></radio>
							<text class="text">{{ item.addrId == defaultId ? '默认' : '选择' }}</text>
						</label>
					</view>
					<view class="_right">
						<view class="events">
							<view class="event-item" @click="handleUpdate(item.addrId)">
								<text class="iconfont icon-edit"></text>
								<text class="title">编辑</text>
							</view>
							<view class="event-item" @click="handleRemove(item.addrId)">
								<text class="iconfont icon-delete"></text>
								<text class="title">删除</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-empty text="亲，暂无收货地址" marginTop="100" mode="data" v-if="!list.length" />
		<!-- 底部操作按钮 -->
		<view class="footer-fixed">
			<view class="btn-wrapper">
				<button type="default" @click="handleCreate">添加地址</button>
			</view>
		</view>
	</view>
</template>

<script>
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				//当前页面参数
				options: {},
				// 正在加载
				isLoading: true,
				// 收货地址列表
				list: [],
				// 默认收货地址
				defaultId: null
			}
		},

		onLoad(options) {
			// 当前页面参数
			this.options = options

		},

		onShow() {
			// 获取页面数据
			this.getPageData()
		},

		methods: {
			// 获取页面数据
			getPageData() {
				uni.showLoading({
					title: '加载中'
				});
				const app = this
				app.isLoading = true
				Promise.all([app.getDefaultId(), app.getAddressList()])
					.then(() => {
						uni.hideLoading();
						// 列表排序把默认收货地址放到最前
						// app.onReorder()
					}).catch(() => {
						uni.hideLoading();
					})
			},

			// 获取收货地址列表
			getAddressList() {
				$http.post('/api/v1/mp/user/addr/list', {}).then(res => {
					if (res.code == 0) {
						this.list = res.data
					}
				})
			},

			// 获取默认的收货地址
			getDefaultId() {
				$http.post('/api/v1/mp/user/addr/queryDefault', {}).then(res => {
					if (res.code == 0) {
						this.defaultId = res && res.data && res.data.addrId
					}
				})
			},

			// 列表排序把默认收货地址放到最前
			onReorder() {
				const app = this
				app.list.sort(item => {
					return item.addrId == app.defaultId ? -1 : 1
				})
			},

			// 添加地址
			handleCreate() {
				uni.navigateTo({
					url: "/packageOperate/address/create"
				})
			},

			// 编辑地址
			handleUpdate(addressId) {
				uni.navigateTo({
					url: `/packageOperate/address/create?addrId=${ addressId }`
				})
			},

			// 删除地址
			handleRemove(addressId) {
				const _this = this
				uni.showModal({
					title: "提示",
					content: "您确定要删除当前收货地址吗?",
					success(res) {
						if (res.confirm) {
							$http.post('/api/v1/mp/user/addr/remove', {
								addrId: addressId
							}).then(res => {
								if (res.code == 0) {
									_this.getPageData()
									uni.$u.toast('删除成功');
								}
							})
						}
					}
				});
			},

			// 设置默认地址
			handleSetDefault(addressId) {
				$http.post('/api/v1/mp/user/addr/setDefault', {
					addrId: addressId
				}).then(res => {
					this.getPageData()
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.addres-list {
		padding-bottom: calc(constant(safe-area-inset-bottom) + 120rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 120rpx);
	}

	.address-item {
		margin: 20rpx auto 20rpx auto;
		padding: 30rpx 40rpx;
		width: 95%;
		box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
		border-radius: 10rpx;
		background: #fff;
	}

	.contacts {
		font-size: 30rpx;
		margin-bottom: 16rpx;

		.name {
			margin-right: 16rpx;
		}
	}

	.address {
		font-size: 28rpx;

		.region {
			margin-right: 10rpx;
		}
	}

	.line {
		margin: 20rpx 0;
		border-bottom: 1rpx solid #f3f3f3;
	}

	.item-option {
		display: flex;
		justify-content: space-between;
		height: 48rpx;

		// 单选框
		.item-radio {
			font-size: 28rpx;

			.radio {
				vertical-align: middle;
				transform: scale(0.76)
			}

			.text {
				vertical-align: middle;
			}
		}

		// 操作
		.events {
			display: flex;
			align-items: center;
			line-height: 48rpx;

			.event-item {
				font-size: 28rpx;
				margin-right: 26rpx;
				color: #4c4c4c;

				&:last-child {
					margin-right: 0;
				}

				.title {
					margin-left: 8rpx;
				}
			}
		}

	}


	// 底部操作栏
	.footer-fixed {
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;
		right: 0;
		min-height: 120rpx;
		z-index: 11;
		box-shadow: 0 -4rpx 40rpx 0 rgba(151, 151, 151, 0.24);
		background: #fff;

		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		.btn-wrapper {
			display: flex;
			align-items: center;
			padding: 20rpx 0;
		}

		/deep/ button {
			width: 640rpx;
			height: 90rpx;
			line-height: 90rpx;
			font-size: 36rpx;
			color: #fff;
			background-color: $uni-bg-color;
			border: none;
			border-radius: 20rpx;
		}
	}
</style>
