<template>
	<view class="address-list">
		<!-- 顶部导航栏 -->
		<custom-navbar title="常用地址" :show-back="true"></custom-navbar>

		<!-- 地址列表 -->
		<scroll-view scroll-y class="address-scroll">
			<view class="address-list-content">
				<!-- 地址项 -->
				<view class="address-item" v-for="(item, index) in addressList" :key="index"
					@click="selectAddress(item)" :class="{'no-select': !item.is_ok}">
					<view class="address-info">
						<view class="address-header">
							<text class="name">{{item.name}}</text>
							<text class="phone">{{item.call_phone}}</text>
							<!-- <text class="tag" v-if="item.isDefault">宜兴埠 商圈</text> -->
						</view>
						<view class="address-detail">
							<text class="detail-title">{{item.house_number}}</text>
							<text class="detail-desc">{{item.server_address}}</text>
						</view>
					</view>
					<view class="address-actions">
						<view class="default-check" @click.stop="toggleDefault(item)">
							<image src="/static/images/agree_n.png" class="agree-btn" mode="aspectFit"
								v-if="item.is_default == 2"></image>
							<image src="/static/images/agree_y.png" class="agree-btn" mode="aspectFit" v-else></image>
							<text class="text-default">默认地址</text>
						</view>
						<view class="action-btns">
							<!-- <view class="action-item" @click.stop="editAddress(item)">
                <image src="/static/images/edit_icon.png" mode="aspectFit" class="action-icon"></image>
                <text class="text-default">修改</text>
              </view> -->
							<view class="action-item" @click.stop="reviseAddress(item)">
								<image src="/static/images/revise.png" mode="aspectFit" class="action-item__revise">
								</image>
								<text class="text-default">修改</text>
							</view>
							<view class="action-item" @click.stop="deleteAddress(item)">
								<image src="/static/images/delete_icon.png" mode="aspectFit"
									class="action-item__delect">
								</image>
								<text class="text-default">删除</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 添加地址按钮 -->
		<view class="fixed-kong"></view>
		<view class="bottom-fixed">
			<view class="add-btn" @click="addAddress">
				<text class="add-text">+添加新地址</text>
			</view>
		</view>
	</view>
</template>

<script>
	import request from '../../utils/request'
	export default {
		data() {
			return {
				scrollHeight: 0,
				isAgree: false,
				addressList: [],
				id: '',
				setAdressType:1,//存储地址信息方法的类型1，2
			}
		},
		onLoad(options) {
			if(options.setAdressType){
				this.setAdressType = options.setAdressType
			}
			// 计算滚动区域高度
			this.calculateScrollHeight()
			// 获取地址列表
			this.id = options.id;
			this.getAddressList()
		},
		methods: {
			// 计算滚动区域高度
			calculateScrollHeight() {
				const systemInfo = uni.getSystemInfoSync()
				const windowHeight = systemInfo.windowHeight
				const statusBarHeight = systemInfo.statusBarHeight
				const navBarHeight = 44 // 导航栏高度
				const bottomHeight = 100 // 底部按钮高度

				this.scrollHeight = windowHeight - statusBarHeight - navBarHeight - bottomHeight
			},

			// 获取地址列表
			getAddressList() {
				// TODO: 调用接口获取地址列表
				// this.addressList = res.data
				request.post('/user/addresslist', {
					id: this.id
				}).then(res => {
					console.log(res)
					this.addressList = res.data;
				})
			},

			// 选择地址
			selectAddress(item) {
				if (!item.is_ok) return;
				// TODO: 处理地址选择逻辑
				console.log('选择地址:', item)
				// 返回上一页并传递选中的地址信息
				const pages = getCurrentPages()
				const prevPage = pages[pages.length - 2]

				if (prevPage) {
					// if (prevPage.route === 'pages/order/submit') {
					// 	prevPage.$vm.addReservation(item)
					// 	uni.navigateBack()
					// }
					console.log('上一页路径：', prevPage.route);
					if (prevPage.$vm && typeof prevPage.$vm.addReservation === "function") {
           				 prevPage.$vm.addReservation(item); // 存在则调用
          			}
					if(this.setAdressType==1){
						 this.$store.commit("setreservationAddress",item)
					}else if(this.setAdressType==2){
						 this.$store.commit("setreservationAddress2",item)
					}
					
					uni.navigateBack()

				}

			},

			// 编辑地址
			editAddress(item) {
				uni.navigateTo({
					url: `/pages/address/edit?id=${item.id}`
				})
			},

			// 删除地址
			deleteAddress(item) {
				uni.showModal({
					title: '提示',
					content: '确定要删除该地址吗？',
					success: (res) => {
						if (res.confirm) {
							// TODO: 调用删除接口
							console.log('删除地址:', item)
							request.post('/user/deleteaddress', {
								id: item.id
							}).then(res => {
								if (res.state == 1) {
									this.getAddressList();
									uni.showToast({
										title: '地址删除成功',
										icon: 'none'
									})
								}
							})
						}
					}
				})
			},

			// 添加地址
			addAddress() {
				uni.navigateTo({
					url: '/pages/address/add'
				})
			},

			//
			reviseAddress(item) {
				uni.navigateTo({
					url: `/pages/address/revise?data=${encodeURIComponent(JSON.stringify(item))}`
				})
			},

			// 切换默认地址
			toggleDefault(item) {
				// TODO: 处理切换默认地址逻辑
				console.log('切换默认地址:', item)
				if (item.is_default == 2) {
					request.post('/user/editdefaultaddress', {
						id: item.id
					}).then(res => {
						if (res.state == 1) {
							this.getAddressList()
							uni.showToast({
								title: '默认地址切换成功',
								icon: 'none'
							})
						}
					})
				}
			}
		}
	}
