<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="待处理" name="0"></el-tab-pane>
				<el-tab-pane label="处理完成" name="1"></el-tab-pane>
				<el-tab-pane label="已退回" name="2"></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.ApplyTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
						</el-select>
					</el-form-item>
					<el-form-item label="关键字" prop="Keyword">
						<el-input v-model="state.queryParams.Keyword" @keyup.enter="getList" 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="tiemChange"
							value-format="YYYY-MM-DD[T]HH:mm:ss.sss[Z]"
						/>
						<!-- 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="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="content" label="申请内容" show-overflow-tooltip></el-table-column>
				<el-table-column prop="knowledgeApplyType" label="申请类型" show-overflow-tooltip>
					<template #default="{ row }">
						<span>
							{{ row.knowledgeApplyType === 0 ? '新增' : '更新' }}
						</span>
					</template>
				</el-table-column>
				<el-table-column label="状态" show-overflow-tooltip>
					<template #default="{ row }">
						<span style="color: var(--el-color-primary)" v-if="row.status === 0">待处理</span>
						<span style="color: var(--el-color-success)" v-if="row.status === 1">处理完成</span>
						<span style="color: var(--el-color-danger)" v-if="row.status === 2">已退回</span>
					</template>
				</el-table-column>
				<el-table-column prop="content" label="处理人" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creationName" label="申请人" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creationOrgName" label="部门名称" show-overflow-tooltip></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 prop="toNo" label="截止时间" show-overflow-tooltip width="170">
					<template #default="scope">
						<span>{{ formatDate(scope.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="scope">
						<span>{{ formatDate(scope.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="scope">
						<span>{{ formatDate(scope.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="180" fixed="right" align="center">
					<template #default="{ row }">
						<el-button text type="primary" @click="onDetail(row)" title="查看详情" v-if="[1, 2].includes(row.status)"> 查看详情 </el-button>
						<el-button text type="primary" @click="onHandle(row)" title="处理" v-if="[0].includes(row.status)"> 处理 </el-button>
						<el-button text type="danger" @click="onReturn(row)" title="退回" v-if="[0].includes(row.status)"> 退回 </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>
		<Detail ref="DetailRef" />
		<Handle ref="HandleRef" @updateList="getList" />
	</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 { formatDate } from '/@/utils/formatTime';
import table2excel from 'js-table2excel';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
// 引入节流
import { throttle } from '/@/utils/tools';
import { KnowledgeApply, KnowledgeReturn } from '/@/api/knowledge/apply';

// 引入组件
const Detail = defineAsyncComponent(() => import('/@/views/knowledge/apply/component/Detail.vue'));
const Handle = defineAsyncComponent(() => import('/@/views/knowledge/apply/component/Handle.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: ' ',
		ApplyType: '',
		IsOvertime: '',
		StartTime: '',
		EndTime: '',
	},
	tableList: [],
	loading: false,
	total: 0,
	multipleSelection: [],
	ApplyTypeOptions: [
		{
			label: '新增',
			value: 0,
		},
		{
			label: '修改',
			value: 1,
		},
	],
});
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const ruleFormRef = ref<FormInstance>();
const DetailRef = ref();
const HandleRef = 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'); // 删除无用的参数
	KnowledgeApply(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 = '';
	state.queryParams.PageIndex = 1;
	getList();
}, 1000);
// 切换tab 查询列表
const handleClick = () => {
	resetQuery();
};
// 查看详情
const onDetail = (row:any) => {
	DetailRef.value.openDialog(row)
};
// 处理
const onHandle = (row:any) => {
	HandleRef.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('操作成功');
					getList();
				})
				.catch(() => {});
		})
		.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>
