<template>
	<div class="knowledge-knowledgeDelApply-container layout-pd">
		<el-card shadow="never">
			<el-tabs v-model="state.queryParams.Status" @tab-change="handleClick">
				<el-tab-pane label="全部" name="0"></el-tab-pane>
				<el-tab-pane label="审核中" name="1"></el-tab-pane>
				<el-tab-pane label="审核通过" name="2"></el-tab-pane>
				<el-tab-pane label="审核不通过" name="3"></el-tab-pane>
				<el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt15">
					<el-form-item label="关键字" prop="Keyword">
						<el-input v-model="state.queryParams.Keyword" placeholder="标题/创建人/创建部门/申请部门" @keyup.enter="getList" clearable style="width: 300px;"> </el-input>
					</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="tiemChange"
							value-format="YYYY-MM-DD[T]HH:mm:ss.sss[Z]"
						/>
						<!-- value-format="YYYY-MM-DD" -->
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="getList" :loading="state.loading" v-waves> <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-tabs>
		</el-card>
		<el-card shadow="never">
			<div class="flex-center-between mb20">
				<p class="table-title">信息列表</p>
				<div>
					<el-button type="primary" :disabled="!state.multipleSelection.length" @click="onImportTable">
						<SvgIcon name="iconfont icon-daochu" class="mr5" />导出
					</el-button>
				</div>
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableList" v-loading="state.loading" row-key="id" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" :reserve-selection="true" />
				<el-table-column prop="fromNo" label="标题" show-overflow-tooltip></el-table-column>
				<el-table-column prop="toNo" label="类型" show-overflow-tooltip></el-table-column>
				<el-table-column prop="toNo" label="热点" show-overflow-tooltip></el-table-column>
				<el-table-column prop="toNo" label="作者" show-overflow-tooltip></el-table-column>
				<el-table-column prop="toNo" label="创建部门" show-overflow-tooltip></el-table-column>
				<el-table-column prop="toNo" label="阅读次数" show-overflow-tooltip></el-table-column>
				<el-table-column label="状态" show-overflow-tooltip>
					<template #default="scope">
						<el-button text type="primary">
							{{ scope.row.toNo }}
						</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="toNo" label="申请时间" show-overflow-tooltip width="170">
					<template #default="scope">
						<span>{{ formatDate(scope.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="scope">
						<el-button text type="primary" @click="onRecord(scope.row)" title="查看审核记录"> 查看审核记录 </el-button>
						<el-button text type="primary" @click="onRecall(scope.row)" 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="getList" />
		</el-card>
		<!-- 审核记录 -->
		<ProcessRecord ref="processRecordRef" />
	</div>
</template>

<script lang="ts" setup name="knowledgeDelApply">
import { ref, reactive, onMounted, defineAsyncComponent } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage,ElMessageBox } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import table2excel from 'js-table2excel';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
// 引入节流
import { throttle } from '/@/utils/tools';
import { KnowledgeDeletelist,KnowledgeRevoke } from '/@/api/knowledge';

// 引入组件
const ProcessRecord = defineAsyncComponent(() => import('/@/views/knowledge/component/ProcessRecord.vue'));

const shortcuts = [
	{
		text: '近一周',
		value: () => {
			const end = new Date();
			const start = new Date();
			start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
			return [start, end];
		},
	},
	{
		text: '近一个月',
		value: () => {
			const end = new Date();
			const start = new Date();
			start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
			return [start, end];
		},
	},
	{
		text: '近三个月',
		value: () => {
			const end = new Date();
			const start = new Date();
			start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
			return [start, end];
		},
	},
];

// 定义变量内容
const state = reactive(<any>{
	queryParams: {
		PageIndex: 1,
		PageSize: 10,
		Status: '0',
		ApplyType: '',
		IsOvertime: '',
		StartTime: '',
		EndTime: '',
	},
	tableList: [],
	loading: false,
	total: 0,
	multipleSelection: [],
	directionList: [
		{
			label: '全部',
			value: '',
		},
		{
			label: '呼入',
			value: 0,
		},
		{
			label: '呼出',
			value: 1,
		},
	],
});
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const ruleFormRef = ref<FormInstance>();
const processRecordRef = ref();
// 创建时间
const tiemChange = (val: string[]) => {
	state.queryParams.StartTime = val[0];
	state.queryParams.EndTime = val[1];
};
/** 获取知识库删除列表 */
const getList = () => {
	state.loading = true;
	let request = JSON.parse(JSON.stringify(state.queryParams));
	Reflect.deleteProperty(request, 'crTime'); // 删除无用的参数
	KnowledgeDeletelist(request)
		.then((response: any) => {
			state.tableList = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 多选表格
const handleSelectionChange = (val: any) => {
	state.multipleSelection = val;
};
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.StartTime = '';
	state.queryParams.EndTime = '';
	getList();
}, 1000);
// 切换tab 查询列表
const handleClick = () => {
	getList();
};
// 查看申请记录
const onRecord = (row: any) => {
	processRecordRef.value.openDialog(row);
};
// 撤回
const onRecall = (row: any) => {
	ElMessageBox.confirm(`是否确定要撤回申请？撤回后请到【主动撤回】中进行查看和管理`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
	})
		.then(() => {
			KnowledgeRevoke(row.id).then(() => {
				ElMessage.success('操作成功');
				getList();
			});
		})
		.catch(() => {});
};
// 导出表格
const onImportTable = () => {
	const tabeHeader = [
		{ key: 'userName', colWidth: '', title: '用户名称', type: 'text', isCheck: true },
		{ key: 'fromNo', colWidth: '', title: '主叫号码', type: 'text', isCheck: true },
		{ key: 'toNo', colWidth: '', title: '被叫号码', type: 'text', isCheck: true },
		{ key: 'callTypeText', colWidth: '', title: '通话类型', type: 'text', isCheck: true },
		{ key: 'callStatusText', colWidth: '', title: '通话状态', type: 'text', isCheck: true },
		{ key: 'callDirectionText', colWidth: '', title: '呼叫方向', type: 'text', isCheck: true },
		{ key: 'endByText', colWidth: '', title: '通话结束方', type: 'text', isCheck: true },
		{ key: 'phoneIspText', colWidth: '', title: '运营商', type: 'text', isCheck: true },
		{ key: 'ringOffTypeText', colWidth: '', title: '挂断类型', type: 'text', isCheck: true },
		{ key: 'attribution', colWidth: '', title: '归属地', type: 'text', isCheck: true },
		// { key: 'creationTime', colWidth: '', title: '创建时间', type: 'text', isCheck: true },
		// { key: 'image', colWidth: '', width: '70', height: '40', title: '图片描述', type: 'image', isCheck: true },
	];
	table2excel(tabeHeader, state.multipleSelection, `${themeConfig.value.globalTitle} ${formatDate(new Date(), 'YYYY-mm-dd HH-MM')}`);
};
onMounted(() => {
	getList();
});
</script>
<style lang="scss" scoped></style>