</script>

<style scoped lang="less">
	.address-list-content {
		padding-bottom: 32rpx;
	}

	.address-list {
		background-color: #fff;
		min-height: 100vh;
	}
	

	.address-item {
		background-color: #FFFFFF;
		margin: 0 30rpx;
		padding: 32rpx 0rpx 32rpx 0rpx;
		margin-bottom: 8rpx;
		border-bottom: 1px solid rgba(0, 0, 0, 0.05);

		.address-info {
			.address-header {
				display: flex;
				align-items: center;
				margin-bottom: 18rpx;

				.name {
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #333333;
					line-height: 37rpx;
					text-align: left;
					font-style: normal;
					margin-right: 14rpx;
				}

				.phone {
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #333333;
					line-height: 37rpx;
					text-align: left;
					font-style: normal;
					flex: 1;
				}
			}
		}
	}

	.no-select {
		background-color: #e2e2e2;
	}




	/* .tag {
  font-size: 24rpx;
  color: #E8101E;
  background-color: rgba(252, 67, 124, 0.08);
  padding: 6rpx 16rpx;
  border-radius: 6rpx;
} */

	.address-detail {
		display: flex;
		flex-direction: column;
	}

	.detail-title {
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 30rpx;
		color: #333333;
		line-height: 40rpx;
		text-align: left;
		font-style: normal;
		margin-bottom: 10rpx;
	}

	.detail-desc {
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 24rpx;
		color: #999999;
		line-height: 36rpx;
		text-align: left;
		font-style: normal;
		margin-bottom: 41rpx;
	}

	.address-actions {
		display: flex;
		justify-content: space-between;
		align-items: center;

		.default-check {
			display: flex;
			align-items: center;

			.agree-btn {
				margin-right: 14rpx;
				height: 28rpx;
				width: 28rpx;
			}
		}

		.action-btns {
			display: flex;
			align-items: center;

			.action-item {
				display: flex;
				align-items: center;
				margin-left: 48rpx;

				.action-item__revise {
					width: 28rpx;
					height: 28rpx;
					margin-right: 4rpx;
				}

				.action-item__delect {
					width: 28rpx;
					height: 28rpx;
					margin-right: 4rpx;
				}
			}
		}

		.text-default {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 20rpx;
			color: #666666;
			line-height: 28rpx;
			text-align: left;
			font-style: normal;
		}
	}

	.bottom-fixed {
		box-shadow: 0rpx -4rpx 12rpx 0rpx rgba(81, 118, 171, 0.2);
		padding: 0rpx 30rpx 0rpx 30rpx;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		height: 166rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		/* #ifdef APP-PLUS || MP-WEIXIN */
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		/* #endif */
	}

	.fixed-kong {
		height: 166rpx;
	}

	.add-btn {
		width: 690rpx;
		height: 78rpx;
		border-radius: 39rpx;
		border: 1rpx solid #E8101E;
		text-align: center;
		line-height: 64rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}

	.add-text {
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 30rpx;
		color: #E8101E;
		line-height: 42rpx;
		text-align: left;
		font-style: normal;
	}

	/* 空状态样式 */
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding-top: 200rpx;
	}

	.empty-icon {
		width: 240rpx;
		height: 240rpx;
		margin-bottom: 32rpx;
	}

	.empty-text {
		font-size: 30rpx;
		color: #999999;
	}

	/* 平台适配 */
	/* #ifdef MP-WEIXIN */
	.custom-nav {
		padding-top: calc(var(--status-bar-height) + constant(safe-area-inset-top));
		padding-top: calc(var(--status-bar-height) + env(safe-area-inset-top));
	}

	/* #endif */
</style>