<template>
	<div class="system-parameter-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="settingName">
					<el-input v-model="state.queryParams.settingName" placeholder="请填写参数名称" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="参数" prop="code">
					<el-input v-model="state.queryParams.code" 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)" v-waves 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,
				}"
			>
				<template #buttons>
					<el-button type="primary" @click="addParameter" v-waves v-auth="'system:parameter:add'">
						<SvgIcon name="ele-Plus" class="mr5" />新增参数
					</el-button>
				</template>
			</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
					:print-config="{}"
					:scrollY="{ enabled: true, gt: 100 }"
					id="systemParameter"
					:custom-config="{ storage: true }"
					showHeaderOverflow
				>
					<vxe-column field="settingName" title="参数名称" min-width="120"></vxe-column>
					<vxe-column field="code" title="参数" min-width="120"></vxe-column>
					<vxe-column field="settingValue" title="参数值" min-width="140"></vxe-column>
					<vxe-column field="remark" title="参数说明" width="300"></vxe-column>
					<vxe-column field="creator.name" title="创建人" 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="sort" title="排序" width="100"></vxe-column>
					<vxe-column title="操作" fixed="right" width="120" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="updateParameter(row)" v-auth="'system:parameter:edit'" title="修改参数"> 修改 </el-button>
							<el-button link type="info" @click="viewParameter(row)" 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>
		<!--  参数新增  -->
		<parameter-add ref="ParameterAddRef" @updateList="queryList" />
		<!--  参数编辑  -->
		<parameter-edit ref="ParameterEditRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="systemParameter">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { getSystemSettings } from '@/api/system/parameter';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';

// 引入组件
const ParameterAdd = defineAsyncComponent(() => import('@/views/system/parameter/components/Parameter-add.vue')); // 参数新增
const ParameterEdit = defineAsyncComponent(() => import('@/views/system/parameter/components/Parameter-edit.vue')); // 参数编辑
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 20,
		settingName: null, // 参数名称
		code: null, // 参数编码
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<any>(null); // 表单ref
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	getSystemSettings(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 ParameterAddRef = ref<RefType>(); // 参数新增
const addParameter = () => {
	ParameterAddRef.value.openDialog();
};
// 修改参数
const ParameterEditRef = ref<RefType>(); // 参数编辑
const updateParameter = (row: any) => {
	ParameterEditRef.value.openDialog(row.id);
};
// 查看详情
const viewParameter = (row: any) => {
	ParameterEditRef.value.openDialog(row.id, true);
};
// 删除参数
const parameterDelete = (row: any) => {
	ElMessageBox.confirm(`此操作将删除模板：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			// workflowDelete(row.id).then(() => {
			ElMessage.success('删除成功');
			queryList();
			// });
		})
		.catch(() => {});
};
// 页面加载时
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>