<template>
	<view>
		<view class="address" v-for="(res, index) in siteList" :key="res.id">
			<u-row>
				<u-col :span="11">
					<view
						class="item"
						@tap="
							pagePathFlag
								? setAddress(res)
								: () => {
										return;
								  }
						"
					>
						<view class="top">
							<u-row style="width: 100%;">
								<u-col :span="5">
									<view class="name">{{ res.name }}</view>
								</u-col>
								<u-col :span="4">
									<view class="phone">{{ res.tel }}</view>
								</u-col>
								<u-col :span="3" style="text-align: end;">
									<u-tag
										v-show="res.isDefault"
										text="默认"
										type="error"
										size="mini"
									/>
								</u-col>
							</u-row>
						</view>
						<view class="bottom">
							{{ res.province + res.city + res.county + res.addressDetail }}
						</view>
					</view>
				</u-col>
				<u-col :span="1">
					<u-icon name="edit-pen" color="#3b3b3b" :size="40" @tap="addressEdit(res)" />
				</u-col>
			</u-row>
		</view>

		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="35"></u-icon>
				新建收货地址
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { onShow } from '@dcloudio/uni-app';
import { addressList } from '@/api/index';

const siteList = ref([] as any);

// 判断上个页面是否是 我的 页面
const pagePathFlag = ref(false);

onShow(() => {
	let pages = getCurrentPages();
	let pagePath: any = pages[pages.length - 2]; //上个页面
	if (pagePath.route === 'pages/profile/index') return (pagePathFlag.value = false);
	else return (pagePathFlag.value = true);
});

const toAddSite = () => {
	uni.navigateTo({
		url: './addSite',
	});
};

// 设置地址
const setAddress = (item: any) => {
	setTimeout(() => {
		uni.setStorage({
			key: 'addressId',
			data: item.id,
			success() {
				let pages = getCurrentPages(); // 当前页面
				let beforePage: any = pages[pages.length - 2]; // 上一页
				console.log(beforePage);
				uni.navigateBack({
					success: function() {
						if (beforePage.route === 'pages/order/quotation')
							beforePage.addressData = item;
						else beforePage.init(); // 执行上一页的onLoad方法
					},
				});
			},
		});
	}, 50);
};

const addressEdit = (res: any) => {
	uni.navigateTo({
		url: `./addSite?data=${JSON.stringify(res)}&editId=1 `,
	});
};

const initAddressLit = () => {
	addressList().then((res: any) => {
		siteList.value = res.data.data.list;
	});
};

initAddressLit();

uni.$on('initAddress', () => {
	initAddressLit();
});
</script>

<style lang="scss" scoped>
page {
	background-color: #fff;
	padding: 0 20rpx;
}

.address {
	padding: 40rpx 20rpx;
}
.item {
	.top {
		font-weight: bold;
		font-size: 34rpx;

		.phone {
			font-size: 32rpx;
		}

		.tag {
			display: flex;
			font-weight: normal;
			align-items: center;

			text {
				display: block;
				width: 60rpx;
				height: 34rpx;
				line-height: 34rpx;
				color: #ffffff;
				font-size: 20rpx;
				border-radius: 6rpx;
				text-align: center;
				margin-left: 30rpx;
				background-color: rgb(49, 145, 253);
			}

			.red {
				background-color: red;
			}
		}
	}

	.bottom {
		display: flex;
		margin-top: 20rpx;
		font-size: 28rpx;
		justify-content: space-between;
		color: #999999;
	}
}

.addSite {
	position: fixed;
	bottom: 30rpx;
	left: 80rpx;
	display: flex;
	justify-content: space-around;
	width: 600rpx;
	line-height: 100rpx;
	// position: absolute;
	background-color: #fa3534;
	border-radius: 60rpx;
	font-size: 30rpx;

	.add {
		display: flex;
		align-items: center;
		color: #ffffff;

		.icon {
			margin-right: 10rpx;
		}
	}
}
</style>
