<template>
	<a-drawer
		:title="formData.id ? '编辑车辆预约表' : '增加车辆预约表'"
		:width="800"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="24" v-if="formData.id">
				<a-col :span="12">
					<a-form-item label="企业客户：" name="depotGrainName">
						<a-input v-model:value="formData.depotGrainName" disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="出入库：" name="depotBizType">
						<a-radio-group v-model:value="formData.depotBizType" placeholder="请选择出入库" :options="depotBizTypeOptions" disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="粮食种类：" name="grainGroup">
						<a-select v-model:value="formData.grainGroup" placeholder="请选择粮食种类" :options="grainGroupOptions" disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="粮食类型：" name="grainType">
						<a-select v-model:value="formData.grainType" placeholder="请选择粮食类型" :options="grainTypeOptions" disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="车牌：" name="licensePlate">
						<a-input v-model:value="formData.licensePlate" placeholder="请输入车牌" disabled allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="9">
					<a-form-item label="后车牌：" name="licensePlateBack">
						<a-input v-model:value="formData.licensePlateBack" placeholder="请输入后车牌" disabled allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="3">
					<a-form-item label=" " name="licensePlateBackText">
						<a-select v-model:value="formData.licensePlateBackText" :options="[{label:'挂',value:'挂'},{label:'超',value:'超'}]"
								  @change="licensePlateBackTextCheck" disabled allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="司机姓名：" name="driverName">
						<a-input v-model:value="formData.driverName" placeholder="请输入司机姓名" disabled allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="司机行驶证：" name="driverTravel">
						<a-upload
							v-model:file-list="formData.picUrls"
							class="avatar-uploader"
							list-type="picture-card"
							:show-upload-list="false"
							:custom-request="customRequest"
							accept="image/png, image/jpeg, image/jpg"
							disabled
						>
							<img v-if="imageUrl" :src="imageUrl" alt="avatar" style="max-height: 100px; max-width: 100px"/>
							<div v-else>
								<plus-outlined/>
								<div class="ant-upload-text">上传</div>
							</div>
						</a-upload>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="司机驾驶证：" name="driverLicense">
						<a-upload
							v-model:file-list="formData.picUrls2"
							class="avatar-uploader"
							list-type="picture-card"
							:show-upload-list="false"
							:custom-request="customRequestTwo"
							accept="image/png, image/jpeg, image/jpg"
							disabled
						>
							<img v-if="imageUrlTwo" :src="imageUrlTwo" alt="avatar" style="max-height: 100px; max-width: 100px"/>
							<div v-else>
								<plus-outlined/>
								<div class="ant-upload-text">上传</div>
							</div>
						</a-upload>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="司机手机号：" name="driverMobile">
						<a-input v-model:value="formData.driverMobile" placeholder="请输入司机手机号" disabled allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="经办人身份证号：" name="keeperCitizenId">
						<a-input v-model:value="formData.keeperCitizenId" placeholder="请输入经办人身份证号" disabled allow-clear/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="24" v-else>
				<a-col :span="12">
					<a-form-item label="企业客户：" name="depotId">
						<a-select v-model:value="formData.depotId" placeholder="请选择企业客户" :options="depotIdOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="出入库：" name="depotBizType">
						<a-radio-group v-model:value="formData.depotBizType" placeholder="请选择出入库" :options="depotBizTypeOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="粮食种类：" name="grainGroup">
						<a-select v-model:value="formData.grainGroup" placeholder="请选择粮食种类" :options="grainGroupOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="粮食类型：" name="grainType">
						<a-select v-model:value="formData.grainType" placeholder="请选择粮食类型" :options="grainTypeOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="车牌：" name="licensePlate">
						<a-input v-model:value="formData.licensePlate" placeholder="请输入车牌" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="9">
					<a-form-item label="后车牌：" name="licensePlateBack">
						<a-input v-model:value="formData.licensePlateBack" placeholder="请输入后车牌" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="3">
					<a-form-item label=" " name="licensePlateBackText">
						<a-select v-model:value="formData.licensePlateBackText" :options="[{label:'挂',value:'挂'},{label:'超',value:'超'}]"
								  @change="licensePlateBackTextCheck" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="司机姓名：" name="driverName">
						<a-input v-model:value="formData.driverName" placeholder="请输入司机姓名" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="司机行驶证：" name="driverTravel">
						<a-upload
							v-model:file-list="formData.picUrls"
							class="avatar-uploader"
							list-type="picture-card"
							:show-upload-list="false"
							:custom-request="customRequest"
							accept="image/png, image/jpeg, image/jpg"
						>
							<img v-if="imageUrl" :src="imageUrl" alt="avatar" style="max-height: 100px; max-width: 100px"/>
							<div v-else>
								<plus-outlined/>
								<div class="ant-upload-text">上传</div>
							</div>
						</a-upload>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="司机驾驶证：" name="driverLicense">
						<a-upload
							v-model:file-list="formData.picUrls2"
							class="avatar-uploader"
							list-type="picture-card"
							:show-upload-list="false"
							:custom-request="customRequestTwo"
							accept="image/png, image/jpeg, image/jpg"
						>
							<img v-if="imageUrlTwo" :src="imageUrlTwo" alt="avatar" style="max-height: 100px; max-width: 100px"/>
							<div v-else>
								<plus-outlined/>
								<div class="ant-upload-text">上传</div>
							</div>
						</a-upload>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="司机手机号：" name="driverMobile">
						<a-input v-model:value="formData.driverMobile" placeholder="请输入司机手机号" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="经办人身份证号：" name="keeperCitizenId">
						<a-input v-model:value="formData.keeperCitizenId" placeholder="请输入经办人身份证号" allow-clear/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="24">
				<a-col :span="12">
					<a-form-item label="企业单位：" name="companyName">
						<a-input v-model:value="formData.companyName" placeholder="请输入企业单位" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="企业单位联系人：" name="companySalesmanName">
						<a-input v-model:value="formData.companySalesmanName" placeholder="请输入企业单位联系人" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="企业单位联系人电话：" name="companySalesmanMobile">
						<a-input v-model:value="formData.companySalesmanMobile" placeholder="请输入企业单位联系人电话" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
				</a-col>
				<a-col :span="12">
					<a-form-item label="收款人：" name="payeeName" v-if="formData.depotBizType === '入库'">
						<a-input v-model:value="formData.payeeName" placeholder="请输入收款人" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="收款人手机号：" name="payeeMobile" v-if="formData.depotBizType === '入库'">
						<a-input v-model:value="formData.payeeMobile" placeholder="请输入收款人手机号" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="收款人开户行：" name="payeeBank" v-if="formData.depotBizType === '入库'">
						<a-input v-model:value="formData.payeeBank" placeholder="请输入收款人开户行" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="收款人开户行账户：" name="payeeBankBillNo" v-if="formData.depotBizType === '入库'">
						<a-input v-model:value="formData.payeeBankBillNo" placeholder="请输入收款人开户行账户" allow-clear/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="depotTruckBookForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required, rules} from '@/utils/formRules'
