<template>
	<div class="business-repeat-event-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="标题" prop="Title">
					<el-input
						v-model.trim="state.queryParams.Title"
						placeholder="请填写重复性事件标题"
						clearable
						@keyup.enter="handleQuery"
						class="keyword-input"
					/>
				</el-form-item>
				<el-form-item label="关键词" prop="KeyWords">
					<el-input v-model.trim="state.queryParams.KeyWords" placeholder="请填写关键词" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
			>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="businessRepeatEvent"
					:custom-config="{ storage: true }"
					showHeaderOverflow
				>
					<vxe-column field="title" title="重复事件标题" min-width="200"></vxe-column>
					<vxe-column field="keyWords" title="关键词" min-width="200"></vxe-column>
					<vxe-column field="creatorName" title="创建人" min-width="120"></vxe-column>
					<vxe-column field="creationTime" title="创建时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="lastModificationName" title="更新人" min-width="120"></vxe-column>
					<vxe-column field="lastModificationTime" title="更新时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.lastModificationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column title="操作" fixed="right" width="150" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onEventDetail(row)" title="查看重复性详情"> 事件详情 </el-button>
							<el-button link type="primary" @click="onEventEdit(row)" v-auth="'business:repeatEvent:edit'" title="编辑重复性事件"> 编辑 </el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
		<!-- 事件详情   -->
		<repeat-event-detail ref="repeatEventDetailRef" />
		<!-- 编辑重复性事件 -->
		<repeat-event-edit ref="repeatEventEditRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="businessRepeatEvent">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { repeatEventList } from '@/api/business/repeatEvent';

// 引入组件
const RepeatEventDetail = defineAsyncComponent(() => import('@/views/business/repeatEvent/components/Repeat-event-detail.vue')); // 重复性事件详情
const RepeatEventEdit = defineAsyncComponent(() => import('@/views/business/repeatEvent/components/Repeat-event-edit.vue')); // 编辑重复性事件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 2,
		Title: null,
		Keyword: null,
		KeyWords: null,
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<RefType>(null); // 表单ref
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	repeatEventList(state.queryParams)
		.then((res: any) => {
			state.loading = false;
			state.tableData = res.result.items ?? [];
			state.total = res.result.total ?? 0;
		})
		.finally(() => {
			state.loading = false;
		});
};
// 重置表单
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 重复性事件详情
const repeatEventDetailRef = ref<RefType>();
const onEventDetail = (row: any) => {
	repeatEventDetailRef.value.openDialog(row);
};
// 编辑重复性事件
const repeatEventEditRef = ref<RefType>();
const onEventEdit = (row: any) => {
	repeatEventEditRef.value.openDialog(row);
};
// 页面加载时
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
