<template>
	<div class="system-parameter-container layout-pd">
    <el-card shadow="never">
      <el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent>
        <el-form-item label="参数名称" prop="settingName">
          <el-input v-model="state.queryParams.settingName" placeholder="请输入参数名称" clearable @keyup.enter="queryList" />
        </el-form-item>
        <el-form-item label="参数" prop="code">
          <el-input v-model="state.queryParams.code" placeholder="请输入参数" clearable @keyup.enter="queryList" />
        </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)" v-waves class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
        </el-form-item>
      </el-form>
    </el-card>
		<el-card shadow="never">
			<div class="mb20">
				<el-button type="primary" @click="addParameter" v-waves v-auth="'system:parameter:add'">
					<SvgIcon name="ele-Plus" class="mr5" />新增
				</el-button>
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="state.loading">
				<el-table-column type="index" width="60" label="序号" />
				<el-table-column prop="settingName" label="参数名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="code" label="参数" show-overflow-tooltip width="130"></el-table-column>
				<el-table-column prop="settingValue" label="参数值" show-overflow-tooltip>
					<template #default="{ row }">
						<span v-if="row.settingValue">{{ row.settingValue.join('|') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="参数说明" show-overflow-tooltip width="300"></el-table-column>
				<el-table-column prop="creator.name" label="创建人" show-overflow-tooltip></el-table-column>
				<el-table-column prop="afterBegin" label="创建时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="sort" label="排序" show-overflow-tooltip width="60"></el-table-column>
				<el-table-column label="操作" width="200" fixed="right" 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)" v-auth="'system:parameter:detail'" title="查看详情"> 查看 </el-button>
						<!--              <el-button link type="danger" @click="parameterDelete(row)" v-auth="'system:timeLimit:detail'" title="删除参数"> 删除 </el-button>-->
					</template>
				</el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
			<!-- 分页 -->
			<pagination
				:total="state.total"
				v-model:page="state.queryParams.PageIndex"
				v-model:limit="state.queryParams.PageSize"
				@pagination="queryList"
			/>
		</el-card>
		<!--  参数新增  -->
		<parameter-add ref="ParameterAddRef" @updateList="queryList" />
		<!--  参数编辑  -->
		<parameter-edit ref="ParameterEditRef" @updateList="queryList" />
	</div>
</template>

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

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

// 定义变量内容
const state = reactive<any>({
	loading: false, // 加载状态
	queryParams: {
		// 查询参数
		PageIndex: 1,
		PageSize: 10,
		settingName: '', // 参数名称
		code: '', // 参数编码
	},
	total: 0, // 总条数
	tableData: [], // 表格数据
});
const ruleFormRef = ref<any>(null); // 表单ref
// 获取参数列表
const queryList = throttle(() => {
	state.loading = true;
  if (!auth('system:parameter:query')) ElMessage.error('抱歉，您没有权限获取系统参数列表！');
  else {
    getSystemSettings(state.queryParams)
        .then((res) => {
          state.loading = false;
          state.tableData = res.result.items ?? [];
          state.total = res.result.total ?? 0;
        })
        .finally(() => {
          state.loading = false;
        });
  }
}, 1000);
// 重置表单
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(() => {});
};
// 页面加载时
onMounted(() => {
	queryList();
});
</script>

<style lang="scss" scoped>
.system-parameter-container {

}
</style>
