<template>
	<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="6">
					<a-form-item label="车牌号码" name="licensePlate">
						<a-input v-model:value="searchFormState.licensePlate" placeholder="车牌号码查询" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="进出时间：" name="inOutTime">
						<a-range-picker v-model:value="searchFormState.inOutTime" value-format="YYYY-MM-DD" show-date />
					</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-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 #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizAccessRecordAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-button
						v-if="hasPerm('bizAccessRecordBatchDelete')"
						buttonName="批量删除"
                        icon="DeleteOutlined"
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchBizAccessRecord"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'inOutFactory'">
					<a-tag
						:color="
							record.inOutFactory === '1'
								? 'volcano'
								: record.inOutFactory === '2'
								  ? 'processing'
								  : 'error'
						"
					>
						{{ $TOOL.dictTypeData('in_out_factory', record.inOutFactory) }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'releaseType'">
					<a-tag
						:color="
							record.releaseType.toString() === '1'
								? 'volcano'
								: record.releaseType.toString() === '2'
								  ? 'processing'
								  : 'error'
						"
					>
						{{ $TOOL.dictTypeData('release_type', record.releaseType.toString()) }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="detailRef.onOpen(record)" >详情</a>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<Detail ref="detailRef" @successful="tableRef.refresh()" />
</template>

<script setup name="bizaccessrecord">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import Detail from './detail.vue'
	import bizAccessRecordApi from '@/api/biz/bizAccessRecordApi'
	const tableRef = ref()
	const detailRef = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }

	//查询数据
	const searchFormState = ref({appointmentFlag:'1'})
	const searchFormRef = ref()

	const columns = [
		{
			title: '车牌号码',
			dataIndex: 'licensePlate',
			align:'center'
		},
		{
			title: '车牌颜色',
			dataIndex: 'licensePlateColor',
			align:'center'
		},
		{
			title: '进出类型',
			dataIndex: 'inOutFactory',
			align:'center'
		},
		{
			title: '进出时间',
			dataIndex: 'inOutTime',
			align:'center'
		},

		{
			title: '放行方式',
			dataIndex: 'releaseType',
			align:'center'
		},
	]
	// 操作栏通过权限判断是否显示
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})

	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		// inOutTime范围查询条件重载
		if (searchFormParam.inOutTime) {
			searchFormParam.inOutTimeBegin = searchFormParam.inOutTime[0]
			searchFormParam.inOutTimeEnd = searchFormParam.inOutTime[1]
			delete searchFormParam.inOutTime
		}
		return bizAccessRecordApi.bizAccessRecordPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizAccessRecord = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizAccessRecordApi.bizAccessRecordDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchBizAccessRecord = (params) => {
		bizAccessRecordApi.bizAccessRecordDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
