<template>
	<div class="statistics-call-detail-index-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
				:tools="[{ 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, keyField: 'id',useKey: true }"
					ref="tableRef"
					height="auto"
					auto-resize
					:scrollY="{ enabled: true, gt: 100 }"
					show-overflow
					id="statisticsCallDetailIndexTime"
					:custom-config="{ storage: true }"
					show-footer
					:footer-method="footerMethod"
					:params="{ exportMethod: callDetailListDateExport, exportParams: requestParams }"
				>
					<vxe-column field="hour" title="时间" width="110"> </vxe-column>
					<vxe-column field="inTotal" title="呼入总量"> </vxe-column>
					<vxe-column field="inConnectionQuantity" title="接通总量"> </vxe-column>
					<vxe-column field="notAcceptedHang" title="未接通秒挂"></vxe-column>
					<vxe-column field="inConnectionRate" title="接通率"></vxe-column>
					<vxe-column field="averageDuration" title="平均时长"></vxe-column>
					<vxe-column field="inAvailableAnswer" title="有效接通量"></vxe-column>
					<vxe-column field="inHangupImmediateWhenAnswered" title="接通秒挂"></vxe-column>
					<vxe-column field="effectiveConnectionRate" title="有效接通率"></vxe-column>
					<vxe-column field="timeoutConnection" title="超时接通量"></vxe-column>
					<vxe-column field="timeoutSuspension" title="超时挂断量"></vxe-column>
					<vxe-column field="onTimeConnectionRate" title="按时接通率"></vxe-column>
					<vxe-column field="queueByeCount" title="队列挂断"></vxe-column>
					<vxe-column field="ivrByeCount" title="IVR挂断"></vxe-column>
					<vxe-column field="outTotal" title="呼出总量"></vxe-column>
					<vxe-column field="outConnectionQuantity" title="呼出接通量"></vxe-column>
					<vxe-column field="outConnectionRate" title="呼出接通率"></vxe-column>
				</vxe-table>
			</div>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsCallDetailIndexTime">
import { onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { callDateListDate, callDetailListDateExport, callPeriodBase } from '@/api/statistics/call';
import { useRoute } from 'vue-router';
import XEUtils from 'xe-utils';

const state = reactive<any>({
	queryParams: {
		// 查询条件
		Keyword: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	callForwardingSource: [],
	totalCount: {},
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const route = useRoute();
const routeQueryParams = route.query;
const queryList = async () => {
	state.loading = true;
	try {
		requestParams.value = {
			StartTime: routeQueryParams.StartTime,
			EndTime: routeQueryParams.EndTime,
			Keyword: state.queryParams.Keyword,
		};
		const { result } = await callDateListDate(requestParams.value);
		state.tableData = result.list ?? [];
		state.totalCount = result.total;
		state.loading = false;
	} catch (e) {
		state.loading = false;
		console.log(e);
	}
};
// 获取基础信息
const getBaseInfo = async () => {
	try {
		const { result } = await callPeriodBase();
		state.callForwardingSource = result.callForwardingSource ?? [];
	} catch (e) {
		console.log(e);
	}
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 计算合计
const footerMethod = ({ columns, data }) => {
	return [
		columns.map((column: any, columnIndex: number) => {
			if (columnIndex === 0) {
				return '合计';
			}
			// 后端返回了数据集合 state.totalCount 所以不需要计算 直接进行赋值
			return XEUtils.get(state.totalCount, column.property);
		}),
	];
};
// 合计
const getSummaries = (param: any) => {
	const { columns } = param;
	const sums: string[] = [];
	columns.forEach((column: { property: string }, index: number) => {
		if (index === 0) {
			sums[index] = '合计';
			return;
		}
		switch (column.property) {
			case 'inTotal':
				sums[index] = state.totalCount?.inTotal;
				break;
			case 'inConnectionQuantity':
				sums[index] = state.totalCount?.inConnectionQuantity;
				break;
			case 'notAcceptedHang':
				sums[index] = state.totalCount?.notAcceptedHang;
				break;
			case 'inConnectionRate':
				sums[index] = state.totalCount?.inConnectionRate;
				break;
			case 'averageDuration':
				sums[index] = state.totalCount?.averageDuration;
				break;
			case 'inAvailableAnswer':
				sums[index] = state.totalCount?.inAvailableAnswer;
				break;
			case 'inHangupImmediateWhenAnswered':
				sums[index] = state.totalCount?.inHangupImmediateWhenAnswered;
				break;
			case 'effectiveConnectionRate':
				sums[index] = state.totalCount?.effectiveConnectionRate;
				break;
			case 'timeoutConnection':
				sums[index] = state.totalCount?.timeoutConnection;
				break;
			case 'timeoutSuspension':
				sums[index] = state.totalCount?.timeoutSuspension;
				break;
			case 'onTimeConnectionRate':
				sums[index] = state.totalCount?.onTimeConnectionRate;
				break;
			case 'queueByeCount':
				sums[index] = state.totalCount?.queueByeCount;
				break;
			case 'ivrByeCount':
				sums[index] = state.totalCount?.ivrByeCount;
				break;
			case 'outTotal':
				sums[index] = state.totalCount?.outTotal;
				break;
			case 'outConnectionQuantity':
				sums[index] = state.totalCount?.outConnectionQuantity;
				break;
			case 'outConnectionRate':
				sums[index] = state.totalCount?.outConnectionRate;
				break;
			default:
				sums[index] = '';
				break;
		}
	});
	return sums;
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseInfo();
});
</script>
