<template>
	<view class="contentStyle" style="">
		<view class="status-bar"></view>
		<uni-nav-bar :title="'选择'+checkType" left-icon="left" @clickLeft="goBackAdd" backgroundColor="transparent"
			:border="false">
		</uni-nav-bar>

		<scroll-view class="page" scroll-y>
			<!-- 楼栋网格列表 -->

			<view v-if="dataList.length === 0" class="empty-state">
				<uni-icons type="info" size="60" color="#ccc"></uni-icons>
				<text class="empty-text">
					{{ '暂无数据'}}
				</text>
			</view>
			<view class="building-grid" :class="{ disabled: isNavigating }">
				<view class="building-item"
					:class="{ active:(checkType=='楼栋'&& selectedBuilding === item.id)||(checkType=='单元'&& selectedUnit === item.id)||(checkType=='楼层'&&selectedFloor === item.id) ||(checkType=='户号'&&selectedHouseNumber === item.id) }"
					v-for="(item, index) in dataList" :key="index" @click="selectBuilding(item)">
					<text class="building-text">{{item.name }}</text>
				</view>
			</view>
		</scroll-view>

	</view>



</template>

<script setup>
	import {
		ref,
		onMounted,
	} from 'vue'
	import {
		onUnload,
		onShow

	} from '@dcloudio/uni-app'
	import {
		getHouseBuildings,
		getHouseUnits,
		getHouseFloors,
		getHouseHouses
	} from '/pageSubPack/api/apiSub.js'

	// 状态栏高度
	const statusBarHeight = ref(20)

	// 从缓存获取类型
	const checkType = ref(uni.getStorageSync('checkType') || '')
	const dataList = ref([])
	// 添加防重复点击锁
	const isNavigating = ref(false)




	// 选中项（从缓存读取）
	const selectedBuilding = ref(uni.getStorageSync('buildingId') || '')
	const selectedUnit = ref(uni.getStorageSync('unitId') || '')
	const selectedFloor = ref(uni.getStorageSync('floorId') || '')
	const selectedHouseNumber = ref(uni.getStorageSync('houseNumberId') || '')

	// 页面挂载后赋值列表
	onMounted(() => {
		console.log(checkType);
		if (checkType.value === '楼栋') {
			getList('楼栋')
		} else if (checkType.value === '单元') {
			getList('单元')
		} else if (checkType.value === '楼层') {
			getList('楼层')
		} else if (checkType.value === '户号') {
			getList('户号')
		}

	})

	// 选择项方法
	const selectBuilding = (item) => {
		// 防重复点击
		if (isNavigating.value) return
		isNavigating.value = true

		if (checkType.value === '楼栋') {
			selectedBuilding.value = item.id
			uni.setStorageSync('buildingId', item.id)
			uni.setStorageSync('buildingName', item.name)
			uni.removeStorageSync('unitId')
			uni.removeStorageSync('unitName')
			uni.removeStorageSync('floorId')
			uni.removeStorageSync('floorName')
			uni.removeStorageSync('houseNumberId')
			uni.removeStorageSync('houseNumberName')
			uni.setStorageSync('checkType', '单元')
		} else if (checkType.value === '单元') {
			selectedUnit.value = item.id
			uni.setStorageSync('unitId', item.id)
			uni.setStorageSync('unitName', item.name)
			uni.removeStorageSync('floorId')
			uni.removeStorageSync('floorName')
			uni.removeStorageSync('houseNumberId')
			uni.removeStorageSync('houseNumberName')
			uni.setStorageSync('checkType', '楼层')
		} else if (checkType.value === '楼层') {
			selectedFloor.value = item.id
			uni.setStorageSync('floorId', item.id)
			uni.setStorageSync('floorName', item.name)
			uni.removeStorageSync('houseNumberId')
			uni.removeStorageSync('houseNumberName')
			uni.setStorageSync('checkType', '户号')
		} else if (checkType.value === '户号') {
			selectedHouseNumber.value = item.id
			uni.setStorageSync('houseNumberId', item.id)
			uni.setStorageSync('houseNumberName', item.name)
		}

		uni.showToast({
			title: '选择成功',
			icon: 'success'
		})

		setTimeout(() => {
			goFreash()
		}, 1000)
	}
	// 刷新跳转
	const goFreash = () => {
		console.log(checkType.value, '刷新页面')
		if (checkType.value !== '户号') {
			// 楼栋/单元/楼层：替换当前页，链式选择不堆叠
			uni.redirectTo({
				url: '/pageSubPack/my/selectBuilding'
			})
		} else {
			// 户号选择完成：返回已有的 addHouse 页面（addHouse 通过 onShow 重新读取 storage）
			uni.navigateBack()
		}
	}

	// 返回新增
	const goBackAdd = () => {
		if (checkType.value === "楼栋") {
			uni.removeStorageSync("buildingId")
			uni.removeStorageSync("buildingName")
		} else if (checkType.value === "单元") {
			uni.removeStorageSync("unitId")
			uni.removeStorageSync("unitName")
		} else if (checkType.value === "楼层") {
			uni.removeStorageSync("floorId")
			uni.removeStorageSync("floorName")
		} else if (checkType.value === "户号") {
			uni.removeStorageSync("houseNumberId")
			uni.removeStorageSync("houseNumberName")
		}
		uni.navigateBack()
	}
	// 获取数据
	const getList = async (type) => {
		try {
			if (type == '楼栋') {
				const res = await getHouseBuildings({
					villageId: uni.getStorageSync('communityId')
				})
				const newList = res.data.map(item => ({
					id: item.id,
					name: item.buildingName
				}))

				dataList.value = newList
			} else if (type == '单元') {
				const res = await getHouseUnits({
					buildingId: selectedBuilding.value
				})
				const newList = res.data.map(item => ({
					id: item.id,
					name: item.name
				}))

				dataList.value = newList
			} else if (type == '楼层') {
				const res = await getHouseFloors({
					buildingId: selectedBuilding.value,
					unitNo: selectedUnit.value
				})
				const newList = res.data.map(item => ({
					id: item,
					name: item
				}))

				dataList.value = newList
			} else if (type == '户号') {
				const res = await getHouseHouses({
					buildingId: selectedBuilding.value,
					unitNo: selectedUnit.value,
					floorNo: selectedFloor.value
				})
				const newList = res.data.map(item => ({
					id: item.houseId,
					name: item.houseNo
				}))

				dataList.value = newList
			}

		} catch (err) {
			uni.showToast({
				title: '加载失败',
				icon: 'none'
			})
			console.error('列表接口报错：', err)
		} finally {

		}
	}
	// 生命周期
	onShow(() => {

	})
	onUnload(() => {})