import depotTruckBookApi from '@/api/biz/depotTruckBookApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi"
import sysConfig from "@/config";
import fileApi from "@/api/dev/fileApi";
import {message} from "ant-design-vue";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const depotBizTypeOptions = ref([])
const grainGroupOptions = ref([])
const grainTypeOptions = ref([])
const depotIdOptions = ref([])
const imageUrl = ref('')
const imageUrlTwo = ref('')

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		if (formData.value.driverTravel) {
			// 让其回显
			imageUrl.value = sysConfig.API_URL + formData.value.driverTravel
			formData.value.picUrls = [imageUrl.value]
		}
		if (formData.value.driverLicense) {
			// 让其回显
			imageUrlTwo.value = sysConfig.API_URL + formData.value.driverLicense
			formData.value.picUrls2 = [imageUrlTwo.value]
		}
	} else {
		dictOptionsApi.options(Object.assign('', {'dictValue': '粮库'})).then((data) => {
			depotIdOptions.value = data
		})
		formData.value.depotBizType = '入库'
		formData.value.grainGroup = '玉米'
		formData.value.grainType = '湿粮'
	}
	depotBizTypeOptions.value = tool.dictList('出入库')
	grainGroupOptions.value = tool.dictList('粮食种类')
	grainTypeOptions.value = tool.dictList('粮食类型')
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 后车牌
const licensePlateBackTextCheck = () => {
	if (formData.value.licensePlateBack && formData.value.licensePlateBack.length > 6)
		formData.value.licensePlateBack = formData.value.licensePlateBack.substring(0, 6)
	if (formData.value.licensePlateBackText)
		formData.value.licensePlateBack = formData.value.licensePlateBack + formData.value.licensePlateBackText
}
// 默认要校验的
const formRules = {
	depotBizType: [required('请选择')],
	licensePlate: [required('请输入车牌号'), rules.licensePlate],
	driverName: [required('请输入司机姓名')],
	driverMobile: [required('请输入司机手机号'), rules.telOrPhoneMore],
	grainGroup: [required('请输入')],
	grainType: [required('请输入')],
	depotId: [required('请选择')],
	keeperCitizenId: [required('请选择'), rules.idCard],
	payeeMobile: [rules.telOrPhoneMore],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.picUrls = ref([])
			formDataParam.picUrls2 = ref([])
			depotTruckBookApi
				.depotTruckBookSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
const customRequest = (data) => {
	formData.value.picUrls = ref([])
	getBase64(data.file)
		.then((res) => {
			imageUrl.value = res
			formData.value.picUrls.push(res)

			//保存图片
			const fileData = new FormData()
			fileData.append('file', data.file)
			fileApi
				.fileUploadReturnId(fileData)
				.then((result) => {
					formData.value.driverTravel = result.toString()
				})
		})
		.catch((err) => {
		})
}
const customRequestTwo = (data) => {
	formData.value.picUrls2 = ref([])
	getBase64(data.file)
		.then((res) => {
			imageUrlTwo.value = res
			formData.value.picUrls2.push(res)

			//保存图片
			const fileData = new FormData()
			fileData.append('file', data.file)
			fileApi
				.fileUploadReturnId(fileData)
				.then((result) => {
					formData.value.driverLicense = result.toString()
				})
		})
		.catch((err) => {
		})
}

// 文件转base64，用于显示图片
const getBase64 = (file) => {
	return new Promise((resolve, reject) => {
		// FileReader类就是专门用来读文件的
		const reader = new FileReader()
		reader.readAsDataURL(file)
		// 成功和失败返回对应的信息，reader.result一个base64，可以直接使用
		reader.onload = () => resolve(reader.result)
		// 失败返回失败的信息
		reader.onerror = (error) => reject(error)
	})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
