<template>
	<view class="content">
		<navbar title="车辆信息" leftClick @leftClick="$utils.navigateBack" />
		<view class="form-container">
			<!-- 车牌号 -->
			<view class="form-item">
				<view class="label">车牌号</view>
				<input 
					v-model="carInfo.plateNumber" 
					placeholder="请输入车牌号" 
					class="input"
					maxlength="8"
				/>
			</view>
			
			<!-- 车辆类型 -->
			<view class="form-item" @click="selectCarType">
				<view class="label">车辆类型</view>
				<view class="select-value">
					<text v-if="!carInfo.carType" class="placeholder">请选择车辆类型</text>
					<text v-else>{{ carInfo.carType }}</text>
					<uv-icon name="arrow-right" size="16" color="#999"></uv-icon>
				</view>
			</view>
			
			<!-- 轴数 -->
			<view class="form-item" @click="selectAxleCount">
				<view class="label">轴数</view>
				<view class="select-value">
					<text v-if="!carInfo.axleCount" class="placeholder">请选择轴数</text>
					<text v-else>{{ carInfo.axleCount }}</text>
					<uv-icon name="arrow-right" size="16" color="#999"></uv-icon>
				</view>
			</view>
			
			<!-- 载重量 -->
			<view class="form-item">
				<view class="label">载重量(吨)</view>
				<input 
					v-model="carInfo.loadCapacity" 
					placeholder="请输入载重量" 
					class="input"
					type="number"
				/>
			</view>
			
			<!-- 车辆照片 -->
			<view class="form-item">
				<view class="label">车辆照片</view>
				<view class="upload-container">
					<view v-for="(image, index) in carInfo.images" :key="index" class="image-item">
						<image :src="image" mode="aspectFill" class="uploaded-image" @click="previewImage(index)"></image>
						<view class="delete-btn" @click="deleteImage(index)">
							<uv-icon name="close" size="16" color="#fff"></uv-icon>
						</view>
					</view>
					<view v-if="carInfo.images.length < 3" class="upload-btn" @click="uploadImage">
						<uv-icon name="plus" size="32" color="#999"></uv-icon>
						<text>上传照片</text>
					</view>
				</view>
				<view class="upload-tip">最多上传3张照片</view>
			</view>
			
			<!-- 证件照片 -->
			<view class="form-item">
				<view class="label">相关证件</view>
				<view class="upload-container">
					<view v-for="(doc, index) in carInfo.documents" :key="index" class="image-item">
						<image :src="doc" mode="aspectFill" class="uploaded-image" @click="previewDocument(index)"></image>
						<view class="delete-btn" @click="deleteDocument(index)">
							<uv-icon name="close" size="16" color="#fff"></uv-icon>
						</view>
					</view>
					<view v-if="carInfo.documents.length < 5" class="upload-btn" @click="uploadDocument">
						<uv-icon name="plus" size="32" color="#999"></uv-icon>
						<text>上传证件</text>
					</view>
				</view>
				<view class="upload-tip">请上传行驶证、营运证等相关证件，最多5张</view>
			</view>
		</view>
		
		<!-- 底部按钮 -->
		<view class="bottom-buttons">
			<button class="btn-save" @click="saveCar">{{ isEdit ? '保存' : '添加' }}</button>
		</view>
	</view>
</template>

