<template>
	<div class="knowledge-apply-container layout-pd">
		<el-card shadow="never">
			<el-tabs v-model="state.queryParams.Status" @tab-change="handleClick">
				<el-tab-pane label="全部" name=" "></el-tab-pane>
				<el-tab-pane :label="v.value" :name="v.key" v-for="(v, i) in state.statusOptions" :key="i"></el-tab-pane>
				<el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt15">
					<el-form-item label="申请类型" prop="ApplyType">
						<el-select v-model="state.queryParams.ApplyType" placeholder="请选择申请类型" class="w100">
							<el-option v-for="item in state.eKnowledgeApplyType" :key="item.key" :label="item.value" :value="item.key" />
						</el-select>
					</el-form-item>
					<el-form-item label="关键字" prop="Keyword">
						<el-input v-model="state.queryParams.Keyword" @keyup.enter="queryList" placeholder="申请人/申请部门" clearable> </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="timeChange"
							value-format="YYYY-MM-DD[T]HH:mm:ss"
						/>
						<!-- value-format="YYYY-MM-DD" -->
					</el-form-item>
					<el-form-item label="是否超时" prop="IsOvertime">
						<el-select v-model="state.queryParams.IsOvertime" placeholder="请选择是否超时" class="w100">
							<el-option label="否" value="1" />
							<el-option label="是" value="0" />
						</el-select>
					</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-tabs>
		</el-card>
		<el-card shadow="never">
			<!-- 表格 -->
			<el-table :data="state.tableList" v-loading="state.loading" row-key="id">
				<el-table-column prop="content" label="申请内容" show-overflow-tooltip width="170"></el-table-column>
				<el-table-column prop="knowledgeApplyTypeName" label="申请类型" show-overflow-tooltip></el-table-column>
				<el-table-column label="状态" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span class="color-primary" v-if="row.status === 0">{{ row.statusName }}</span>
						<span class="color-success" v-if="row.status === 1">{{ row.statusName }}</span>
						<span class="color-danger" v-if="row.status === 2">{{ row.statusName }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="processor" label="处理人" show-overflow-tooltip width="170"></el-table-column>
				<el-table-column prop="creationName" label="申请人" show-overflow-tooltip width="170"></el-table-column>
				<el-table-column prop="creationOrgName" label="部门名称" show-overflow-tooltip width="170"></el-table-column>
				<el-table-column prop="toNo" 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 prop="toNo" label="截止时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="toNo" label="处理完成时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.handleTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="toNo" label="退回时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.returnTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="是否超时" show-overflow-tooltip>
					<template #default="{ row }">
						{{ row.isOvertime ? '是' : '否' }}
					</template>
				</el-table-column>
				<el-table-column label="操作" width="160" fixed="right" align="center">
					<template #default="{ row }">
						<el-button
							link
							type="primary"
							@click="onDetail(row)"
							title="查看详情"
							v-if="[1, 2].includes(row.status)"
							v-auth="'knowledge:apply:detail'"
						>
							查看详情
						</el-button>
						<el-button link type="primary" @click="onHandle(row)" title="处理" v-if="[0].includes(row.status)" v-auth="'knowledge:apply:handle'">
							处理
						</el-button>
						<el-button link type="danger" @click="onReturn(row)" title="退回" v-if="[0].includes(row.status)" v-auth="'knowledge:apply:recall'">
							退回
						</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>
		<knowledge-apply-detail ref="KnowledgeApplyDetailRef" />
		<knowledge-apply-handle ref="KnowledgeApplyHandleRef" @updateList="queryList" />
	</div>
</template>

<script lang="ts" setup name="knowledgeApply">
import { ref, reactive, onMounted, defineAsyncComponent } 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 { formatDate } from '/@/utils/formatTime';
import { throttle } from '/@/utils/tools';
import {shortcuts} from '/@/utils/constants';
import { handleList, KnowledgeReturn, baseData } from '/@/api/knowledge/apply';

// 引入组件
const KnowledgeApplyDetail = defineAsyncComponent(() => import('/@/views/knowledge/apply/component/Knowledge-apply-detail.vue')); // 详情组件
const KnowledgeApplyHandle = defineAsyncComponent(() => import('/@/views/knowledge/apply/component/Knowledge-apply-handle.vue')); // 处理组件

// 定义变量内容
const state = reactive(<any>{
	queryParams: {
    PageSize: 10, // 每页条数
		PageIndex: 1, // 当前页码
		Status: ' ',  // 状态
		ApplyType: '',  // 申请类型
		IsOvertime: '', // 是否超时
		StartTime: '',  // 开始时间
		EndTime: '',  // 结束时间
	},
	tableList: [],  // 表格数据
	loading: false, // 加载状态
	total: 0, // 总条数
	eKnowledgeApplyType: [],  // 申请类型数据
	statusOptions: [],  // 状态数据
});
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:apply:query')) ElMessage.error('抱歉，您没有权限查看知识申请列表');
	else {
		state.loading = true;
		let request = other.deepClone(state.queryParams);
		Reflect.deleteProperty(request, 'crTime'); // 删除无用的参数
		handleList(request)
			.then((response: any) => {
				state.tableList = response?.result.items ?? [];
				state.total = response?.result.total;
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	}
}, 500);
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	state.queryParams.StartTime = '';
	state.queryParams.EndTime = '';
	state.queryParams.PageIndex = 1;
	queryList();
}, 500);
// 切换tab 查询列表
const handleClick = () => {
	queryList();
};
// 查看详情
const KnowledgeApplyDetailRef = ref<RefType>();
const onDetail = (row: any) => {
	KnowledgeApplyDetailRef.value.openDialog(row);
};
// 处理
const KnowledgeApplyHandleRef = ref<RefType>();
const onHandle = (row: any) => {
	KnowledgeApplyHandleRef.value.openDialog(row);
};
// 退回
const onReturn = (row: any) => {
	ElMessageBox.prompt('退回原因', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		inputPlaceholder: '请输入退回原因，不超过50字符',
		draggable: true,
		inputErrorMessage: '请输入退回原因',
		inputType: 'textarea',
		inputValidator: (value) => {
			if (value) return true;
			else return '请输入退回原因';
		},
	})
		.then(({ value }) => {
			let req = {
				id: row.id,
				opinion: value,
			};
			KnowledgeReturn(req)
				.then(() => {
					ElMessage.success('操作成功');
					queryList();
				})
				.catch(() => {});
		})
		.catch(() => {});
};
// 页面基础数据
const getBaseData = async () => {
	try {
		const { result } = await baseData();
		state.statusOptions = result.eKnowledgeApplyStatus;
		state.eKnowledgeApplyType = result.eKnowledgeApplyType;
	} catch (error) {
		console.log(error);
	}
};
onMounted(() => {
	queryList();
	getBaseData();
});
</script>
<style lang="scss" scoped></style>
