<template>
	<view class="container">
		<view class="address-list">
			<u-swipe-action v-for="(item, index) in addressList" :key="index" :index="index" @click="deleteAddress" :options="options">
				<view class="address-row" @click="setAddress(item)">
					<view class="address-first-name" :class="item.isDefault == 1 ? 'address-first-name-active' : ''">{{item.username ? item.username.slice(1,2) : ''}}</view>
					<view class="address-info">
						<view class="address-name-phone">
							<text class="address-name">{{item.username}}</text>
							<text class="address-phone">{{item.phone}}</text>
						</view>
						<view class="address-type-text">
							<view class="address-type" :class="item.isDefault == 1 ? 'address-type-active' : ''">{{item.isDefault == 1 ? '默认' : item.tag}}</view>
							<text class="address-text">{{item.province}},{{item.city}},{{item.area}},{{item.address}}</text>
						</view>
					</view>
					<view class="address-edit" @click.stop="editAddress(item)">编辑</view>
				</view>
			</u-swipe-action>
		</view>
		<view class="address-handle">
			<u-button type="success" shape="circle" :ripple="true" @click="addAddress" class="handle-custom">新增地址</u-button>
		</view>
		<u-modal v-model="modalShow" content="是否将删除该地址" @confirm="submitDelete" :async-close="true" :show-cancel-button="true"></u-modal>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		data() {
			return {
				pageType: 1,
				addressList: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
				modalShow: false,
			}
		},
		onLoad(options) {
			this.pageType = options.type
		},
		onShow() {
			this.getAddress()
		},
		methods: {
			//  查询地址
			getAddress() {
				NET.request(API.getAddressList, {}, 'POST').then(res => {
					this.addressList = res.data
				}).catch(error => {
					this.$refs.uTips.show({
						title: '获取默认地址失败',
						type: 'warning',
					})
				})
			},
			//  设置地址
			setAddress(item) {
				if (this.pageType == 2) {
					uni.setStorage({
						key: 'defaultAddress',
						data: {
							memberAddressId: item.memberAddressId,
							username: item.username,
							phone: item.phone,
							province: item.province,
							city: item.city,
							area: item.area,
							address: item.address,
						}
					});
					uni.navigateBack()
				}
			},
			//  删除地址
			deleteAddress(index, index1) {
				this.addressId = this.addressList[index].id
				this.modalShow = true
			},
			//  提交删除地址
			submitDelete() {
				NET.request(API.deleteAddress + this.addressId, {}, 'GET').then(res => {
					this.getAddress()
					this.modalShow = false
				}).catch(error => {
					this.modalShow = false
					this.$refs.uTips.show({
						title: '删除该地址失败',
						type: 'warning',
					})
				})
			},
			//  编辑地址
			editAddress(item) {
				uni.navigateTo({
					url: '/pagesMain/addressForm?addressId=' + item.id
				});
			},
			//  新增地址
			addAddress() {
				uni.navigateTo({
					url: '/pagesMain/addressForm'
				});
			}
		},
	}
</script>

<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		float: left;
		box-sizing: border-box;
		padding-bottom: 70px;
		overflow-y: auto;

		.address-list {
			width: 100%;
			float: left;

			.address-row {
				width: 100%;
				height: 77px;
				float: left;
				border-bottom: 1px solid #DBDBDB;
				box-sizing: border-box;
				padding: 20px 15px;

				.address-first-name {
					width: 36px;
					height: 36px;
					float: left;
					background: #656565;
					border-radius: 50%;
					color: #FFFFFF;
					font-size: 18px;
					font-family: PingFang SC;
					text-align: center;
					line-height: 36px;
				}

				.address-first-name-active {
					background: #51A539;
				}

				.address-info {
					width: calc(100% - 103px);
					height: 36px;
					float: left;
					margin: 0 10px 0 15px;

					.address-name-phone {
						width: 100%;
						height: 16px;
						float: left;
						line-height: 16px;
						font-family: PingFang SC;

						.address-name {
							color: #333333;
							font-size: 15px;
							margin-right: 16px;
						}

						.address-phone {
							color: #656565;
							font-size: 12px;
						}
					}

					.address-type-text {
						width: 100%;
						height: 16px;
						margin-top: 4px;
						float: left;
						line-height: 16px;
						font-family: PingFang SC;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;

						.address-type {
							height: 16px;
							padding: 0 6px;
							display: inline-block;
							border-radius: 4px;
							font-size: 12px;
							color: #FFFFFF;
							background: #656565;
							margin-right: 6px;
						}

						.address-type-active {
							background: #51A539;
						}

						.address-text {
							font-size: 12px;
							color: #333333;
						}
					}
				}

				.address-edit {
					width: 42px;
					height: 26px;
					margin: 5px 0;
					float: left;
					border-left: 1px solid #DBDBDB;
					line-height: 26px;
					text-align: right;
					font-size: 12px;
					font-family: PingFang SC;
					color: #656565;
				}
			}
		}

		.address-handle {
			width: calc(100% - 30px);
			height: 40px;
			position: fixed;
			bottom: 20px;
			left: 15px;

			.handle-custom {
				background-color: #51A539;
			}
		}
	}
</style>
