<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="所属类型" name="type">
						<a-input v-model:value="searchFormState.type" placeholder="请输入所属类型" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="启用状态" name="enabledState">
						<a-select
							v-model:value="searchFormState.enabledState"
							placeholder="请选择启用状态"
							:options="enabledStateOptions"
						/>
					</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" style="height: 100%" class="mt-4">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('extendFieldAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('extendFieldBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchExtendField"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'enabledState'">
					{{ $TOOL.dictTypeData('COMMON_STATUS', record.enabledState) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('extendFieldEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['extendFieldEdit', 'extendFieldDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteExtendField(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('extendFieldDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="extendfield">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import Form from './detail/form.vue'
	import extendFieldApi from '@/api/base/extendfield/extendFieldApi'
	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: 'name'
		},
		{
			title: '所属类型',
			dataIndex: 'type'
		},
		{
			title: '字段名',
			dataIndex: 'fieldName'
		},
		{
			title: '字段类型',
			dataIndex: 'fieldType'
		},
		{
			title: '备注',
			dataIndex: 'remarks'
		},
		{
			title: '启用状态',
			dataIndex: 'enabledState'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['extendFieldEdit', 'extendFieldDelete'])) {
		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)
		return extendFieldApi.extendFieldPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteExtendField = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		extendFieldApi.extendFieldDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchExtendField = (params) => {
		extendFieldApi.extendFieldDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	const enabledStateOptions = tool.dictList('COMMON_STATUS')
</script>