</script>
<style lang="scss">
	page {
		background: linear-gradient(to left bottom, #e2efff 0%, #f9f9f9 50%);
		overflow: hidden;
		height: 100vh;
	}
</style>

<style scoped>
	.contentStyle {
		display: flex;
		flex-direction: column;
		height: 100vh;
		/* 关键：占满屏幕高度 */
		/* background-color: #fff; */
	}

	.page {
		flex: 1;
		overflow-y: auto;
		padding: 0px 40rpx;
		box-sizing: border-box;
	}

	/* 底部按钮 */
	.bottom-btn {
		padding: 20rpx 30rpx 40rpx;
	}


	/* 网格布局：3列 */
	.building-grid {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		gap: 30rpx;
	}

	/* 正在跳转时禁用点击 */
	.building-grid.disabled {
		pointer-events: none;
	}

	/* 空状态 */
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;
		color: #ccc;
	}

	.empty-text {
		font-size: 32rpx;
		color: #999;
		margin-top: 20rpx;
		margin-bottom: 10rpx;
	}

	/* 楼栋按钮 */
	.building-item {
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #f9f9f9;
		/* border: 1rpx solid #e5e5e5; */
		border-radius: 20rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
		transition: all 0.2s ease;
		overflow: hidden;
		min-width: 0;
	}

	/* 选中状态 */
	.building-item.active {
		background-color: #2F77FD;
		/* border-color: #1677ff; */
	}

	.building-item.active .building-text {
		color: #fff;
	}

	/* 楼栋文字 */
	.building-text {
		font-size: 32rpx;
		color: #333;
		font-weight: 500;
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
	}

	/* 成功提示弹窗 */
	.toast-mask {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: transparent;
		z-index: 9999;
	}

	.toast-content {
		background-color: rgba(0, 0, 0, 0.8);
		padding: 40rpx 60rpx;
		border-radius: 8rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.toast-icon {
		width: 60rpx;
		height: 60rpx;
		line-height: 60rpx;
		border-radius: 50%;
		background-color: #fff;
		color: #1677ff;
		font-size: 40rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.toast-text {
		color: #fff;
		font-size: 28rpx;
	}

	.status-bar {
		width: 100vw;
		height: 46px;
	}
</style>