<template>
	<a-card :bordered="false">
		<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="number">
						<a-input v-model:value="searchFormState.number" placeholder="请输入编码" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="名称" name="name">
						<a-input v-model:value="searchFormState.name" 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="$TOOL.dictList('COMMON_STATUS')"
						/>
					</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" 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 class="table-operator">
				<a-space>
					<a-button
						type="primary"
						@click="
							navigateTo('/basicData/brand/detail', {
								type: 'ADD'
							})
						"
						v-if="hasPerm('sysBrandAdd')"
					>
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('sysBrandDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchRecords"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'enabledState'">
<!--					<a-switch
						checkedValue="ENABLE"
						unCheckedValue="DISABLED"
						checked-children="启用"
						un-checked-children="停用"
						v-model:checked="record.enabledState"
					/>-->
					{{ $TOOL.dictTypeData('COMMON_STATUS', record.enabledState) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a
							@click="
								navigateTo('/basicData/brand/detail', {
									type: 'SEARCH',
									id: record.id
								})
							"
							v-if="hasPerm('sysBrandEdit')"
							>查看</a
						>
						<a-divider type="vertical" v-if="hasPerm(['sysBrandEdit', 'sysBrandDelete'], 'and')" />
						<a
							@click="
								navigateTo('/basicData/brand/detail', {
									type: 'EDIT',
									id: record.id
								})
							"
							v-if="hasPerm('sysBrandEdit')"
							>编辑</a
						>
						<a-divider type="vertical" v-if="hasPerm(['sysBrandEdit', 'sysBrandDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteRecord(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('sysBrandDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
</template>

<script setup name="brand">
	import sysBrandApi from '@/api/base/brand/sysBrandApi'
	import { useTableManagement } from '@/hook/useTableManagement'

	const brandColumns = [
		{
			title: '编码',
			dataIndex: 'number',
			sorter: (a, b) => a.number.length - b.number.length,
			sortDirections: ['descend', 'ascend'],
			align: 'center',
			resizable: true,
			width: 300,
			ellipsis: true
		},
		{
			title: '名称',
			dataIndex: 'name',
			sorter: (a, b) => a.name.length - b.name.length,
			sortDirections: ['descend', 'ascend'],
			align: 'center',
			resizable: true,
			width: 300,
			ellipsis: true
		},
		{
			title: '可用状态',
			dataIndex: 'enabledState',
			align: 'center',
			resizable: true,
			width: 100,
			ellipsis: true
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			sorter: (a, b) => a.createTime.length - b.createTime.length,
			sortDirections: ['descend', 'ascend'],
			align: 'center',
			resizable: true,
			width: 300,
			ellipsis: true
		}
	]

	const {
		searchFormState,
		tableRef,
		selectedRowKeys,
		columns,
		loadData,
		reset,
		deleteRecord,
		deleteBatchRecords,
		options,
		searchFormRef,
		toolConfig,
		navigateTo
	} = useTableManagement(
		{
			page: sysBrandApi.page,
			delete: sysBrandApi.sysBrandDelete
		},
		brandColumns,
		['sysBrandEdit', 'sysBrandDelete']
	)
</script>
