<template>
	<a-drawer title="装货时间配置" :width="650" :open="visible" :destroy-on-close="true" @close="onClose">
		<a-card :bordered="false" style="margin-bottom: 10px" class="mb-2">
			<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
				<a-row :gutter="24">
					<a-col :span="9">
						<a-form-item label="装货开始时间" name="confStartTime">
							<a-range-picker v-model:value="searchFormState.confStartTime" value-format="YYYY-MM-DD HH:mm" show-time allow-clear  />
						</a-form-item>
					</a-col>
					<a-col :span="9">
						<a-form-item label="装货结束时间" name="confEndTime">
							<a-range-picker v-model:value="searchFormState.confEndTime" value-format="YYYY-MM-DD HH:mm" show-time allow-clear  />
						</a-form-item>
					</a-col>
					<a-col :span="6">
						<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
						<a-button style="margin: 0 8px" @click="reset">重置</a-button>
						
						<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizLoadTimeAdd')">
							<template #icon><plus-outlined /></template>
							新增
						</a-button>
					</a-col>
				</a-row>
			</a-form>
		</a-card>
		<a-card :bordered="false">
			<s-table
				ref="tableRef"
				:columns="columns"
				:data="loadData"
				bordered
				:row-key="(record) => record.id"
			>
				<template #bodyCell="{ column, record, index }">
					<template v-if="column.dataIndex === 'serial'">
						{{ index + 1 }}
					</template>
					<template v-if="column.dataIndex === 'action'">
						<a-space>
							<a @click="formRef.onOpen(record)" v-if="hasPerm('bizLoadTimeEdit')">编辑</a>
							<a-divider type="vertical" v-if="hasPerm(['bizLoadTimeEdit', 'bizLoadTimeDelete'], 'and')" />
							<a-button type="link" danger size="small" v-if="hasPerm('bizLoadTimeDelete')" @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="bizloadtime">
	import { cloneDeep } from 'lodash-es'
	import Form from './timeform.vue'
	import bizLoadTimeApi from '@/api/biz/bizLoadTimeApi'

	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '装货点位',
			dataIndex: 'loadPoint'
		},
		{
			title: '装货时间段',
			dataIndex: 'beginTime-endTime'
		},
		{
			title: '可预约次数',
			dataIndex: 'availableNumber'
		},
		{
			title: '已预约日期',
			dataIndex: 'alreadyDate'
		},
		{
			title: '已预约次数',
			dataIndex: 'alreadyNumber'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizLoadTimeEdit', 'bizLoadTimeDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}
	// 打开抽屉
	const onOpen = (record) => {
		recordData.value = record
		searchFormState.value = {
			loadPointId: 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
					}
				]

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