<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="driverInfo.name" 
					placeholder="请输入司机姓名" 
					class="input"
				/>
			</view>
			
			<!-- 联系电话 -->
			<view class="form-item">
				<view class="label">联系电话</view>
				<input 
					v-model="driverInfo.phone" 
					placeholder="请输入司机联系电话" 
					class="input"
					type="number"
					maxlength="11"
				/>
			</view>
			
			<!-- 驾驶车型 -->
			<view class="form-item">
				<view class="label">驾驶车型</view>
				<view class="select-value" @click="selectCarType">
					<text v-if="!driverInfo.carType" class="placeholder">请选择驾驶车型</text>
					<text v-else>{{ driverInfo.carType }}</text>
					<uv-icon name="arrow-right" size="16" color="#999"></uv-icon>
				</view>
			</view>
			
			<!-- 工作经验 -->
			<view class="form-item">
				<view class="label">工作经验</view>
				<view class="select-value" @click="selectExperience">
					<text v-if="!driverInfo.experience" class="placeholder">请选择工作经验</text>
					<text v-else>{{ driverInfo.experience }}</text>
					<uv-icon name="arrow-right" size="16" color="#999"></uv-icon>
				</view>
			</view>
			
			<!-- 所在地区 -->
			<view class="form-item">
				<view class="label">所在地区</view>
				<view class="address-input" @click="selectLocation">
					<text v-if="!driverInfo.location" class="placeholder">请选择所在地区</text>
					<text v-else>{{ driverInfo.location }}</text>
					<uv-icon name="map-pin" size="20" color="#007AFF"></uv-icon>
				</view>
			</view>
			
			<!-- 身份证号 -->
			<view class="form-item">
				<view class="label">身份证号</view>
				<input 
					v-model="driverInfo.idCard" 
					placeholder="请输入身份证号" 
					class="input"
					maxlength="18"
				/>
			</view>
			
			<!-- 驾驶证照片 -->
			<view class="form-item">
				<view class="label">驾驶证照片</view>
				<view class="upload-container">
					<view v-for="(image, index) in driverInfo.licenseImages" :key="index" class="image-item">
						<image :src="image" mode="aspectFill" class="uploaded-image" @click="previewImage(index, 'license')"></image>
						<view class="delete-btn" @click="deleteLicenseImage(index)">
							<uv-icon name="close" size="16" color="#fff"></uv-icon>
						</view>
					</view>
					<view v-if="driverInfo.licenseImages.length < 2" class="upload-btn" @click="uploadLicenseImage">
						<uv-icon name="plus" size="32" color="#999"></uv-icon>
						<text>上传驾驶证</text>
					</view>
				</view>
				<view class="upload-tip">请上传驾驶证正反面，最多2张</view>
			</view>
			
			<!-- 身份证照片 -->
			<view class="form-item">
				<view class="label">身份证照片</view>
				<view class="upload-container">
					<view v-for="(image, index) in driverInfo.idCardImages" :key="index" class="image-item">
						<image :src="image" mode="aspectFill" class="uploaded-image" @click="previewImage(index, 'idCard')"></image>
						<view class="delete-btn" @click="deleteIdCardImage(index)">
							<uv-icon name="close" size="16" color="#fff"></uv-icon>
						</view>
					</view>
					<view v-if="driverInfo.idCardImages.length < 2" class="upload-btn" @click="uploadIdCardImage">
						<uv-icon name="plus" size="32" color="#999"></uv-icon>
						<text>上传身份证</text>
					</view>
				</view>
				<view class="upload-tip">请上传身份证正反面，最多2张</view>
			</view>
			
			<!-- 推荐理由 -->
			<view class="form-item">
				<view class="label">推荐理由</view>
				<textarea 
					v-model="driverInfo.reason" 
					placeholder="请简述推荐理由，如：技术熟练、工作认真等" 
					class="textarea"
					maxlength="200"
				></textarea>
			</view>
		</view>
		
		<!-- 底部按钮 -->
		<view class="bottom-buttons">
			<button class="btn-save" @click="submitRecommendation">提交推荐</button>
		</view>
	</view>
