<template>
	<a-drawer
		:title="'关单详情'"
		: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-table
				ref="table"
				size="small"
				:columns="columns"
				:data-source="formData.depotWorkList"
				:expand-row-by-click="true"
				bordered
			>
				<template #bodyCell="{ column, record }">
					<template v-if="column.dataIndex === 'action' ">
						<a @click="detailFormLeftRef.onOpen(record)">详情 </a>
					</template>
				</template>
			</a-table>
			<template v-if="formData.truckBookList && formData.truckBookList.length > 0">
				<br>已预约未结束的车次信息：
				<a-table
					ref="table"
					size="small"
					:columns="columnsOffOrder"
					:data-source="formData.truckBookList"
					:expand-row-by-click="true"
					bordered
				>
				</a-table>
				<p style="color: red">※ 强制关单操作处理</p>
				<p> 1. 已预约未审批的车次：预约自动结束，车辆禁止通行</p>
				<p> 2. 已审批待入场车次：预约自动结束，车辆禁止通行</p>
				<p> 3. 已入场未出场的车次：发送车辆出场超时报警，需要驻场员手动驳回离场并引导卸货和出场</p>
				<p> 4. 人工补单将自动驳回</p>
			</template>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" danger v-if="hasPerm('出库还款手动关单')" @click="offOrder()">
				<span v-if="formData.truckBookList && formData.truckBookList.length > 0">
					强制 </span> 关单
			</a-button>
			<a-button @click="onClose">取消</a-button>
		</template>
	</a-drawer>
	<DetailFormLeft ref="detailFormLeftRef"/>
</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 depotWorkApi from '@/api/biz/depotWorkApi'
import sysConfig from "@/config";
import DetailFormLeft from '../depotTruckWeighDouble/detailFormLeft.vue'
import depotTruckWeighDoubleApi from '@/api/biz/depotTruckWeighDoubleApi'
import depotRepayOrderApi from '@/api/biz/depotRepayOrderApi'

// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const detailFormLeftRef = ref()
const submitLoading = ref(false)

const columns = [
	{
		title: '车牌',
		dataIndex: 'licensePlate'
	},
	{
		title: '数量(吨)',
		dataIndex: 'depotTonCount'
	},
	{
		title: '总价(元)',
		dataIndex: 'orderAmount'
	},
	{
		title: '单据分类',
		dataIndex: 'billGroup'
	},
	{
		title: '时间',
		dataIndex: 'createTime'
	},
	{
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '50px'
	}
]
const columnsOffOrder = [
	{
		title: '预约号',
		dataIndex: 'truckBookId'
	},
	{
		title: '车牌',
		dataIndex: 'licensePlate'
	},
	{
		title: '司机姓名',
		dataIndex: 'driverName'
	},
	{
		title: '司机手机号',
		dataIndex: 'driverMobile'
	},
	{
		title: '审批状态',
		dataIndex: 'depotOpApprovalState'
	},
	{
		title: '进程',
		dataIndex: 'bizFlowName'
	},
	{
		title: '通行状态',
		dataIndex: 'bizFlowState'
	}
]

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record)
		depotRepayOrderApi.depotRepayOrderDetail({id: record.id}).then((data) => {
			formData.value = data
		})
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 关单
const offOrder = () => {
	depotRepayOrderApi
		.depotRepayOrderEditOffOrder({"id": formData.value.id})
		.then(() => {
			onClose()
			emit('successful')
		})
		.finally(() => {
			submitLoading.value = false
		})
}
// 默认要校验的
const formRules = {}
// 抛出函数
defineExpose({
	onOpen
})
</script>
