<template>
	<view class="contentStyle">
	<!-- 	<view class="status-bar"></view>
		<uni-nav-bar title="车位详情" left-icon="left" @clickLeft="goBack" :border="false" backgroundColor="transparent">
		</uni-nav-bar> -->

		<scroll-view class="page" scroll-y="true">
			<view class="status-header">
				<view class="status-icon" :class="statusConfig.iconClass">
					<text class="icon-text">{{ statusConfig.iconText }}</text>
				</view>
				<text class="status-title">{{ statusConfig.title }}</text>
				<view class="step-bar">
					<view class="step-item" v-for="(step, index) in stepList" :key="index"
						:class="{ active: index < currentStep, done: index < currentStep - 1 }">
						<view class="step-circle">{{ index + 1 }}</view>
						<text class="step-text">{{ step }}</text>
						<view class="step-line" v-if="index < stepList.length - 1"
							:class="{ active: index < currentStep - 1 }"></view>
					</view>
				</view>
			</view>

			<view class="info-card">
				<view class="card-title">
					<view class="title-line"></view>
					车位信息
				</view>
				<view class="info-row">
					<text class="info-label">小区</text>
					<text class="info-value">{{ form.villageName }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">停车场</text>
					<text class="info-value">{{ form.areaName }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">车位编号</text>
					<text class="info-value">{{ form.parkingCode }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">车位状态</text>
					<text class="info-value"
						:style="{color: form.parkingStatus === '0' ? '#00aa00' : form.parkingStatus === '1' ? '#FF8F40' : '#999' }">{{ form.parkingStatus === '0' ? '闲置' : form.parkingStatus === '1' ? '自用' : '租赁' }}</text>
				</view>
			</view>

			<view class="info-card" v-if="form.carId">
				<view class="card-title">
					<view class="title-line"></view>
					车辆信息
					<text class="unbind" @click="clickUnbind">取消绑定</text>
				</view>
				<view class="info-row">
					<text class="info-label">车牌号</text>
					<text class="info-value">{{ form.carNum }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">车辆品牌</text>
					<text class="info-value">{{ form.carBrand }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">车辆型号</text>
					<text class="info-value">{{ form.carModel }}</text>
				</view>
				<view class="info-row">
					<text class="info-label">车辆颜色</text>
					<text class="info-value">{{ form.carColor }}</text>
				</view>
			</view>
			<view class="empty-card" v-if="!form.carId">
				<view class="card-title">
					<view class="title-line"></view>
					车辆信息
				</view>
				<view  class="empty-state">
					<uni-icons type="info" size="30" color="#ccc"></uni-icons>
					<text class="empty-text">暂无绑定车辆</text>
				</view>
			</view>
		</scroll-view>
		<view class="bottom-btn-wrap">
			<button class="btn-modify" @click="onModify">修改</button>
			<button class="btn-delete" :class="{ 'delete-only': currentStatus !== 'fail' }"
				@click="onDelete">删除</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed,
		onMounted
	} from 'vue'
	import {
		onReachBottom,
		onPullDownRefresh,
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	import {
		getUserParkingSpaceList,
		rebindCar,
		deleteParking,
		getParkingDetail
	} from '@/pageSubPack/api/apiSub.js'

	const statusBarHeight = ref(20)
	const id = ref(undefined)
	const currentStatus = ref('')
	const checkStatus = ref('')
	// parkingId
	const form = ref({
		community: "",
		parkingSpace: "",
		parkingSpaceNum: "",
		status: '',
		carNum: "",
		carBrand: "",
		carModel: "",
		carColor: "",
		carId:''
	})
	const stepList = ["车位信息", "物业审核", "认证成功"]
	const statusMap = {
		success: {
			title: "车位认证成功",
			iconClass: "icon-success",
			iconText: "✓",
			currentStep: 3
		},
		pending: {
			title: "车位审核中，请耐心等待",
			iconClass: "icon-pending",
			iconText: "⏳",
			currentStep: 2
		},
		fail: {
			title: "车位认证失败，请重新提交",
			iconClass: "icon-fail",
			iconText: "✕",
			currentStep: 2
		},
	}

	const statusConfig = computed(() => statusMap[currentStatus.value] || statusMap.pending)
	const currentStep = computed(() => statusConfig.value.currentStep)
	onLoad((option) => {
		console.log(option);
		id.value = option.id
		checkStatus.value = option.statusClass || ''
		currentStatus.value = option.statusClass == '0' ? 'pending' : option.statusClass == '1' ?
			'success' : option.statusClass == '2' ? 'fail' : ''
		loadParkingSpaceDetail()

	})
	onMounted(() => {

	})
	const loadParkingSpaceDetail = async () => {
		// 防重复请求

		try {
			const res = await getParkingDetail({
				id: id.value
			})

			const data = res.data
			form.value = data
			console.log(form.value);
		} catch (err) {
			uni.showToast({
				title: err.msg,
				icon: 'none'
			})
			// console.error('列表接口报错：', err)
		} finally {

		}
	}


	const clickUnbind = () => {
		uni.showModal({
			title: "确认取消绑定",
			content: "确认取消绑定该车辆吗?",
			success: (res) => {
				if (res.confirm) {
					uni.showLoading({
						title: '解绑中...',
						mask: true
					})
					rebindCar({
						parkingId: id.value,
						residentId:uni.getStorageSync('userId'),
						carId:'',
						
					}).then(unbindRes => {
						uni.hideLoading()
						if (unbindRes.code === 200) {
							uni.showToast({
								title: '解绑成功',
								icon: 'success'
							})
							setTimeout(() => {
									uni.navigateBack({ delta: 1 })
							}, 1500)
						} else {
							uni.showToast({
								title: unbindRes.msg || '解绑失败',
								icon: 'none'
							})
						}
					})
				}
			}
		})
	}

	const onModify = () => {
		uni.setStorageSync('operationType', 'update')
		uni.navigateTo({
			url: '/pageSubPack/my/addParkingSpace?id=' + id.value + '&checkStatus=' + checkStatus.value + '&sourcePage=addParkingSpace'
		})
	}

	const goBack = () => {
		uni.navigateBack()
	}

	const onDelete = () => {
		uni.showModal({
			title: "确认删除",
			content: "确认删除此车位信息吗?",
			success: (res) => {
				if (res.confirm) {
					uni.showLoading({
						title: '删除中...',
						mask: true
					})
					deleteParking({
						parkingId: id.value
					}).then(deleteRes => {
						uni.hideLoading()
						if (deleteRes.code === 200) {
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							})
							setTimeout(() => {
									uni.navigateBack({ delta: 1 })
							}, 1500)
						} else {
							uni.showToast({
								title: deleteRes.msg || '删除失败',
								icon: 'none'
							})
						}
					})
				}
			}
		})
	}

</script>

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

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

	.contentStyle {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f9f9f9;
	}

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

	.status-header {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 40rpx 30rpx 30rpx;
	}

	.status-icon {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 20rpx;
	}

	.icon-success {
		background-color: #2F77FD;
	}

	.icon-pending {
		background-color: #2F77FD;
	}

	.icon-fail {
		background-color: #E34D59;
	}

	.icon-text {
		font-size: 40rpx;
		font-weight: bold;
		color: #fff;
	}

	.status-title {
		font-size: 30rpx;
		font-weight: 500;
		color: #333;
		margin-bottom: 40rpx;
	}

	.step-bar {
		display: flex;
		align-items: center;
		width: 100%;
		justify-content: space-between;
		padding: 0 20rpx;
	}

	.step-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		position: relative;
		flex: 1;
	}

	.step-circle {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		background-color: #1677ff;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		z-index: 2;
	}

	.step-item.active .step-circle {
		background-color: #1677ff;
	}

	.step-item:not(.active) .step-circle {
		background-color: #e5e5e5;
		color: #999;
	}

	.step-text {
		font-size: 24rpx;
		color: #666;
		margin-top: 10rpx;
		text-align: center;
	}

	.step-line {
		position: absolute;
		top: 20rpx;
		left: 50%;
		width: 100%;
		height: 4rpx;
		background-color: #e5e5e5;
		z-index: 1;
	}

	.step-line.active {
		background-color: #1677ff;
	}

	.info-card {
		padding: 20rpx;
		border: 1rpx solid #f0f0f0;
		background-color: #fff;
		border-radius: 8rpx;
		margin-bottom: 20rpx;
	}

	.empty-card {
		padding: 20rpx;
		border: 1rpx solid #f0f0f0;
		background-color: #fff;
		border-radius: 8rpx;
		margin-bottom: 20rpx;
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 70rpx 0;
		color: #ccc;
	}

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

	.card-title {
		display: flex;
		align-items: center;
		font-size: 30rpx;
		font-weight: 500;
		color: #333;
		margin-bottom: 30rpx;
	}

	.title-line {
		width: 8rpx;
		height: 28rpx;
		background-color: #1677ff;
		margin-right: 10rpx;
		border-radius: 4rpx;
	}

	.info-row {
		display: flex;
		margin-bottom: 24rpx;
		align-items: center;
	}

	.info-label {
		width: 180rpx;
		font-size: 28rpx;
		color: #666;
	}

	.info-value {
		font-size: 28rpx;
		color: #333;
		flex: 1;
		align-items: center;
	}

	.unbind {
		color: #1677ff;
		font-size: 28rpx;
		margin-bottom: 20rpx;
		display: inline-block;
		margin-left: auto;

	}

	.bottom-btn-wrap {
		padding: 20rpx 30rpx 40rpx;
		display: flex;
	}

	.btn-modify {
		flex: 1;
		height: 88rpx;
		line-height: 88rpx;
		background-color: #1677ff;
		color: #fff;
		font-size: 30rpx;
		border-radius: 8rpx;
		border: none;
		margin-right: 20rpx;
	}

	.btn-delete {
		flex: 1;
		height: 88rpx;
		line-height: 88rpx;
		background-color: #fff;
		color: #f53f3f;
		border: 1rpx solid #f53f3f;
		font-size: 30rpx;
		border-radius: 8rpx;
	}

	.btn-delete.delete-only {
		flex: 1;
	}
</style>