<!-- 发起订单 -->
<template>
	<view class="content">
		<view class="dil">
			<view class="top-icon" @click="getback()">
				<uni-icons type="left" size="18"></uni-icons>
			</view>
			<!-- 发起救援 头部-->

			<!-- 起 -->

			<!-- 填空 -->
			<view class="tinput">
				<view class="text1"> <text class="hong1">*</text>加油车辆</view>
				<view class="you" @click="carSelectShow = true">
					<text style="color: blue;">选择车辆</text> <text style="margin-left: 5px;">{{carName}}</text>
				</view>
			</view>
			<view class="tinput">
				<view class="text1"> <text class="hong1">*</text>加了多少油(L)</view>
				<view class="you">
					<input type="number" v-model="refuelNum" placeholder="请输入加了多少油">
				</view>
			</view>
			<view class="tinput">
				<view class="text1"> <text class="hong1">*</text> 加油消费金额(元)</view>
				<view class="you">
					<input type="number" v-model="refuelMonry" placeholder="请输入消费金额">
				</view>
			</view>
			<view class="tinput">
				<view class="text1"> <text class="hong1">*</text>当前公里数(KM)</view>
				<!-- <u-number-box v-model="value" @change="valChange"></u-number-box> -->
				<uni-number-box :min="0" :max="9989999" v-model="refuelDistance"></uni-number-box>

			</view>
			<view class="tinput" @click="show = true">
				<view class="text1"> <text class="hong1">*</text> 选择时间</view>
				<view class="you">
					<text>{{recordTime}}</text>
				</view>
			</view>
			<view class="tinput">
				<view class="text1"> 证明截图</view>
				<view class="">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="1"></u-upload>
				</view>


			</view>

			<view class="anniu" @click="getadd()">
				<text>提交</text>
			</view>
			<u-datetime-picker :show="show" v-model="value1" @cancel="cancelclose" @confirm="confirmopen"
				mode="datetime"></u-datetime-picker>
			<u-picker :show="carSelectShow" :columns="carList" keyName="rescueCarNum" @confirm="confirmCar"
				@cancel="cancelCar"></u-picker>
		</view>


	</view>
</template>

<script>
	import request from '../../utils/request'
	import config from '@/config'
	import upload from '@/utils/upload.js'
	export default {
		data() {
			return {
				fileList1: [],
				showImage: "",
				id: 1,
				carSelectShow: false,
				carList: [],
				value: 0,
				show: false,
				value1: Number(new Date()),
				recordTime: '',
				refuelNum: '',
				refuelMonry: '',
				refuelDistance: '',
				carId: null,
				carName: null,
			}
		},
		onLoad() {
			this.timef()
			this.getCarList()
		},

		methods: {
			confirmCar(e) {
				console.log(e);
				this.carSelectShow = false
				this.carId = e.value[0].id
				this.carName = e.value[0].rescueCarNum
			},
			cancelCar() {
				this.carSelectShow = false
			},
			getCarList() {
				request({
					url: '/system/rescueCar/list',
					method: 'get',
					params: {
						pageNum: 1,
						pageSize: 100
					}
				}).then((res) => {
					this.carList.push(res.rows)
				})
			},
			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,
					})
				})
				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) {
				upload({
					url: '/common/upload',
					filePath: url,
				}).then((res) => {
					console.log('images', res.fileName);
					this.showImage = res.fileName
				})

			},
			valChange(e) {
				console.log('当前值为: ' + e.value)
			},
			getadd() {
				if (!this.refuelNum || !this.refuelMonry || !this.recordTime || !this.refuelDistance || !this.carId) {
					uni.showToast({
						title: "必填不能有空",
						icon: 'error'
					})
					return
				}

				let data = {
					driverId: uni.getStorageSync('driverInfo'),
					refuelNum: this.refuelNum,
					refuelMoney: this.refuelMonry,
					recordTime: this.recordTime,
					refuelDistance: this.refuelDistance,
					showImage: this.showImage,
					carId: this.carId
				}
				request({
					url: '/app/driver/addRefuelRecord',
					method: 'post',
					data: data
				}).then((res) => {
					if (res.code == 200) {
						console.log(res);
						uni.showToast({
							title: '提交成功',

						})
						setTimeout(
							uni.navigateBack(), 1000);
					}
				})
			},
			timef() {

				let timestamp = Number(new Date()); // 输入你的时间戳
				let date = new Date(timestamp);

				let year = date.getFullYear();
				let month = ("0" + (date.getMonth() + 1)).slice(-2);
				let day = ("0" + date.getDate()).slice(-2);
				let hour = ("0" + date.getHours()).slice(-2);
				let minute = ("0" + date.getMinutes()).slice(-2);

				let formattedDate = year + "-" + month + "-" + day + " " + hour + ":" + minute;
				this.recordTime = formattedDate
			},
			cancelclose(e) {
				console.log(e);
				this.show = false
			},
			confirmopen(e) {
				console.log(e);
				let timestamp = e.value; // 输入你的时间戳
				let date = new Date(timestamp);

				let year = date.getFullYear();
				let month = ("0" + (date.getMonth() + 1)).slice(-2);
				let day = ("0" + date.getDate()).slice(-2);
				let hour = ("0" + date.getHours()).slice(-2);
				let minute = ("0" + date.getMinutes()).slice(-2);

				let formattedDate = year + "-" + month + "-" + day + " " + hour + ":" + minute;
				this.recordTime = formattedDate
				console.log(formattedDate); // 输出格式化后的日期字符串
				this.show = false
			},
			getback() {
				uni.navigateBack({
					delta: 1,
				})
			},
			getzhi(index) {
				this.carzhi = index
			},
			getzhi2(index) {
				this.maneizhi = index
			},

		}

	}
