<template>
	<div class="system-log-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="Name">
					<el-input v-model.trim="state.queryParams.Name" placeholder="操作动作" clearable @keyup.enter="handleQuery"  />
				</el-form-item>
				<el-form-item label="操作人" prop="CreatorName">
					<el-input v-model.trim="state.queryParams.CreatorName" placeholder="操作人" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="操作地址" prop="ExecuteUrl">
					<el-input v-model.trim="state.queryParams.ExecuteUrl" placeholder="操作地址" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="请求参数" prop="ExecuteParam">
					<el-input v-model.trim="state.queryParams.ExecuteParam" placeholder="请求参数" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="是否查询全部" prop="IsAll">
					<el-select v-model="state.queryParams.IsAll" placeholder="请选择是否查询全部" @change="handleQuery">
						<el-option :value="true" label="是" />
						<el-option :value="false" label="否" />
					</el-select>
				</el-form-item>
				<el-form-item label="操作时间" prop="crTime">
					<el-date-picker
						v-model="state.queryParams.crTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
					/>
				</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" :loading="state.loading">
						<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
					:print-config="{}"
					:scrollY="{ enabled: true, gt: 100 }"
					id="systemLog"
					:custom-config="{ storage: true }"
					showHeaderOverflow
				>
					<vxe-column field="creatorName" title="操作人" min-width="120"></vxe-column>
					<vxe-column field="name" title="操作动作" min-width="140"></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="ipUrl" title="操作IP" min-width="120"></vxe-column>
					<vxe-column field="executeUrl" title="操作地址" min-width="200"></vxe-column>
					<vxe-column field="remark" title="备注" min-width="200"></vxe-column>
					<vxe-column field="statusText" title="操作结果" width="100"></vxe-column>
					<vxe-column title="操作" fixed="right" width="90" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onDetail(row)" title="查看日志明细" v-auth="'system:log:detail'"> 日志明细 </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>
		<!-- 日志明细 -->
		<log-detail ref="logDetailRef" />
	</div>
</template>

<script lang="tsx" setup name="systemLog">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { logList } from '@/api/system/log';
import { formatDate } from '@/utils/formatTime';
import { defaultDate, defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import Other from '@/utils/other';

// 引入组件
const logDetail = defineAsyncComponent(() => import('@/views/system/log/components/Detail.vue')); // 日志明细
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		Name:null, // 操作动作
		CreatorName:null, // 操作人
		ExecuteUrl:null, // 操作地址
		ExecuteParam:null, // 请求参数
		IsAll:false, // 是否查询全部
		crTime:defaultDate,
		StartTime:null, // 开始时间
		EndTime:null, // 结束时间
	},
	tableData: [], // 列表数据
	loading: false, // 加载
	total: 0, // 总条数
});
const ruleFormRef = ref<FormInstance>(); // 表单ref
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 日志列表 */

const requestParams = ref<EmptyObjectType>({});
const queryList = async () => {
	state.loading = true;
	try {
		requestParams.value = Other.deepClone(state.queryParams);
		requestParams.value.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
		requestParams.value.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
		Reflect.deleteProperty(requestParams.value, 'crTime');
		const response = await logList(requestParams.value);
		state.tableData = response.result?.items ?? [];
		state.total = response.result?.total ?? 0;
		state.loading = false;
	} catch (e) {
		state.loading = false;
		console.log(e);
	}
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 查看日志明细
const logDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	logDetailRef.value?.openDialog(row);
};
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
