<template>
	<div class="tels-callLog-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" :loading="state.loading">
						<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 #statusText="{ row }">
					<el-tag :type="row.statusText === '成功' ? 'success' : 'danger'"> {{ row.statusText }}</el-tag>
				</template>
				<!-- 表格操作 -->
				<template #operation="{ row }">
					<el-button link type="primary" @click="onDetail(row)" title="查看日志明细" v-auth="'system:log:detail'"> 日志明细 </el-button>
				</template>
			</ProTable>
		</el-card>
		<!-- 日志明细 -->
		<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 { ElButton } from 'element-plus';
import { throttle } from '@/utils/tools';
import { logList } from '@/api/system/log';
import { formatDate } from '@/utils/formatTime';

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

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{
		prop: 'creationTime',
		label: '操作时间',
		width: 170,
		render: (scope: any) => {
			return <span>{formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS')}</span>;
		},
	},
	{ prop: 'nameText', label: '操作动作' },
	{ prop: 'creatorName', label: '操作人', width: 200 },
	{ prop: 'statusText', label: '操作结果', width: 100 },
	{ prop: 'executeUrl', label: '操作地址' },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 100, align: 'center' },
]);
// 定义变量内容
const state = reactive({
	queryParams: {
    PageIndex: 1, // 当前页
    PageSize: 10, // 每页条数
		Keyword: null, // 关键字
	},
	tableData: [], // 列表数据
	loading: false, // 加载
	total: 0, // 总条数
});
const ruleFormRef = ref<FormInstance>(); // 表单ref
const searchCol = ref(true); // 展开/收起
// 展开/收起
const closeSearch = () => {
	searchCol.value = !searchCol.value;
};
/** 通话记录列表 */
const queryList = async () => {
	state.loading = true;
	try {
		const response = await logList(state.queryParams);
		state.tableData = response.result?.items ?? [];
		state.total = response.result?.total ?? 0;
		state.loading = false;
	} catch (e) {
		state.loading = false;
		console.log(e);
	}
};
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
}, 300);
// 查看日志明细
const logDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	logDetailRef.value?.openDialog(row);
};
onMounted(() => {
	queryList();
});
</script>
<style lang="scss" scoped>
.arrow {
	transition: transform var(--el-transition-duration);
	cursor: pointer;
}
.arrow.is-reverse {
	transform: rotateZ(-180deg);
}
</style>
