<template>
	<a-card :bordered="false" v-if="indexShow">
		<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" name="name">
						<a-input v-model:value="searchFormState.name" placeholder="请输入NAME" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="AGE" name="age">
						<a-input v-model:value="searchFormState.age" placeholder="请输入AGE" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="SEX" name="sex">
						<a-input v-model:value="searchFormState.sex" placeholder="请输入SEX" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="REMARK" name="remark">
						<a-input v-model:value="searchFormState.remark" placeholder="请输入REMARK" />
					</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 @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
					</a>
				</a-col>
			</a-row>
		</a-form>
		<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="onDetail()" v-if="hasPerm('demo2Add')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('demo2BatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchDemo2"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="onDetail(record)" v-if="hasPerm('demo2Edit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['demo2Edit', 'demo2Delete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteDemo2(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('demo2Delete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Detail v-else ref="detailRef" @onClose="indexShow = true" @successful="tableRef.refresh()" />
</template>

<script setup name="demo2">
	import { cloneDeep } from 'lodash-es'
	import Detail from './detail.vue'
	import demo2Api from '@/api/biz/demo2Api'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const detailRef = ref()
	const indexShow = ref(true)
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: 'NAME',
			dataIndex: 'name'
		},
		{
			title: 'AGE',
			dataIndex: 'age'
		},
		{
			title: 'SEX',
			dataIndex: 'sex'
		},
		{
			title: 'REMARK',
			dataIndex: 'remark'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['demo2Edit', 'demo2Delete'])) {
		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 demo2Api.demo2Page(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteDemo2 = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		demo2Api.demo2Delete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchDemo2 = (params) => {
		demo2Api.demo2Delete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	// 切换至表单
    const onDetail = (record = null) => {
    	indexShow.value = false
    	nextTick(() => {
    		if (record) {
    			detailRef.value.onOpen(record)
    		}
    	})
    }
</script>
