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

		<scroll-view class="page" scroll-y="true">
			<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.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 class="photo-row">
					<text class="info-label" style="width: 200rpx;">车辆图片</text>
					<view class="photo-list">
						<image class="photo-item" :src="form.carImageUrl"></image>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="bottom-btn-wrap">
			<button class="btn-modify" @click="clickUpdate">修改</button>
			<button class="btn-delete" @click="onDelete">删除</button>
		</view>
	</view>
</template>

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

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

	const statusBarHeight = ref(20)
	const currentStatus = ref('')
	const isDeleting = ref(false)
	const isUpdating = ref(false)
	const id = ref(undefined)
	const form = ref({
		plateNo: '',
		carBrandName: '',
		model: '',
		color: '',
		carImg: ''
	})
	let isFirstLoad = true
	onLoad((options) => {
		console.log(options);
		id.value = options.id
		currentStatus.value = options.status
		loadCarDetail(options.id)

	})

	onShow(() => {
		// 首次加载由 onLoad 处理，仅从编辑页返回时刷新
		if (isFirstLoad) {
			isFirstLoad = false
			return
		}
		isUpdating.value = false
		if (id.value) {
			loadCarDetail()
		}
	})

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

		try {
			const res = await getCarDetail({
				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 clickUpdate = () => {
		if (isUpdating.value) return
		isUpdating.value = true
		uni.setStorageSync('operationType', 'update')
		uni.navigateTo({
			url: '/pageSubPack/my/addCar?id=' + id.value
		})
	}

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

</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: #f5f7fa;
	}

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

	.info-card {
		background-color: #fff;
		padding: 20rpx;
	}

	.card-title {
		display: flex;
		align-items: center;
		font-size: 32rpx;
		font-weight: 600;
		color: #000;
		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: #000;
	}

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

	.photo-row {
		margin-top: 30rpx;
		display: flex;
		flex-direction: column;
	}

	.photo-list {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
		margin-top: 20rpx;
	}

	.photo-item {
		width: 100%;
		background-color: #f0f4ff;
		border-radius: 8rpx;
	}

	.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;
		margin-right: 0rpx;
	}
</style>