<template>
	<div class="statistics-department-unSigned-container layout-padding">
    <div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item prop="crTime">
					<statistical-time v-model="state.queryParams.crTime" @change="handleQuery" ref="statisticalTimeRef" :disabled="state.loading"/>
				</el-form-item>
				<el-form-item label="数据范围" prop="Level">
					<el-segmented
						:options="[
							{
								value: '0',
								label: '全部',
							},
							{
								value: '1',
								label: '本级',
							},
							{
								value: '2',
								label: '下级',
							},
						]"
						v-model="state.queryParams.Level"
						@change="handleQuery"
						:disabled="state.loading"
					/>
				</el-form-item>
				<el-form-item label="签收状态" prop="Signed">
					<el-segmented
						:options="[
							{
								value: '0',
								label: '未签收',
							},
							{
								value: '1',
								label: '已签收',
							},
						]"
						v-model="state.queryParams.Signed"
						@change="handleQuery"
						:disabled="state.loading"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
					<el-popover :width="500" trigger="click">
						<template #reference>
							<el-button type="primary" title="口径说明"><SvgIcon name="ele-QuestionFilled" class="mr5"/>口径说明</el-button>
						</template>
						<el-descriptions title="" :column="1" border style="max-height: 400px; overflow: auto">
							<el-descriptions-item label="工单状态">未签收、办理中、退回信件、办理完成</el-descriptions-item>
							<el-descriptions-item label="来源渠道">工单来源方式</el-descriptions-item>
							<el-descriptions-item label="当前节点">当前工单流转目标所在的部门</el-descriptions-item>
							<el-descriptions-item label="工单编码">工单编号（系统自动生成）</el-descriptions-item>
							<el-descriptions-item label="受理时间">工单新建时间</el-descriptions-item>
							<el-descriptions-item label="工单标题">工单标题</el-descriptions-item>
							<el-descriptions-item label="工单期满时间">工单到期办理时间</el-descriptions-item>
							<el-descriptions-item label="转办部门">工单转办给部门办理</el-descriptions-item>
							<el-descriptions-item label="转办时间">转办工单的时间</el-descriptions-item>
							<el-descriptions-item label="签收部门">工单办理部门</el-descriptions-item>
							<el-descriptions-item label="未签收时间（小时）">未签收工单的时间</el-descriptions-item>
							<el-descriptions-item label="受理类型">工单受理类型</el-descriptions-item>
							<el-descriptions-item label="热点分类">工单热点分类</el-descriptions-item>
						</el-descriptions>
					</el-popover>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: queryList,
				}"
				:tools="[{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }]"
			>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30,useKey:true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="statisticsDepartmentUnSigned"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
					:params="{ exportMethod: departmentUnSignExport, exportParams: requestParams }"
				>
					<vxe-column field="order.expiredStatusText" title="超期状态" width="90" align="center">
						<template #default="{ row }">
							<span :class="'overdue-status-' + row.order?.expiredStatus" :title="row.order?.expiredStatusText"></span>
						</template>
					</vxe-column>
					<vxe-column field="order.statusText" title="工单状态" width="100">
						<template #default="{ row }">
							<el-text type="danger" tag="b" v-if="[1, 2, 3, 9, 101, 102, 103, 104, 105, 200].includes(row.order.status)">{{ row.order.statusText }}</el-text>
							<span v-else>{{ row.order.statusText }}</span>
						</template>
					</vxe-column>
					<vxe-column field="order.sourceChannel" title="来源渠道" width="100"></vxe-column>
					<vxe-column field="order.currentStepName" title="当前节点" width="110"></vxe-column>
					<vxe-column field="order.no" title="工单编码" width="140"></vxe-column>
					<vxe-column field="order.title" title="工单标题" width="200">
						<template #default="{ row }">
							<order-detail :order="row.order" @updateList="queryList">{{ row.order?.title }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="order.startTime" title="受理时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.startTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="order.expiredTime" title="工单期满时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.order?.expiredTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="workflowStep.creatorOrgName" title="转办部门" width="140"></vxe-column>
					<vxe-column field="workflowStep.creationTime" title="转办时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.workflowStep?.creationTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="workflowStep.acceptorOrgName" title="签收部门" width="140"></vxe-column>
					<vxe-column field="unsignedTime" title="未签收时间(小时)" width="140"></vxe-column>
					<vxe-column field="order.acceptType" title="受理类型" width="110"></vxe-column>
					<vxe-column field="order.hotspotName" title="热点分类" width="150"></vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsDepartmentUnSigned">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import { formatDate } from '@/utils/formatTime';
import { departmentUnSignExport, departmentUnSign, departmentSecondDetailExport } from '@/api/statistics/department';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';

// 引入组件
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		Level: '0',
		Signed: '0',
		crTime: defaultDate, // 时间默认今天开始到今天结束
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
const ruleFormRef = ref<RefType>(); // 表单ref
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
const requestParams = ref<EmptyObjectType>({});
/** 获取列表 */
const queryList = () => {
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	requestParams.value.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	Reflect.deleteProperty(requestParams.value, 'crTime');
	state.loading = true;
	departmentUnSign(requestParams.value)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
