<template>
	<div class="app-container">
		<el-card shadow="always">
			<!-- 查询 -->
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item label="文件名" prop="path">
					<el-input placeholder="模糊查询" clearable @keyup.enter="handleSearch" style="width: 240px" v-model="state.queryParams.name" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" plain @click="handleSearch">
						<SvgIcon name="elementSearch" />
						搜索
					</el-button>
					<el-button @click="handleQuery">
						<SvgIcon name="elementRefresh" />
						重置
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="box-card">
			<template #header>
				<div class="card-header">
					<span class="card-header-text">缓存列表</span>
					<div>
						<el-button type="danger" v-auth="'function:cache:delete'" plain @click="onTableRowDelAll">
							<SvgIcon name="elementDelete" />
							全部删除
						</el-button>
						<el-button type="danger" plain :disabled="state.multiple" v-auth="'function:cache:delete'" @click="onTabelRowDel">
							<SvgIcon name="elementDelete" />
							批量删除
						</el-button>
					</div>
				</div>
			</template>
			<!--数据表格-->
			<el-table v-loading="state.loading" :data="state.tableData" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="脚本路径" align="center" prop="name" />
				<el-table-column label="首次缓存" align="center" prop="createAt" />
				<el-table-column label="最后触发" align="center" prop="accessAt" />
				<el-table-column label="过期时间" align="center" prop="expireAt" />
				<el-table-column label="命中次数" align="center" prop="hitCount" />
				<el-table-column label="删除" align="center" width="200" class-name="medium-padding fixed-width">
					<template #default="scope">
						<div>
							<el-button v-if="scope.row.parentId != 0" type="primary" v-auth="'system:api:delete'" @click="onTabelRowDel(scope.row)">
								<SvgIcon name="elementDelete" />
							</el-button>
						</div>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页设置-->
			<div v-show="state.count > 0">
				<el-divider></el-divider>
				<el-pagination
					background
					:total="state.count"
					:current-page="state.queryParams.pageNum"
					:page-size="state.queryParams.pageSize"
					layout="total, sizes, prev, pager, next, jumper"
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
				/>
			</div>
		</el-card>
		<!-- 添加或修改配置参数对话框 -->
		<EditModule ref="editModuleRef" :title="state.title" />
	</div>
</template>

<script lang="ts" setup>
import { ref, toRefs, reactive, onMounted, getCurrentInstance, onUnmounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { listCache, delCache, delCacheAll } from '@/api/function/cache';
const { proxy } = getCurrentInstance() as any;
const editModuleRef = ref();
const state = reactive({
	// 遮罩层
	loading: true,
	// 选中数组
	ids: [],
	// 非单个禁用
	single: true,
	// 非多个禁用
	multiple: true,
	// 弹出层标题
	title: '',
	// 配置参数表格数据
	tableData: [] as Array<any>,
	// 总条数
	total: 0,
	methodOptions: [],
	// 查询参数
	queryParams: {
		// 页码
		pageNum: 1,
		// 每页大小
		pageSize: 10,
		path: undefined,
		name: undefined,
		description: undefined,
		method: undefined,
		apiGroup: undefined,
	},
});

/** 查询配置参数列表 */
const handleQuery = () => {
	state.loading = true;
	listCache().then((response) => {
		const res = response || {};
		const arr: any = Object.entries(res.data).map(([fileName, Obj]) => ({
			name: fileName,
			expireAt: new Date(Obj.expireAt).toLocaleString(),
			accessAt: Obj.accessAt == 0 ? new Date(Obj.expireAt).toLocaleString() : new Date(Obj.accessAt).toLocaleString(),
			createAt: new Date(Obj.createAt).toLocaleString(),
			hitCount: Number(Obj.hitCount)+1,
		}));

		state.tableData = arr;
		state.loading = false;
	});
};
/** 前端搜索 */
const handleSearch = () => {
	if (!state.queryParams.name) {
		handleQuery();
		return;
	}
	state.loading = true;
	const search: any = state.tableData.filter((item) => {
		return item.name.includes(state.queryParams.name);
	});
	state.tableData = search;
	state.loading = false;
};
/** 重置按钮操作 */
const resetQuery = () => {
	state.queryParams.path = undefined;
	state.queryParams.description = undefined;
	state.queryParams.method = undefined;
	state.queryParams.apiGroup = undefined;
	handleQuery();
};

// 打开新增配置参数弹窗
const onOpenAddModule = () => {
	state.title = '添加Api参数';
	editModuleRef.value.openDialog({});
};
// 打开编辑配置参数弹窗
const onOpenEditModule = (row: object) => {
	state.title = '修改Api参数';
	editModuleRef.value.openDialog(row);
};
/** 删除按钮操作 */
const onTabelRowDel = (row: any) => {
	const name = row.name || state.ids;
	ElMessageBox({
		message: '是否确认删除?',
		title: '警告',
		showCancelButton: true,
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(function () {
		return delCache(name).then(() => {
			handleQuery();
			ElMessage.success('删除成功');
		});
	});
};

/** 删除所有 */
const onTableRowDelAll = () => {
	ElMessageBox({
		message: '是否确认删除所有缓存?',
		title: '警告',
		showCancelButton: true,
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(function () {
		return delCacheAll().then(() => {
			handleQuery();
			ElMessage.success('删除成功');
		});
	});
};
// 多选框选中数据
const handleSelectionChange = (selection: any) => {
	state.ids = selection.map((item: any) => item.name);
	state.single = selection.length != 1;
	state.multiple = !selection.length;
};

//分页页面大小发生变化
const handleSizeChange = (val: any) => {
	state.queryParams.pageSize = val;
	handleQuery();
};
//当前页码发生变化
const handleCurrentChange = (val: any) => {
	state.queryParams.pageNum = val;
	handleQuery();
};

// 页面加载时
onMounted(() => {
	// 查询配置参数信息
	handleQuery();
});
// 页面卸载时
onUnmounted(() => {
	proxy.mittBus.off('onEditApiModule');
});
</script>
