<template>
	<view class="address-manager">
		<!-- 地址列表 -->
		<view class="address-list">
			<!-- 地址项1 - 默认地址 -->
			<view v-for="(item,index) in addresses" :key="index" class="address-item">
				<view class="address-content">
					<view class="name-phone">
						<text class="name">{{item.realName }}</text>
						<text class="phone">{{phone(item.phone) }}</text>
						<view v-if="item.isDefault==1" class="default-tag">默认</view>
					</view>
					<text class="address">{{ item.province+item.city+item.region+item.address  }}</text>
				</view>
				<view class="" style="display: flex;align-items: center; gap: 15rpx;  ">
					<up-icon @click="Service.GoPage('/pages/userFunc/addAddress?addressId='+item.addressId)"
						name="edit-pen" size="20"></up-icon>
					<up-icon @click="dele(item.addressId)" name="trash" size="20"></up-icon>
				</view>
			</view>
		</view>
		<up-loadmore :status="status" />
		<view class="" style="width: 100%; height: 120rpx;">

		</view>

		<!-- 新增地址按钮 -->
		<view class=""
			style=" background-color: #fff; border-top: 1rpx solid #e2e2e2; position: fixed; left: 0; bottom: 0; width: 100vw;  height: 110rpx; ">
			<up-button @click="Service.GoPage('/pages/userFunc/addAddress')" color='var(--nav-mian)' shape='circle'
				style="width: 90%; margin: 15rpx auto 0; " text="新增地址"></up-button>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { ref } from 'vue';
	import { Service } from "@/Service/Service"
	import { onShow, onLoad, onReachBottom } from "@dcloudio/uni-app";
	import { AddressService } from "@/Service/api/AddressService"

	// 地址数据
	const addresses = ref<Array<any>>([
	]);

	let page = ref(1)
	let status = ref('nomore')

	onLoad(() => {

	});

	onShow(() => {
		getData()
	});

	onReachBottom(() => {
		getList()
	})

	const getData = () => {
		page.value = 1
		status.value = 'loadmore'
		addresses.value = []
		getList()
	}

	const getList = () => {
		if (status.value == 'loading' || status.value == 'nomore') {
			return
		}
		status.value = 'loading'
		AddressService.GetUserAddressList(page.value).then(res => {
			if (res.code == 0) {
				addresses.value = [...addresses.value, ...res.data.list]
				status.value = res.data.list.length == 10 ? 'loadmore' : 'nomore'
				page.value++
			} else {
				Service.Msg(res.msg)
			}
		})
	}

	const phone = (data : any) => {
		return String(data).slice(0, 3) + '****' + String(data).slice(-4)
	}

	const dele = (id : any) => {
		uni.showModal({
			title: '提示', // 对话框标题
			content: '是否删除该地址', // 显示的内容
			success: function (res) {
				if (res.confirm) {
					AddressService.DelUserAddress(id).then(res => {
						if (res.code == 0) {
							Service.Msg('删除成功!')
						} else {
							Service.Msg(res.msg)
						}
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	}
</script>

<style scoped lang="scss">
	page {
		background-color: #f5f5f5;
	}

	.address-manager {
		padding: 20rpx;
	}

	/* 提示信息样式 */
	.tip-bar {
		background-color: #fff8e6;
		padding: 20rpx 30rpx;
		display: flex;
		align-items: center;

		.info-icon {
			color: #ff9c07;
			margin-right: 10rpx;
		}

		.tip-text {
			font-size: 26rpx;
			color: #ff9c07;
			flex: 1;
		}
	}

	/* 地址列表样式 */
	.address-list {

		.address-item {
			border-radius: 15rpx;
			padding: 30rpx;
			background-color: #fff;
			margin-top: 20rpx;
			display: flex;
			justify-content: space-between;
			align-items: flex-start;
			border-bottom: 1rpx solid #f5f5f5;

			&:last-child {
				border-bottom: none;
			}

			.address-content {
				flex: 1;

				.name-phone {
					display: flex;
					align-items: center;
					margin-bottom: 10rpx;

					.name {
						font-size: 28rpx;
						color: #333;
						font-weight: 500;
						margin-right: 20rpx;
					}

					.phone {
						font-size: 26rpx;
						color: #666;
						margin-right: 15rpx;
					}

					.default-tag {
						background-color: #ff6b00;
						color: #fff;
						font-size: 20rpx;
						padding: 2rpx 15rpx;
						border-radius: 20rpx;
					}
				}

				.address {
					font-size: 26rpx;
					color: #666;
					line-height: 1.5;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
					margin-top: 20rpx;
				}
			}

			.edit-icon {
				width: 30rpx;
				height: 30rpx;
				color: #999;
				margin-top: 5rpx;
			}
		}
	}

	/* 新增地址按钮样式 */
	.add-btn {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 100rpx;
		background-color: #ff6b00;
		color: #fff;
		font-size: 32rpx;
		font-weight: 500;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 0;
	}
</style>