<template>
	<div class="auxiliary-msg-template-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="关键字" prop="Keyword">
					<el-input
						v-model="state.queryParams.Keyword"
						placeholder="模板名称/模板内容/模板编码"
						clearable
						@keyup.enter="queryList"
						class="keyword-input"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList" :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>
		</el-card>
		<el-card shadow="never">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:total="state.total"
				v-model:page-index="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
			>
				<template #tableHeader="scope">
					<el-button type="primary" @click="addTem" v-auth="'auxiliary:msgTemplate:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="updateTem(row)" v-auth="'auxiliary:msgTemplate:edit'" title="修改模板"> 编辑 </el-button>
				</template>
			</ProTable>
		</el-card>
		<!--  新增模板  -->
		<msg-template-add ref="msgTemplateAddRef" @updateList="queryList" />
		<!--  编辑模板  -->
		<msg-template-edit ref="msgTemplateEditRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="auxiliaryMsgTemplate">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElButton, FormInstance } from 'element-plus';
import { msgTemplateList } from '@/api/auxiliary/msgTemplate';
import dayjs from 'dayjs';
// 引入组件
const MsgTemplateAdd = defineAsyncComponent(() => import('@/views/auxiliary/msgTemplate/components/M-template-add.vue')); // 新增模板
const MsgTemplateEdit = defineAsyncComponent(() => import('@/views/auxiliary/msgTemplate/components/M-template-edit.vue')); // 编辑模板

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'name', label: '模板名称' },
	{ prop: 'content', label: '模板内容', minWidth: 400 },
	{ prop: 'code', label: '模板编码' },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 120, align: 'center' },
]);
// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		Keyword: null, // 关键字
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<any>(null); // 表单ref
// 获取参数列表
const queryList = () => {
	state.loading = true;
	msgTemplateList(state.queryParams)
		.then((res) => {
			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 msgTemplateAddRef = ref<RefType>(); // 新增模板
const addTem = () => {
	msgTemplateAddRef.value.openDialog();
};
// 修改模板
const msgTemplateEditRef = ref<RefType>(); // 修改模板
const updateTem = (row: any) => {
	msgTemplateEditRef.value.openDialog(row.id);
};
// 页面加载时
onMounted(() => {
	queryList();
});
</script>
