<template>
	<div class="knowledge-draft-container layout-pd">
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt15">
				<el-form-item label="标题" prop="street">
					<el-input v-model="state.queryParams.street" placeholder="请填写标题" @keyup.enter="queryList" clearable> </el-input>
				</el-form-item>
				<el-form-item label="类型" prop="KnowledgeTypeId">
					<el-cascader
						:options="state.typeData"
						filterable
						:props="{ checkStrictly: true, value: 'id', label: 'name', emitPath: false, children: 'children' }"
						placeholder="请选择类型"
						clearable
						class="w100"
						v-model="state.queryParams.KnowledgeTypeId"
					>
					</el-cascader>
				</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="timeChange"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
					/>
					<!-- value-format="YYYY-MM-DD" -->
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList" :loading="state.loading" v-waves> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" v-waves 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">
			<div class="mb20">
				
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableList" v-loading="state.loading" row-key="id">
				<el-table-column prop="title" label="标题" show-overflow-tooltip></el-table-column>
				<el-table-column prop="knowledgeTypeName" label="类型" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creationTime" 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 label="操作" width="120" fixed="right" align="center">
					<template #default="{row}">
						<el-button link type="primary" @click="onEdit(row)" title="编辑" v-auth="'knowledge:draft:edit'"> 编辑 </el-button>
						<el-button link type="danger" @click="onDel(row)" title="删除" v-auth="'knowledge:draft:delete'"> 删除 </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>
	</div>
</template>

<script lang="ts" setup name="knowledgeDraft">
import { ref, reactive, onMounted, onActivated } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import { auth } from '/@/utils/authFunction';
import other from '/@/utils/other';
import { treeList } from '/@/api/knowledge/type';
import { formatDate } from '/@/utils/formatTime';
import { useRouter } from 'vue-router';
import { throttle } from '/@/utils/tools';
import {shortcuts} from '/@/utils/constants';
import { KnowledgeDel, myDraftsList } from '/@/api/knowledge';

// 定义变量内容
const state = reactive(<any>{
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 10, // 每页显示条数
		Status: '0',  // 状态 0 草稿箱 1 已发布
		ApplyType: '',  // 申请类型
		IsOvertime: '',   // 是否超时
		StartTime: '',  // 开始时间
		EndTime: '',  // 结束时间
	},
	typeData: [], // 知识分类
	tableList: [],  // 表格数据
	loading: false, // 加载状态
	total: 0, // 总条数
});
const router = useRouter(); // 路由
const ruleFormRef = ref<FormInstance>();  // 表单ref
// 创建时间
const timeChange = (val: string[]) => {
	state.queryParams.StartTime = val[0];
	state.queryParams.EndTime = val[1];
};
/** 获取草稿箱列表 */
const queryList = throttle(() => {
	if (!auth('knowledge:draft:query')) ElMessage.error('抱歉，您没有权限获取草稿箱列表');
	else {
		state.loading = true;
		let request = other.deepClone(state.queryParams);
		Reflect.deleteProperty(request, 'crTime'); // 删除无用的参数
		myDraftsList(request)
			.then((response: any) => {
				state.tableList = response?.result.items ?? [];
				state.total = response?.result.total;
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	}
}, 300);
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.StartTime = '';
	state.queryParams.EndTime = '';
	queryList();
}, 300);
// 修改知识草稿
const onEdit = (row: any) => {
	router.push({
		name: 'knowledgeEdit',
		params: {
			id: row.id,
			tagsViewName: '编辑草稿',
			isDraft: 'isDraft',
		},
	});
};
const onDel = (row: any) => {
	ElMessageBox.confirm(`确定要从草稿箱删除`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
	})
		.then(() => {
			KnowledgeDel(row.id).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
// 知识分类
const getKnowledgeType = async () => {
	state.loading = true;
	try {
		const res: any = await treeList({ IsEnable: true });
		state.typeData = res.result ?? [];
		state.loading = false;
	} catch (error) {
		state.loading = false;
	}
};
onMounted(() => {
	queryList();
	getKnowledgeType();
});
onActivated(() => {
	queryList();
});
</script>
