<template>
	<a-drawer :title="title" :width="850" :open="visible" :destroy-on-close="true" @close="onClose">
		<a-card :bordered="false" class="mb-2">
			<s-table
				ref="tableRef"
				:columns="columns"
				:data="loadData"
				bordered
				:row-key="(record) => record.id"
			>
				<template #operator class="table-operator">
					<a-space>
						<a-button type="primary" @click="formRef.onOpen(null, supplierId)" v-if="hasPerm('bizSupplierTransportAdd')">
							<template #icon><plus-outlined /></template>
							新增
						</a-button>
					</a-space>
				</template>
				<template #bodyCell="{ column, record, index }">
					<template v-if="column.dataIndex === 'serial'">
						{{ index + 1 }}
					</template>
					<template v-if="column.dataIndex === 'transportType'">
						{{ $TOOL.dictTypeData('transport_type', record.transportType) }}
					</template>
					<template v-if="column.dataIndex === 'action'">
						<a-space>
							<a @click="formRef.onOpen(record, supplierId)" v-if="hasPerm('bizSupplierTransportEdit')">编辑</a>
							<a-divider type="vertical" v-if="hasPerm(['bizSupplierTransportEdit', 'bizSupplierTransportDelete'], 'and')" />
							<a-button type="link" danger size="small" v-if="hasPerm('bizSupplierTransportDelete')" @click="deleteConfig(record)">删除</a-button>
						</a-space>
					</template>
				</template>
			</s-table>
		</a-card>
	</a-drawer>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="bizsuppliertransport">
	import { cloneDeep } from 'lodash-es'
	import Form from './transportForm.vue'
	import bizSupplierTransportApi from '@/api/biz/bizSupplierTransportApi'
	import {ExclamationCircleOutlined} from '@ant-design/icons-vue';
	import {Modal} from 'ant-design-vue';
	import {createVNode} from 'vue';
	
	const submitLoading = ref(false)
	const toolConfig = { refresh: true, height: false, columnSetting: false, striped: false }
	// 默认是关闭状态
	const visible = ref(false)
	const searchFormState = ref({})
	const tableRef = ref()
	const formRef = ref()
	const recordData = ref()
	const title = ref()
	const supplierId = ref()
	
	const columns = [
		{
			title: '序号',
			width: 50,
			dataIndex: 'serial',
			align:'center'
		},
		{
			title: '运输类型',
			dataIndex: 'transportType',
			align: 'center',
		},
		{
			title: '运输号',
			dataIndex: 'transportNo',
			align: 'center',
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			align: 'center'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizSupplierTransportEdit', 'bizSupplierTransportDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}
	// 打开抽屉
	const onOpen = (record) => {
		recordData.value = record
		title.value = "【" + record.supplierName + "】-运输车辆/船舶管理"
		searchFormState.value = {
			supplierId: record.id
		}
		supplierId.value = record.id
		visible.value = true
	}
	const loadData = (parameter) => {
		return bizSupplierTransportApi.bizSupplierTransportPage(Object.assign(parameter, searchFormState.value)).then((data) => {
			return data
		})
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}
	// 删除
	const deleteConfig = (record) => {
		Modal.confirm({
			title: '确定删除该数据吗？',
			icon: createVNode(ExclamationCircleOutlined),
			content: '',
			onOk() {
				submitLoading.value = true
				let params = [
					{
						id: record.id
					}
				]

				bizLoadTimeApi
					.bizLoadTimeDelete(params)
					.then(() => {
						tableRef.value.refresh(true)
					})
					.finally(() => {
						submitLoading.value = false
					})
			},
			onCancel() {}
		})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
