<template>
	<div class="statistics-call-hot-number-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item label="热线号码" prop="gateway">
					<el-input v-model="state.queryParams.gateway" placeholder="热线号码" clearable @keyup.enter="queryList" class="keyword-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="queryList"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
					/>
				</el-form-item>
				<el-form-item label-width="0">
					<el-button type="primary" @click="queryList" :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>
		</el-card>
		<el-card shadow="never">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				@updateTable="queryList"
				:loading="state.loading"
				:pagination="false"
				show-summary
				border
				:summary-method="getSummaries"
			>
			</ProTable>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="statisticsCallHotNumber">
import { onMounted, reactive, ref } from 'vue';
import { ElButton, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { callHotline } from '@/api/statistics/call';
import { shortcuts } from '@/utils/constants';
import dayjs from 'dayjs';

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ type: 'index', fixed: 'left', width: 55, label: '序号', align: 'center' },
	{ prop: 'gateWay', label: '热线号码', align: 'center' },
	{
		prop: 'allCall',
		label: '电话呼入汇总',
		align: 'center',
		_children: [
			{ prop: 'callInCount', label: '呼入', align: 'center', sortable: true },
			{ prop: 'connectCount', label: '接通', align: 'center', sortable: true },
			{ prop: 'noConnectByeCount', label: '未接通秒挂', align: 'center', sortable: true },
			{ prop: 'callInConnectRate', label: '接通率', align: 'center', render: (scope) => <span>{scope.row.callInConnectRate}%</span> },
		],
	},
	{
		prop: 'effective',
		label: '接通话务分析',
		align: 'center',
		_children: [
			{ prop: 'effectiveCount', label: '有效接通', align: 'center', sortable: true },
			{ prop: 'aveDuration', label: '平均时长', align: 'center', sortable: true },
			{ prop: 'durationSum', label: '通话时总长', align: 'center', sortable: true },
			{ prop: 'connectByeCount', label: '接通秒挂', align: 'center', sortable: true },
			{ prop: 'timelyAnswerCount', label: '及时应答数', align: 'center', sortable: true },
			{ prop: 'effectiveRate', label: '有效率', align: 'center', render: (scope) => <span>{'' + scope.row.effectiveRate}%</span> },
		],
	},
]);
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive({
	queryParams: {
		// 查询条件
		gateway: null, // 热线号码
		crTime: [dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'), dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')],
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});

/** 获取列表 */
const queryList = throttle(() => {
	state.loading = true;
	let beginDate = null;
	let endDate = null;
	if (state.queryParams?.crTime) {
		beginDate = state.queryParams?.crTime[0];
		endDate = state.queryParams?.crTime[1];
	}
	const request = {
		beginDate,
		endDate,
		gateway: state.queryParams.gateway,
		Keyword: state.queryParams.Keyword,
	};
	callHotline(request)
		.then((res: any) => {
			state.tableData = res.result ?? [];
			state.total = res.result?.total ?? 0;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
}, 300);
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
}, 300);
// 合计
const getSummaries = (param: any) => {
	const { columns, data } = param;
	const sums: string[] = [];
	columns.forEach((column: { property: string }, index: number) => {
		if (index === 0) {
			sums[index] = '合计';
			return;
		}
		const values = data.map((item: { [x: string]: any }) => Number(item[column.property]));
		if (['callInConnectRate', 'effectiveRate', 'gateWay'].includes(column.property)) {
			//百分比不能计算
			sums[index] = '';
			return '';
		} else if (!values.every((value: unknown) => Number.isNaN(value))) {
			sums[index] = `${values.reduce((prev: any, curr: any) => {
				const value = Number(curr);
				if (!Number.isNaN(value)) {
					return prev + curr;
				} else {
					return prev;
				}
			}, 0)}`;
		} else {
			sums[index] = ' ';
		}
	});
	return sums;
};
onMounted(() => {
	queryList();
});
</script>
