<template>
	<view>
		<view class="c-top">
			<view class="top-icon" @click="getback()">
				<uni-icons type="left" size="18"></uni-icons>
				<view class="">返回</view>
			</view>
			<view class="">编辑</view>
			<view style="width: 10%; height: 100%; "></view>
		</view>
		<view class="ail">
			<view class="dis_">
				<view class="">车辆类型</view>
				<view class="right_" @click="show= true">{{carInfo.rescueCarTypeStr||'请选择车辆类型'}}</view>
			</view>
			<view class="dis_">
				<view class="">车牌号</view>
				<view class="right_"> <input type="text" v-model="carInfo.rescueCarNum" /> </view>
			</view>
			<view class="dis_">
				<view class="">品牌型号</view>
				<view class="right_"> <input type="text" v-model="carInfo.rescueCarBrand" /> </view>
			</view>
			<view class="dis_">
				<view class="">车辆图片</view>
				<view class="rightImg">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic1" name="1"
						:maxCount="1"></u-upload>
				</view>
			</view>
			<view class="dis_">
				<view class="">行驶证</view>
				<view class="rightImg">
					<u-upload :fileList="fileList2" @afterRead="afterRead2" @delete="deletePic2" name="2"
						:maxCount="1"></u-upload>
				</view>
			</view>
			<view class="dis_">
				<view class="">购买时间</view>
				<view class="right_" @click="seletTime('buy')">{{carInfo.carBuyTime||'请选择购买时间'}}
				</view>
			</view>
			<view class="dis_">
				<view class="">保养到期时间</view>
				<view class="right_" @click="seletTime('by')"> {{carInfo.carKeepTime||'请选择保养到期时间'}}
				</view>
			</view>
			<view class="dis_">
				<view class="">保险到期时间</view>
				<view class="right_" @click="seletTime('bx')"> {{carInfo.carInsuranceTime||'请选择保险到期时间'}}
				</view>
			</view>
			<view class="dis_">
				<view class="">年检到期时间</view>
				<view class="right_" @click="seletTime('nj')">{{carInfo.carCheckTime||'请选择年检到期时间'}}
				</view>
			</view>
			<view class="dis_">
				<view class="">车牌颜色</view>
				<view class="right_" @click="carColorShow = true">{{carInfo.carLicenseColorStr||'请选择车牌颜色'}}
				</view>
			</view>
			<view class="dis_">
				<view class="">车架号</view>
				<view class="right_"> <input type="text" v-model="carInfo.frameNumber" />
				</view>
			</view>
		</view>
		<view class="bo_dis">
			<u-button type="error" v-if="carId" @click="delItem()" text="删除"></u-button>
			<u-button type="primary" @click="saveOrUpdate" text="保存"></u-button>

		</view>
		<u-picker :show="show" :columns="jycType" keyName="dictLabel" @confirm="confirm" @cancel="cancel"></u-picker>
		<u-picker :show="carColorShow" :columns="licenseColor" keyName="dictLabel" @confirm="confirmClor"
			@cancel="cancel"></u-picker>
		<u-datetime-picker :show="showtime" mode="date" @confirm="confirmtime"
			@cancel="showtime = false"></u-datetime-picker>
	</view>
</template>