</script>

<style scoped lang="scss">
	.content {
		width: 100%;
		height: calc(100vh);
		background-color: #F6F6F6;
		box-sizing: border-box;
		padding-top: 45px;
	}

	.top-icon {
		margin-bottom: 15px;
	}

	.dil {
		box-sizing: border-box;
		background-color: #F6F6F6;
		padding: 0px 12px;
	}

	.top {
		box-sizing: border-box;
		padding: 0px 15px;
		width: 100%;
		background-color: white;
	}

	.top-box {
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		padding: 20px 0px;
	}

	.tb-left {
		height: 100%;
		width: 80%;
		display: flex;
		align-items: center;
	}

	.uicon {
		width: 18px;
		height: 18px;
		border-radius: 4px;
		color: white;
		background: orangered;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 12px;
		font-weight: bold;
		margin-right: 6px;
	}

	.tb-right {
		width: 20px;
		height: 26px;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.text1 {
		font-size: 16px;
		font-weight: bold;
		color: #363636;
	}

	.hong1 {
		margin-top: 5px;
		font-size: 12px;
		font-weight: 400;
		color: #FF5453;
	}

	.hong2 {
		margin-top: 5px;
		font-size: 12px;
		font-weight: 400;

	}

	.tinput {
		width: 100%;
		display: flex;
		background: white;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 16px;
		margin-top: 14px;
	}

	.xinput {
		width: 100%;
		background: white;
		box-sizing: border-box;
		padding: 16px;
		margin-top: 14px;
	}

	.you {
		text-align: right;
	}

	.xz {
		margin-top: 10px;
		display: flex;
		align-items: center;
	}

	.kuang {
		width: 38px;
		height: 23px;
		background: #ECECEC;
		border-radius: 5px;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #666666;
		font-size: 14px;
		margin-right: 15px;
	}

	.xlan {
		background: #CDE7FF !important;
		color: #1D62FF !important;
		border: 1px solid #2A96FE;
	}

	.anniu {
		width: 100%;
		background: linear-gradient(105deg, #FFE3AC 0%, #F3BA60 98%);
		height: 40px;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #542F0E;
		border-radius: 4px;
		margin-top: 20px;
	}
</style>