<template>
	<view class="contentStyle" style="">

		<view class="page">
			<view v-if="carList.length === 0" class="empty-state">
				<uni-icons type="info" size="30" color="#ccc"></uni-icons>
				<text class="empty-text">暂无数据</text>
			</view>
			<view class="car-item" v-for="(item, index) in carList" :key="index" @click="handleItemClick(item, index)">
				<image :src="item.carImageUrl" mode="scaleToFill" class="car-avatar" v-if="item.carImageUrl"/>
				<image :src="defaultCarImg" mode="aspectFill" class="car-avatar-default" v-if="!item.carImageUrl"/>	
				<view class="car-info">
					<view>
						<view class="car-plate">{{ item.carNum }}</view>
						<view class="car-model">{{ item.carBrand }}
							<text v-if="item.carBrand && item.carColor">·</text>
							{{ item.carColor }}
						</view>
					</view>
					<view class="car-bindParkingSpace">{{ item.villageName }}</view>
				</view>
				<!-- certificationStatus -->
				<!-- <view class="status-tag"
					:class="{ underReview: item.certificationStatus === '审核中', unbound: item.certificationStatus === '未绑定', bounded: item.certificationStatus === ''}">
					{{ item.certificationStatus }}
				</view> -->
			</view>
		</view>
		<view class="bottom-btn">
			<button class="confirm-btn" @click="addCar">添加车辆</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		getMyCarList
	} from '@/pageSubPack/api/apiSub.js'
	import {
		onShow,
		onPullDownRefresh,
		onReachBottom
	} from '@dcloudio/uni-app'
	const statusBarHeight = ref(20)
	const defaultCarImg = "https://wuye.ckdzkj.com/images/propertyImgs/defaultCarImg.png"
	const carList = ref([])
	const loadingMore = ref(false)
	const noMoreData = ref(false)

	const pageNum = ref(1)
	const pageSize = ref(10)

	// ==================== 下拉刷新 ====================
	onPullDownRefresh(() => {
		pageNum.value = 1
		noMoreData.value = false
		carList.value = []
		loadingMore.value = false
		getList()
	})

	// 页面加载时请求第一页
		onMounted(() => {
			getList()
		})
		// 从子页面返回时刷新列表（如新增/编辑/删除车辆后）
		onShow(() => {
			pageNum.value = 1
			noMoreData.value = false
			loadingMore.value = false
			carList.value = []
			getList()
		})
	const getList = async () => {
		// 防重复请求
		if (loadingMore.value || noMoreData.value) return
		loadingMore.value = true
		try {
			const res = await getMyCarList({
				pageNum: pageNum.value,
				pageSize: pageSize.value,
				userId: uni.getStorageSync('userId')
			})

			const data = res
			const newList = data.rows || []

			// 第一页 → 覆盖数据
			if (pageNum.value === 1) {
				carList.value = newList
			} else {
				// 后续页 → 追加数据
				carList.value = [...carList.value, ...newList]
			}
			// 判断是否没有更多数据
			if (newList.length < pageSize.value) {
				noMoreData.value = true
			} else {
				pageNum.value++
			}
		} catch (err) {
			uni.showToast({
				title: '加载失败',
				icon: 'none'
			})
			console.error('列表接口报错：', err)
		} finally {
			loadingMore.value = false
			uni.stopPullDownRefresh()
		}
	}


	// ==================== 上拉加载更多 ====================
	onReachBottom(() => {
		getList()
	})

	const getStatusText = (status) => {
		if (status === 0) return '未绑定'
		if (status === 1) return '已绑定'
		if (status === 2) return '审核中'
		return '未绑定'
	}

	const handleItemClick = (item, index) => {
		uni.setStorageSync('operationType', 'update')
		uni.navigateTo({
			url: '/pageSubPack/my/carDetail?id=' + item.id + '&status=' + item.status
		})
	}

	const addCar = () => {
		uni.setStorageSync('operationType', 'add')
		uni.navigateTo({
			url: '/pageSubPack/my/addCar'
		})
	}

	const goBack = () => {
		uni.navigateBack({ delta: 1 })
	}
</script>

<style lang="scss">
	page {
		background: #F3F8FD;
	}
</style>

<style scoped>
	.contentStyle {
		min-height: 100vh;
		background-color: #f5f7fa;
	}

	.page {
		padding: 0px 40rpx 140rpx;
		box-sizing: border-box;
	}

	.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;
	}

	.bottom-btn {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		padding: 20rpx 30rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		flex-shrink: 0;
		background-color: #f5f7fa;
		box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
	}

	.car-item {
		display: flex;
		align-items: center;
		padding: 20rpx;
		margin-bottom: 20rpx;
		border: 1rpx solid #e6e9ff;
		border-radius: 16rpx;
		background-color: #fff;
		position: relative;
		width: calc(100% - 44rpx);
	}

	.car-avatar {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		margin-right: 24rpx;
		flex-shrink: 0;
	}
	.car-avatar-default{
		width: 80rpx;
		height: 80rpx;
		margin-right: 36rpx;
		flex-shrink: 0;
	}

	.car-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.car-plate {
		font-size: 32rpx;
		font-weight: bold;
		color: #000;
		line-height: 1.4;
		margin-bottom: 8rpx;
	}

	.car-model {
		font-size: 28rpx;
		color: #666;
		line-height: 1.4;
	}

	.car-bindParkingSpace {
		font-size: 28rpx;
		color: #666;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		width: 90%;
		margin-top: 10rpx;
	}

	.status-tag {
		font-size: 24rpx;
		padding: 6rpx 14rpx;
		border-radius: 6rpx;
		font-weight: 500;
		flex-shrink: 0;
		position: absolute;
		right: 30rpx;
		top: 30rpx;
	}

	.status-tag.unbound {
		color: #999;
	}

	.status-tag.bounded {
		color: #2F77FD;
	}

	.status-tag.underReview {
		color: #FF8F40;
	}

	.status-tag.unpass {
		color: red;
	}

	.confirm-btn {
		width: 100%;
		height: 88rpx;
		background-color: #1677ff;
		color: #fff;
		border-radius: 12rpx;
		font-size: 34rpx;
		border: none;
		line-height: 88rpx;
	}

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