<script>
	import request from '../../utils/request';
	import upload from '@/utils/upload.js'
	export default {
		data() {
			return {
				show: false,
				carColorShow: false,
				showtime: false,
				carId: null,
				timeType: 'buy',
				fileList1: [],
				fileList2: [],
				imageList1: [],
				imageList2: [],
				carInfo: {
					rescueCarType: "", //车辆类型
					rescueCarNum: "", //车牌号
					rescueCarBrand: "", //品牌型号
					carImage: "", //车辆图片
					driveLicenseImage: "", //行驶证
					carBuyTime: "", //购买时间
					carKeepTime: "", //保养到期时间
					carInsuranceTime: "", //保险到期时间
					carCheckTime: "", //年检到期时间
					carLicenseColor: "", //车牌颜色
					carUseNature: "", //使用性质
					frameNumber: "" //车架号				
				},
				carUseNature: [], //使用性质列表
				jycType: [], //救援车类型
				licenseColor: [] //车牌颜色
			}
		},
		onLoad(option) {
			this.carId = option.id
			this.getDictList()
			if (this.carId) {
				this.getCarInfo()
			}
		},
		onShow() {

		},
		methods: {
			delItem() {
				request({
					url: "/system/rescueCar/" + this.carId,
					method: 'delete',
				}).then(res => {

					uni.showToast({
						title: "删除成功"
					})

					setTimeout(function() {
						uni.navigateBack()
					}.bind(this), 1000); // 2000毫秒后执行  
				})
			},
			// 删除图片
			deletePic1(event) {
				this.imageList1.splice(event.index, 1)
				this.fileList1.splice(event.index, 1)
				console.log(this.fileList1, this.fileList2);
			},
			// 删除图片
			deletePic2(event) {
				this.imageList2.splice(event.index, 1)
				this.fileList2.splice(event.index, 1)
				console.log(this.fileList1, this.fileList2);
			},
			// 新增图片
			async afterRead2(event) {
				console.log('执行了', event);
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList2.length
				lists.map((item) => {
					this.fileList2.push({
						...item,

					})
				})
				for (let i = 0; i < lists.length; i++) {
					console.log('路径', lists[i].url);
					const result = await this.uploadFilePromise2(lists[i].url)
					let item = this.fileList2[fileListLen]
					this.fileList2.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},

			// 新增图片
			async afterRead(event) {
				console.log('执行了', event);
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList1.length
				lists.map((item) => {
					this.fileList1.push({
						...item,

					})
				})
				for (let i = 0; i < lists.length; i++) {
					console.log('路径', lists[i].url);


					const result = await this.uploadFilePromise1(lists[i].url)
				}
				console.log('当前list', this.fileList1);
			},
			uploadFilePromise1(url) {

				uni.compressImage({
					src: url,

					success: res => {
						upload({
							url: '/common/upload',
							filePath: res.tempFilePath,
						}).then((res) => {
							this.imageList1.push(res.fileName)
							console.log(res);
						})

					}
				})


			},
			uploadFilePromise2(url) {

				uni.compressImage({
					src: url,

					success: res => {
						upload({
							url: '/common/upload',
							filePath: res.tempFilePath,
						}).then((res) => {
							this.imageList2.push(res.fileName)
							console.log(res);
						})

					}
				})


			},
			seletTime(type) {
				this.timeType = type
				this.showtime = true
			},
			getback() {
				uni.navigateBack()
			},
			confirmClor(e) {
				this.carColorShow = false
				this.carInfo.carLicenseColor = e.value[0].dictValue
				this.carInfo.carLicenseColorStr = e.value[0].dictLabel
			},
			confirm(e) {

				this.show = false
				this.carInfo.rescueCarType = e.value[0].dictValue
				this.carInfo.rescueCarTypeStr = e.value[0].dictLabel
			},
			cancel() {
				this.show = false
			},
			confirmtime(e) {
				this.showtime = false
				const timestamp = e.value
				const date = new Date(timestamp);

				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');


				let timeTemp = `${year}-${month}-${day}`
				console.log(this.rescueTime);
				if (this.timeType == 'buy') {
					this.carInfo.carBuyTime = timeTemp
				}
				if (this.timeType == 'bx') {
					this.carInfo.carInsuranceTime = timeTemp
				}
				if (this.timeType == 'by') {
					this.carInfo.carKeepTime = timeTemp
				}
				if (this.timeType == 'nj') {
					this.carInfo.carCheckTime = timeTemp
				}
			},
			canceltiem(e) {
				console.log(e);
				this.showtime = false
			},

			async saveOrUpdate() {

				if (this.imageList1.length > 0) {
					this.carInfo.carImage = this.imageList1[0]
				}
				if (this.imageList2.length > 0) {
					this.carInfo.driveLicenseImage = this.imageList2[0]
				}

				if (!this.carId) {
					request({
						url: "/system/rescueCar",
						method: 'post',
						data: this.carInfo
					}).then(res => {

						uni.showToast({
							title: "保存成功"
						})

						setTimeout(function() {
							uni.navigateBack()
						}.bind(this), 1000); // 2000毫秒后执行  
					})
				} else {
					console.log(this.carInfo, 'update');
					request({
						url: "/system/rescueCar",
						method: 'put',
						data: this.carInfo
					}).then(res => {

						uni.showToast({
							title: "保存成功"
						})
						setTimeout(function() {
							uni.navigateBack()
						}.bind(this), 1000); // 2000毫秒后执行  

					})
				}

			},
			async getCarInfo() {
				this.fileList1 = []
				this.fileList2 = []
				request({
					url: "/system/rescueCar/" + this.carId,
					method: 'get',
				}).then(res => {
					this.carInfo = res.data
					if (this.carInfo.carImage) {
						this.fileList1.push({
							url: this.$baseUrl + this.carInfo.carImage
						})
						this.imageList1.push(this.carInfo.carImage)
						console.log(this.fileList1);
					}
					if (this.carInfo.driveLicenseImage) {
						this.fileList2.push({
							url: this.$baseUrl + this.carInfo.driveLicenseImage
						})
						this.imageList2.push(this.carInfo.driveLicenseImage)
					}
				})
			},
			async getDictList() {
				request({
					url: "/system/dict/data/type/car_use_nature",
					method: 'get',
				}).then(res => {

					this.carUseNature.push(res.data)
				})

				request({
					url: "/system/dict/data/type/jyc_type",
					method: 'get',
				}).then(res => {
					this.jycType.push(res.data)

				})

				request({
					url: "/system/dict/data/type/car_license_color",
					method: 'get',
				}).then(res => {
					this.licenseColor.push(res.data)

				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.c-top {
		width: 100%;
		height: 88px;
		display: flex;
		align-items: center;
		justify-content: space-between;

	}

	.top-icon {
		display: flex;
		align-items: center;
	}

	.ail {
		box-sizing: border-box;
		padding: 10px;

	}

	.dis_ {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 16px;
		margin: 10px auto;
		border-bottom: 1px solid #e6e6e6;
		box-sizing: border-box;
		padding-bottom: 10px;

	}

	.right_ {
		color: #666;
		width: 55%;
		text-align: right;

		input {
			width: 100%;
		}
	}

	.rightImg {
		color: #666;
		width: 25%;
		text-align: right;
	}

	.bo_dis {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-around;
		box-sizing: border-box;
		padding: 10px;
	}
</style>