<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-button type="primary" v-if="hasPerm('bizLoadTimeAdd')" @click="addFormRef.onOpen(null, pointId)">
						<template #icon><plus-outlined /></template> 新增
					</a-button>
				</template>`
				<template #bodyCell="{ column, record, index }">
					<template v-if="column.dataIndex === 'serial'">
						{{ index + 1 }}
					</template>
					<template v-if="column.dataIndex === 'beginEndTime'">
						{{ record.beginTime + '~' + record.endTime }}
					</template>
					<template v-if="column.dataIndex === 'action'">
						<a-space>
							<a @click="editFormRef.onOpen(record, pointId)" v-if="hasPerm('bizLoadTimeEdit') && Number(record.alreadyNumber) == 0">编辑</a>

							<a-divider type="vertical" v-if="hasPerm(['bizLoadTimeEdit', 'bizLoadTimeDelete'], 'and') && Number(record.alreadyNumber) == 0" />
							<a-button type="link" danger size="small" v-if="hasPerm('bizLoadTimeDelete') && Number(record.alreadyNumber) == 0" @click="deleteConfig(record)">删除</a-button>
						
							<a @click="alreadyFormRef.onOpen(record, pointId)" v-if="hasPerm('bizLoadTimeAlready') && Number(record.alreadyNumber) != 0">次数</a>
						</a-space>
					</template>
				</template>
			</s-table>
		</a-card>
	</a-drawer>
	
	<TimeAddForm ref="addFormRef" @successful="tableRef.refresh()" />
	<TimeEditForm ref="editFormRef" @successful="tableRef.refresh()" />
	<TimeAlreadyForm ref="alreadyFormRef" @successful="tableRef.refresh()" />
</template>

<script setup name="bizloadtime">
	import { cloneDeep } from 'lodash-es'
	import TimeAddForm from './timeAddForm.vue'
	import TimeEditForm from './timeEditForm.vue'
	import TimeAlreadyForm from './timeAlreadyForm.vue'
	import bizLoadTimeApi from '@/api/biz/bizLoadTimeApi'
	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 addFormRef = ref()
	const editFormRef = ref()
	const alreadyFormRef = ref()
	const recordData = ref()
	const title = ref()
	const pointId = ref()

	const columns = [
		{
			title: '序号',
			width: 50,
			dataIndex: 'serial',
			align:'center'
		},
		{
			title: '装货时间段',
			dataIndex: 'beginEndTime',
			align:'center'
		},
		{
			title: '可预约次数',
			dataIndex: 'availableNumber',
			align:'center'
		},
		{
			title: '已预约次数',
			dataIndex: 'alreadyNumber',
			align:'center'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizLoadTimeEdit', 'bizLoadTimeDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}
	// 打开抽屉
	const onOpen = (record) => {
		recordData.value = record
		title.value = "【" + record.loadPoint + "】-装货时间配置"
		searchFormState.value = {
			pointId: record.id
		}
		pointId.value = record.id
		visible.value = true
	}
	// 加载字段数据
	const loadData = (parameter) => {
		return bizLoadTimeApi.bizLoadTimePage(Object.assign(parameter, searchFormState.value)).then((res) => {
			return res
		})
	}
	// 关闭抽屉
	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>