<script>
	import navbar from '@/components/base/navbar.vue'
	
	export default {
		name: 'StaffCar',
		components: {
			navbar
		},
		data() {
			return {
				isEdit: false,
				carId: '',
				carInfo: {
					plateNumber: '',
					carType: '',
					axleCount: '',
					loadCapacity: '',
					images: [],
					documents: []
				}
			}
		},
		onLoad(options) {
			if (options.mode === 'edit' && options.id) {
				this.isEdit = true;
				this.carId = options.id;
				this.loadCarInfo();
			}
			uni.setNavigationBarTitle({
				title: this.isEdit ? '编辑车辆' : '添加车辆'
			});
		},
		methods: {
			loadCarInfo() {
				// 模拟加载车辆信息
				this.carInfo = {
					plateNumber: '湘A12345',
					carType: '泵车',
					axleCount: '4轴',
					loadCapacity: '25',
					images: ['/static/re/ide1.png'],
					documents: ['/static/re/ide2.png']
				};
			},
			selectCarType() {
				uni.showActionSheet({
					itemList: ['泵车', '搅拌车', '车载泵'],
					success: (res) => {
						const types = ['泵车', '搅拌车', '车载泵'];
						this.carInfo.carType = types[res.tapIndex];
					}
				});
			},
			selectAxleCount() {
				uni.showActionSheet({
					itemList: ['2轴', '3轴', '4轴', '5轴', '6轴'],
					success: (res) => {
						const axles = ['2轴', '3轴', '4轴', '5轴', '6轴'];
						this.carInfo.axleCount = axles[res.tapIndex];
					}
				});
			},
			uploadImage() {
				uni.chooseImage({
					count: 3 - this.carInfo.images.length,
					sizeType: ['compressed'],
					sourceType: ['camera', 'album'],
					success: (res) => {
						this.carInfo.images.push(...res.tempFilePaths);
					}
				});
			},
			uploadDocument() {
				uni.chooseImage({
					count: 5 - this.carInfo.documents.length,
					sizeType: ['compressed'],
					sourceType: ['camera', 'album'],
					success: (res) => {
						this.carInfo.documents.push(...res.tempFilePaths);
					}
				});
			},
			previewImage(index) {
				uni.previewImage({
					current: index,
					urls: this.carInfo.images
				});
			},
			previewDocument(index) {
				uni.previewImage({
					current: index,
					urls: this.carInfo.documents
				});
			},
			deleteImage(index) {
				this.carInfo.images.splice(index, 1);
			},
			deleteDocument(index) {
				this.carInfo.documents.splice(index, 1);
			},
			saveCar() {
				if (!this.carInfo.plateNumber) {
					uni.showToast({
						title: '请输入车牌号',
						icon: 'none'
					});
					return;
				}
				if (!this.carInfo.carType) {
					uni.showToast({
						title: '请选择车辆类型',
						icon: 'none'
					});
					return;
				}
				if (!this.carInfo.axleCount) {
					uni.showToast({
						title: '请选择轴数',
						icon: 'none'
					});
					return;
				}
				
				uni.showToast({
					title: this.isEdit ? '保存成功' : '添加成功',
					icon: 'success'
				});
				
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		padding: 20rpx;
		min-height: 100vh;
		background-color: #f5f5f5;
		padding-bottom: 120rpx;
	}
	
	.form-container {
		background-color: #fff;
		border-radius: 10rpx;
		padding: 30rpx;
	}
	
	.form-item {
		margin-bottom: 40rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
	}
	
	.label {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 20rpx;
		font-weight: 500;
	}
	
	.input {
		width: 100%;
		height: 80rpx;
		padding: 0 20rpx;
		border: 1rpx solid #e0e0e0;
		border-radius: 8rpx;
		font-size: 28rpx;
		box-sizing: border-box;
	}
	
	.select-value {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 80rpx;
		padding: 0 20rpx;
		border: 1rpx solid #e0e0e0;
		border-radius: 8rpx;
		font-size: 28rpx;
		
		.placeholder {
			color: #999;
		}
	}
	
	.upload-container {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
	}
	
	.image-item {
		position: relative;
		width: 200rpx;
		height: 200rpx;
	}
	
	.uploaded-image {
		width: 100%;
		height: 100%;
		border-radius: 8rpx;
	}
	
	.delete-btn {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
		width: 40rpx;
		height: 40rpx;
		background-color: #ff3b30;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.upload-btn {
		width: 200rpx;
		height: 200rpx;
		border: 2rpx dashed #ccc;
		border-radius: 8rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		gap: 10rpx;
		color: #999;
		font-size: 24rpx;
	}
	
	.upload-tip {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}
	
	.bottom-buttons {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx;
		background-color: #fff;
		border-top: 1rpx solid #f0f0f0;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
	}
	
	.btn-save {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #007AFF;
		color: #fff;
		border-radius: 40rpx;
		font-size: 32rpx;
		border: none;
	}
</style>