<template>
	<a-drawer
		:title="'补充车牌审批'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			※ 流程 ：申请候补 → 银行审批<br><br>
			<a-form-item name="positionJson">
				<a-button type="primary" class="childAddButton" @click="addLicensePlates()" disabled>
					<PlusOutlined/>
					补充车牌
				</a-button>
				<a-row :gutter="10" class="form-row">
					<a-col :span="7" class="form-row-con"><br></a-col>
				</a-row>
				<div v-for="(licensePlateInfo, index) in formData.licensePlates" class="form-div">
					<a-row :gutter="24">
						<a-col :span="16">
							<a-input v-model:value="licensePlateInfo.licensePlate" placeholder="请输入车牌" style="margin:1px" disabled/>
						</a-col>
						<a-col :span="8" style="margin-top: 4px">
							<a-button size="small" type="primary" danger ghost @click="delLicensePlates(index)" disabled>移除</a-button>
						</a-col>
					</a-row>
				</div>
			</a-form-item>
		</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="depotRepayOrderForm">
import {message} from 'ant-design-vue'
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required, rules} from '@/utils/formRules'
import depotRepayOrderApi from '@/api/biz/depotRepayOrderApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi";
import depotGrainInfoApi from "@/api/biz/depotGrainInfoApi";
import router from "@/router";
import fileApi from "@/api/dev/fileApi";
import sysConfig from "@/config";

// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const depotShedOptions = ref([])
const depotOptions = ref([])
const imageUrl = ref('')

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		if (formData.value.licensePlatePlug)
			formData.value.licensePlates = formData.value.licensePlatePlug.split(',').map(function (key) {
				return {
					licensePlate: key
				}
			})
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {}
// 验证并提交数据
const onSubmit = () => {
	formData.value.licensePlatePlug = ''
	if (formData.value.licensePlates)
		formData.value.licensePlatePlug = formData.value.licensePlates.map(item => {
			return item.licensePlate
		}).filter(function (s) {
			return s && s.trim()
		}).toString()
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			depotRepayOrderApi
				.depotRepayOrderApprovalPlug({id: formDataParam.id, licensePlatePlug: formDataParam.licensePlatePlug})
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 车牌增行
const addLicensePlates = () => {
	if (!formData.value.licensePlates) {
		formData.value.licensePlates = []
	}
	formData.value.licensePlates.push({
		licensePlatePlug: undefined
	})
}
// 车牌减行
const delLicensePlates = (index) => {
	formData.value.licensePlates.splice(index, 1)
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
