<template>
	<search-form :fields="SearchFields" @search="handleSearch" @rest="reset"></search-form>

	<a-card class="mt-4">
		<a-row :gutter="20">
			<a-col :span="8">
				<s-table
					:alert="false"
					:columns="unitGroupColumns"
					:data="loadData"
					bordered
					:row-key="(record) => record.id"
					:tool-config="{
						plus: true,
						edit: true,
						delete: true,
						refresh: true,
						height: true,
						columnSetting: true,
						striped: false
					}"
					:row-selection="options.rowSelection"
					@plusRowData="handlePlusRowData"
					@editRowData="handleEditRowData"
					@deleteRowData="handleDeleteRowData"
				>
					<template #operator class="table-operator">
						<a-space>
							<span>单位组</span>
						</a-space>
					</template>
				</s-table>
			</a-col>
			<a-col :span="16">
				<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="formRef.onOpen()" v-if="hasPerm('sysUnitAdd')">
								<template #icon><plus-outlined /></template>
								新增
							</a-button>
							<xn-batch-delete
								v-if="hasPerm('sysUnitBatchDelete')"
								:selectedRowKeys="selectedRowKeys"
								@batchDelete="deleteBatchSysUnit"
							/>
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column.dataIndex === 'state'">
							{{ $TOOL.dictTypeData('COMMON_STATUS', record.state) }}
						</template>
						<template v-if="column.dataIndex === 'isBase'">
							{{ $TOOL.dictTypeData('YES_NO', record.isBase) }}
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a-space>
								<a @click="formRef.onOpen(record)" v-if="hasPerm('sysUnitEdit')">编辑</a>
								<a-divider type="vertical" v-if="hasPerm(['sysUnitEdit', 'sysUnitDelete'], 'and')" />
								<a-popconfirm title="确定要删除吗？" @confirm="deleteSysUnit(record)">
									<a-button type="link" danger size="small" v-if="hasPerm('sysUnitDelete')">删除</a-button>
								</a-popconfirm>
							</a-space>
						</template>
					</template>
				</s-table>
			</a-col>
		</a-row>
	</a-card>
</template>

<script setup name="sysunit">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import sysUnitApi from '@/api/baseData/unit/unitApi'
	import { unitGroupColumns } from '@/views/basicData/unit/columns/unitGroupColumns'
	import { SearchFields } from '@/views/basicData/unit/formData/SearchForm'

	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const toolConfig = {
		refresh: true,
		height: true,
		columnSetting: true,
		striped: false
	}
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '名称',
			dataIndex: 'name'
		},
		{
			title: '状态',
			dataIndex: 'state'
		},
		{
			title: '编码',
			dataIndex: 'number'
		},
		{
			title: '换算率',
			dataIndex: 'rate'
		},
		{
			title: '是否基本单位',
			dataIndex: 'isBase'
		},
		{
			title: '精度',
			dataIndex: 'precision'
		},
		{
			title: '单位组id',
			dataIndex: 'unitGroupId'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['sysUnitEdit', 'sysUnitDelete'])) {
		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 sysUnitApi.sysUnitPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}

	// 搜索
	const handleSearch = () => {}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteSysUnit = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		sysUnitApi.sysUnitDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchSysUnit = (params) => {
		sysUnitApi.sysUnitDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	const stateOptions = tool.dictList('COMMON_STATUS')
	const isBaseOptions = tool.dictList('YES_NO')

	// 表格 新增/修改/删除
	const handlePlusRowData = () => {
		formRef.value.onOpen()
	}

	const handleEditRowData = () => {
		formRef.value.onOpen()
	}

	const handleDeleteRowData = () => {}
</script>
