<template>
	<view class="addressList">
		<view style="overflow: hidden;" v-if="addressList.length>0">
			<u-swipe-action :show="item.show" :index="index" v-for="(item, index) in addressList" :key="item.address_id"
				@click="clickDel(item)" @open="open" :options="options">
				<view class="title-wrap">
					<view class="item" :style="{'border-top':index==0?'none':'1px solid #EEEEEE'}">
						<view class="a">{{item.name}} {{item.phone}}<text v-if="item.address_id==defaultId">默认</text>
						</view>
						<view class="b">
							<view class="d">
								{{item.detail}}
							</view>
							<image src="/static/order/edit.png" @click="editAddress(item)"></image>
						</view>
					</view>
				</view>
			</u-swipe-action>
		</view>
		<u-empty text="暂无数据显示哦～" v-else mode="list"></u-empty>
		<view class="footer">
			<view class="operaBtn" @click="addAddress">
				新增收货地址
			</view>
		</view>
		<u-toast ref="uToast" />
		<u-modal v-model="delShow" content="确定删除选中商品吗" :show-cancel-button="true" @cancel="show=false"
			@confirm="delConfirm"></u-modal>
	</view>
</template>

<script>
	import * as AddressApi from '@/api/address'
	export default {
		data() {
			return {
				addressList: [],
				defaultId: '',
				disabled: false,
				btnWidth: 180,
				show: false,
				delShow: false,
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
				addressId: ''
			};
		},
		onShow() {
			this.getAddressList()
			this.getDefault()
		},
		methods: {
			getDefault() {
				// 获取默认地址接口
				const that = this
				return new Promise((resolve, reject) => {
					AddressApi.defaultId()
						.then(res => {
							that.defaultId = res.data.defaultId
						})
						.catch(reject)
				})
			},
			open(index) {
				this.addressList[index].show = true;
				this.addressList.map((val, idx) => {
					if (index != idx) this.addressList[idx].show = false;
				})
			},
			clickDel(item) {
				this.addressId = item.address_id
				this.delShow = true;
			},
			async delConfirm() {
				// 设置默认地址
				const that = this
				AddressApi.remove(that.addressId)
					.then(result => {
						that.$toast(result.message);
						this.delShow = false;
						that.getAddressList()
					})
			},
			getAddressList() {
				const that = this
				return new Promise((resolve, reject) => {
					AddressApi.list()
						.then(result => {
							result.data.list.forEach(item => {
								item.show = false
							});
							that.addressList = result.data.list
						})
						.catch(reject)
				})
			},
			editAddress(item) {
				// 编辑收货地址
				uni.navigateTo({
					url: '/pages/news/park/addressEdit?addressDetail=' + JSON.stringify(item)
				})
			},
			addAddress() {
				uni.navigateTo({
					url: '/pages/news/park/addressEdit'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-empty {
		padding: 100rpx 0;
	}

	.addressList {
		padding: 0 45rpx;
		background-color: #FFFFFF;
		overflow: hidden;
		margin-top: 2rpx;

		.item {
			padding: 30rpx 0;
			overflow: hidden;
			border-top: 1px solid #EEEEEE;

			.a {
				font-size: 32rpx;
				font-weight: 500;
				color: #1E1E1E;
				padding-bottom: 25rpx;
				display: flex;
				align-items: center;

				text {
					width: 66rpx;
					line-height: 34rpx;
					background: #F32020;
					border-radius: 4rpx;
					text-align: center;
					display: block;
					margin-left: 10rpx;
					font-size: 24rpx;
					font-weight: 400;
					color: #FFFFFF;
					border-radius: 5rpx;
				}
			}

			.b {
				display: flex;
				align-items: flex-start;
				justify-content: space-between;

				.d {
					font-size: 32rpx;
					font-weight: 400;
					color: #979797;
					max-width: 600rpx;
				}

				image {
					width: 36rpx;
					height: 36rpx;
					margin-right: 10rpx;
				}
			}
		}
	}

	.footer {
		display: flex;
		width: 100%;
		height: 120rpx;
		justify-content: center;
		position: fixed;
		bottom: 0;
		left: 0;

		.operaBtn {
			width: 686rpx;
			height: 92rpx;
			background: #F34A40;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			opacity: 1;
			line-height: 92rpx;
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 600;
			color: #FFFFFF;
			text-align: center;
		}
	}
</style>
