<template>
	<view class="container">
		<u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" labelWidth="120">

			<u-form-item label="车牌号码" prop="form.vehicleNo" borderBottom ref="item1" required>

				<car-number-input class="car-number-input" @numberInputResult="numberInputResult"
					:defaultStr="form.vehicleNo"></car-number-input>

				<u--input v-model="form.vehicleNo" border="none" maxlength="8" placeholder="请输入车牌号码"></u--input>
			</u-form-item>
			<!-- <u-form-item label="车辆类型" prop="form.vehicleType" borderBottom ref="item1">
				<picker @change="bindPickerChange" :value="index" :range="array">
					<view class="uni-input">{{index ? array[index] : '请选择车辆类型'}}</view>
				</picker>
			</u-form-item> -->
			<!-- <u-form-item label="驾驶证" prop="form.car_jsz" borderBottom ref="item1" required>
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="1"></u-upload>
			</u-form-item>
			<u-form-item label="牵引车车牌号" prop="form.phone" borderBottom ref="item1">
				<u--input v-model="form.phone" border="none" maxlength="11" placeholder="请输入牵引车车牌号"></u--input>
			</u-form-item>
			<u-form-item label="牵引车道运证" prop="form.phone" borderBottom ref="item1">
				<u-upload :fileList="fileList2" @afterRead="afterRead2" @delete="deletePic2" name="1" multiple
					:maxCount="1"></u-upload> </u-form-item>
			<u-form-item label="牵引车行驶证" prop="form.phone" borderBottom ref="item1">
				<u-upload :fileList="fileList3" @afterRead="afterRead3" @delete="deletePic3" name="1" multiple
					:maxCount="1"></u-upload> </u-form-item> -->

			<!-- <u-form-item label="车辆品牌/型号" prop="form.phone" borderBottom ref="item1" >
				<u--input v-model="form.phone" border="none" maxlength="11" placeholder=""></u--input>
			</u-form-item> -->

			<!-- 	<u-form-item label="车辆颜色" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item> -->
			<!-- <u-form-item label="车辆照片" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item> -->
			<!-- 	<u-form-item label="载重能力（吨）" prop="form.phone" borderBottom ref="item1" >
				<u--input v-model="form.phone" border="none" maxlength="11" placeholder=""></u--input>
			</u-form-item> -->


			<!-- <u-form-item label="容积（立方米）" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item> -->
			<!-- <u-form-item label="车辆长度（米）" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item> -->
			<!-- 	<u-form-item label="燃油类型（柴油、汽油、电动等）" prop="form.phone" borderBottom ref="item1" >
				<u--input v-model="form.phone" border="none" maxlength="11" placeholder=""></u--input>
			</u-form-item> -->


			<!-- <u-form-item label="车辆识别代号（VIN）" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item>
			<u-form-item label="发动机号" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item> -->

			<!-- <u-form-item label="道路运输证号" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item> -->
			<!-- 	<u-form-item label="车辆状态（空闲、运输中、维修中）" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item> -->
			<!-- <u-form-item label="绑定司机" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item> -->
			<!-- <u-form-item label="购买日期/注册日期" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item>
			<u-form-item label="年检到期日期" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item>
			<u-form-item label="保险公司" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item> -->


			<!-- <u-form-item label="保险单号" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item>
			<u-form-item label="保险到期日期" prop="form.name" borderBottom ref="item1" >
				<u--input v-model="form.name" border="none" maxlength="5" placeholder=""></u--input>
			</u-form-item> -->


		</u--form>

		<u-button type="primary" class="btn" text="提交信息" shape="circle" @click="add"></u-button>

	</view>
</template>

<script>
	import {
		get,
		post
	} from '@/common/request.js'
	export default {
		data() {
			return {
				form: {
					id: "",
					vehicleNo: "",
					vehicleType: "",
					car_jsz: "",
					qy_carNo: "",
					qy_dyz: "",
					qy_xsz: ""
				},
				rules: {},
				array: ['货车', '卡车', '厢式货车'],
				index: null,
				fileList1: [],
				fileList2: [],
				fileList3: [],
			}
		},
		onLoad(option) {
			this.form.vehicleNo = option.vehicleNo
			this.form.id = option.id
		},

		methods: {
			async add() {

				if (this.form.vehicleNo == '' || this.form.vehicleNo.trim().length != 7 && this.form.vehicleNo.trim()
					.length != 8) {
					uni.showToast({
						title: '请填写正确的车牌号',
						icon: 'none'
					})
					return false
				}

				if (this.form.id) {
					await post('/myapi/api/yq_driver/updateBindVehicle', {
						...this.form
					}).then(res => {
						if (res.code == 1) {
							uni.showToast({
								title: '修改成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/car/car'
								})
							}, 500)
						}
					})

				}else{
					
					
					await post('/myapi/api/yq_driver/bindVehicle', {
						...this.form
					}).then(res => {
						if (res.code == 1) {
							uni.showToast({
								title: '绑定成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/car/car'
								})
							}, 500)
						}
					})
					
				}

			},
			numberInputResult(e) {
				this.form.vehicleNo = e
			},
			bindPickerChange: function(e) {
				this.index = e.detail.value
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "http://192.168.2.21:7001/upload", // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data);
							}, 1000);
						},
					});
				});
			},
		},
	}
</script>

<style scoped lang="less">
	.container {
		padding: 20px;

		.car-number-input {
			width: 100%;
		}

		.tag_title {
			width: 80px;
			height: 30px;
			display: flex;
			align-items: center;
		}

		.btn {
			margin-top: 20px;
		}
	}
</style>