<template>
	<div class="statistics-center-detail-accept-center-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">
					<el-date-picker
						v-model="state.queryParams.crTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
						:clearable="false"
					/>
				</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-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="statisticsCenterDetailAcceptCenter"
					:custom-config="{ storage: true }"
					:params="{ exportMethod: exportMethod, exportParams: requestParams }"
					showHeaderOverflow
				>
					<template v-if="tableType === 'seat'">
						<vxe-column field="acceptUserName" title="值班坐席"></vxe-column>
					</template>
					<template v-else>
						<vxe-column field="hour" title="时间段" min-width="100"></vxe-column>
						<vxe-column field="dutyDeskNum" title="值班坐席"></vxe-column>
					</template>
					<vxe-column field="acceptNum" title="受理总件"></vxe-column>
					<vxe-column field="validNum" title="有效件"></vxe-column>
					<vxe-column field="repetitionNum" title="重复件"></vxe-column>
					<vxe-column field="invalidNum" title="无效件"></vxe-column>
					<vxe-column field="handleNum" title="已办件"></vxe-column>
					<vxe-column field="noHandleNum" title="待办件"></vxe-column>
					<vxe-column field="backNum" title="退回件"></vxe-column>
				</vxe-table>
			</div>
			<pagination
				v-if="tableType === 'seat'"
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			>
			</pagination>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsCenterDetailAcceptCenter">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { useRoute } from 'vue-router';
import { centerAcceptDuty, centerAcceptDutyExport, centerAcceptDutyTime, centerAcceptDutyTimeExport } from '@/api/statistics/center';
import Other from '@/utils/other';
import { FormInstance } from 'element-plus';
import { defaultDate, defaultTimeStartEnd, shortcuts } from '@/utils/constants';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		// 查询条件
		crTime: defaultDate, //
		StartTime: null,
		EndTime: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
const route = useRoute(); // 路由
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 重置按钮操作 */
const ruleFormRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
const routeQueryParams = route.query;
const requestParams = ref<EmptyObjectType>({});
const tableType = routeQueryParams.type;
const exportMethod = ref(centerAcceptDutyExport);
/** 获取列表 */
const queryList = async () => {
	try {
		state.loading = true;
		if (routeQueryParams.type === 'seat') {
			requestParams.value = Other.deepClone(state.queryParams);
			requestParams.value.StartTime = state.queryParams.crTime[0];
			requestParams.value.EndTime = state.queryParams.crTime[1];
			Reflect.deleteProperty(requestParams.value, 'crTime');
			const { result } = await centerAcceptDuty(requestParams.value);
			state.tableData = result.items ?? [];
			state.total = result.total;
			exportMethod.value = centerAcceptDutyExport;
			state.loading = false;
		} else {
			requestParams.value = Other.deepClone(state.queryParams);
			requestParams.value.StartTime = state.queryParams.crTime[0];
			requestParams.value.EndTime = state.queryParams.crTime[1];
			Reflect.deleteProperty(requestParams.value, 'crTime');
			const { result } = await centerAcceptDutyTime(requestParams.value);
			state.tableData = result ?? [];
			exportMethod.value = centerAcceptDutyTimeExport;
			state.loading = false;
		}
	} catch (e) {
		console.log(e);
		state.loading = false;
	}
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	state.queryParams.crTime = [routeQueryParams.StartTime, routeQueryParams.EndTime];
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
