<template>
	<view>
		<view class="flex padding-lg align-center">
			<image v-if="globalImages" :src="globalImages + 'imgs/dui.png'" style="width: 30rpx;height: 30rpx;" mode=""></image>
			<view style="margin-left: 20rpx;">
				当前使用车辆： {{ checkeditem.carBrand }}
			</view>
		</view>
		<view class="margin padding boxss" v-for="(item, index) in list" :key="index">
			<view class="flex" style="justify-content: space-between;">
				<view class="flex">
					<view>
						<image :src="item.carPhoto ? item.carPhoto : ''" style="width: 200rpx;height: 140rpx;" mode=""></image>
					</view>
					<view style="margin-left: 20rpx;">
						<view>{{ item.carPlate }}</view>
						<view style="color: #999;">{{ item.carBrand }} - {{ item.carColor }}</view>
						<view style="color: #00c18a;" v-if="item.status === 2">已认证</view>
						<view style="color: #e6a23c;" v-if="item.status === 1">审核中</view>
						<view style="color: #f56c6c;" v-if="item.status === 3">审核失败</view>
					</view>
				</view>
				<view >
					<view style="margin-left: 20rpx; background: #00c18a; padding: 8rpx 15rpx; border-radius: 11rpx; color: #fff;" v-if="item.checked === 0">使用中</view>
					<view style="margin-left: 20rpx; background: #e6a23c; padding: 8rpx 15rpx; border-radius: 11rpx; color: #fff;" v-if="item.checked === 1">未使用</view>
				</view>
			</view>
			<view style="border-top: 1rpx solid #f3f4f6; margin: 20rpx 0;"></view>
			<view class="flex" style="justify-content: space-between;color: #999;">
				<view class="">
					<!-- 编辑图标 -->
					<view style="position: absolute; left: 130rpx;" @tap="handleEdit(item)">
						<image v-if="globalImages" :src="globalImages + 'imgs/edit.png'" style="width: 32rpx;height: 32rpx;" mode="">
						</image>
					</view>
					<!-- 删除图标 -->
					<view style="position: absolute; left: 60rpx;" @click="handleDelete(item.carId)">
						<image v-if="globalImages" :src="globalImages + 'imgs/del.png'" style="width: 32rpx;height: 32rpx;" mode="">
						</image>
					</view>
				</view>
				<view @click="updateChecked(item)">切换使用</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { waitForGlobalImages } from '@/utils/globalImageLoader'
	export default {
		data() {
			return {
				globalImages: '',
				list: [],
				checkeditem: {}
			}
		},
		onLoad() {
			waitForGlobalImages().then((path) => {
				console.log('✅ 全局图片路径:', path)
				this.globalImages = path
			})
			this.selectCarList()
		},
		methods: {
			// list数据
			selectCarList() {
				this.$Request.getT('/app/car/selectCarList').then(res => {
					if (res.code == 0) {
						if (res.data) {
							this.list = res.data
							this.checkeditem = this.list.find(item => item.checked === 1)
						} else {
							this.list = ''
						}
					}
				})
			},
			insertCar() {
				this.$Request.postT("/app/car/insertCar").then(res => {
					
				});
			},
			updateCar() {
				this.$Request.postT("/app/car/updateCar").then(res => {
					
				});
			},
			handleDelete(id) {
				let data = {
					carId: id
				}
				this.$Request.postT("/app/car/deleteCar", data).then(res => {
				    if (res.code == 0) {
				        uni.showModal({
				            title: '提示',
				            content: '删除成功',
				            showCancel: false,
				            success: () => {
				                this.selectCarList()
				            }
				        });
				    } else {
				        uni.showModal({
				            title: '提示',
				            content: res.msg,
				            showCancel: false
				        });
				    }
				}).catch(err => {
				    uni.showModal({
				        title: '提示',
				        content: '网络错误，请重试',
				        showCancel: false
				    });
				});
			},
			updateChecked(item) {
				const newChecked = item.checked === 0 ? 1 : 0;
				const data = {
					...item,
					checked: newChecked
				}
				this.$Request.postT("/app/car/updateChecked", data).then(res => {
				    if (res.code == 0) {
				        uni.showModal({
				            title: '提示',
				            content: '切换成功',
				            showCancel: false,
				            success: () => {
				                this.selectCarList()
								this.checkeditem = this.list.find(item => item.checked === 1);
				            }
				        });
				    } else {
				        uni.showModal({
				            title: '提示',
				            content: res.msg,
				            showCancel: false
				        });
				    }
				}).catch(err => {
				    uni.showModal({
				        title: '提示',
				        content: '网络错误，请重试',
				        showCancel: false
				    });
				});
			}
		},
	}
</script>

<style scoped lang="scss">
	.boxss {
		background: #fff;
		border-radius: 16rpx;
	}
</style>