</template>

<script>
	import navbar from '@/components/base/navbar.vue'
	
	export default {
		name: 'StaffDriver',
		components: {
			navbar
		},
		data() {
			return {
				driverInfo: {
					name: '',
					phone: '',
					carType: '',
					experience: '',
					location: '',
					idCard: '',
					licenseImages: [],
					idCardImages: [],
					reason: ''
				}
			}
		},
		onLoad() {
			uni.setNavigationBarTitle({
				title: '推荐司机'
			});
		},
		methods: {
			selectCarType() {
				uni.showActionSheet({
					itemList: ['泵车', '搅拌车', '车载泵', '其他'],
					success: (res) => {
						const types = ['泵车', '搅拌车', '车载泵', '其他'];
						this.driverInfo.carType = types[res.tapIndex];
					}
				});
			},
			selectExperience() {
				uni.showActionSheet({
					itemList: ['1年以下', '1-3年', '3-5年', '5-10年', '10年以上'],
					success: (res) => {
						const experiences = ['1年以下', '1-3年', '3-5年', '5-10年', '10年以上'];
						this.driverInfo.experience = experiences[res.tapIndex];
					}
				});
			},
			selectLocation() {
				uni.chooseLocation({
					success: (res) => {
						this.driverInfo.location = res.address;
					}
				});
			},
			uploadLicenseImage() {
				uni.chooseImage({
					count: 2 - this.driverInfo.licenseImages.length,
					sizeType: ['compressed'],
					sourceType: ['camera', 'album'],
					success: (res) => {
						this.driverInfo.licenseImages.push(...res.tempFilePaths);
					}
				});
			},
			uploadIdCardImage() {
				uni.chooseImage({
					count: 2 - this.driverInfo.idCardImages.length,
					sizeType: ['compressed'],
					sourceType: ['camera', 'album'],
					success: (res) => {
						this.driverInfo.idCardImages.push(...res.tempFilePaths);
					}
				});
			},
			previewImage(index, type) {
				const images = type === 'license' ? this.driverInfo.licenseImages : this.driverInfo.idCardImages;
				uni.previewImage({
					current: index,
					urls: images
				});
			},
			deleteLicenseImage(index) {
				this.driverInfo.licenseImages.splice(index, 1);
			},
			deleteIdCardImage(index) {
				this.driverInfo.idCardImages.splice(index, 1);
			},
			submitRecommendation() {
				// 表单验证
				if (!this.driverInfo.name) {
					uni.showToast({ title: '请输入司机姓名', icon: 'none' });
					return;
				}
				if (!this.driverInfo.phone) {
					uni.showToast({ title: '请输入联系电话', icon: 'none' });
					return;
				}
				if (!/^1[3-9]\d{9}$/.test(this.driverInfo.phone)) {
					uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
					return;
				}
				if (!this.driverInfo.carType) {
					uni.showToast({ title: '请选择驾驶车型', icon: 'none' });
					return;
				}
				if (!this.driverInfo.experience) {
					uni.showToast({ title: '请选择工作经验', icon: 'none' });
					return;
				}
				if (!this.driverInfo.idCard) {
					uni.showToast({ title: '请输入身份证号', icon: 'none' });
					return;
				}
				if (!/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(this.driverInfo.idCard)) {
					uni.showToast({ title: '请输入正确的身份证号', icon: 'none' });
					return;
				}
				
				uni.showToast({
					title: '推荐成功',
					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;
	}
	
	.textarea {
		width: 100%;
		height: 120rpx;
		padding: 20rpx;
		border: 1rpx solid #e0e0e0;
		border-radius: 8rpx;
		font-size: 28rpx;
		box-sizing: border-box;
		resize: none;
	}
	
	.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;
	}
	
	.address-input {
		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>