<template>
	<div class="statistics-call-detail-index-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>
					<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" /></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="有效接通"> 呼入时长大于15秒的接通量 </el-descriptions-item>
							<el-descriptions-item label="接通秒挂"> （无效接通）：呼入时长小于15秒的接通量 </el-descriptions-item>
							<el-descriptions-item label="有效接通率">有效接通/接通总量 </el-descriptions-item>
							<el-descriptions-item label="超时接通量">（ 电话开始时间-电话接通时间）>15秒的接通量 </el-descriptions-item>
							<el-descriptions-item label="超时挂断量"> 呼入时长小于15秒的接通量（和接通秒挂一样） </el-descriptions-item>
							<el-descriptions-item label="按时接通率"> 一接通总量-(超时挂断量+超时接通量)/接通总量 </el-descriptions-item>
							<el-descriptions-item label="队列挂断"> 在队列等待中挂断 </el-descriptions-item>
							<el-descriptions-item label="IVR挂断"> 语音提示等待中挂断 </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: 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="statisticsCallDetailIndex"
					:custom-config="{ storage: true }"
					show-footer
					:footer-method="footerMethod"
					:params="{ exportMethod: callDetailListExport, exportParams: requestParams }"
				>
					<vxe-column field="date" title="日期" width="100">
						<template #default="scope">
							<el-button type="primary" link @click="linkDetail(scope, 'date')">
								{{ scope.row.date }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="inTotal" title="呼入总量">
						<template #default="scope">
							<el-button type="primary" link @click="linkDetail(scope)">
								{{ scope.row.inTotal }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="inConnectionQuantity" title="接通总量">
						<template #default="scope">
							<el-button type="primary" link @click="linkDetail(scope)">
								{{ scope.row.inConnectionQuantity }}
							</el-button>
						</template>
					</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="statisticsCallDetailIndex">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { callDetailList, callDetailListExport } from '@/api/statistics/call';
import { useRouter } from 'vue-router';
import dayjs from 'dayjs';
import XEUtils from 'xe-utils';

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		crTime: defaultDate,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	totalCount: {},
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = async () => {
	state.loading = true;
	try {
		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');
		const { result } = await callDetailList(requestParams.value);
		state.tableData = result.list ?? [];
		state.totalCount = result.total;
		state.loading = false;
	} catch (e) {
		state.loading = false;
		console.log(e);
	}
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	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 router = useRouter();
const linkDetail = (scope: any, type?: string) => {
	if (type === 'date') {
		router.push({
			path: '/statistics/call/detailIndexTime',
			query: {
				StartTime: dayjs(scope.row.date).startOf('day').format('YYYY-MM-DD[T]HH:mm:ss'),
				EndTime: dayjs(scope.row.date).endOf('day').format('YYYY-MM-DD[T]HH:mm:ss'),
			},
		});
	} else {
		const TypeCode = scope.column.property === 'inTotal' ? '1' : '2';
		router.push({
			path: '/statistics/call/detailIndexCall',
			query: {
				StartTime: dayjs(scope.row.date).startOf('day').format('YYYY-MM-DD[T]HH:mm:ss'),
				EndTime: dayjs(scope.row.date).endOf('day').format('YYYY-MM-DD[T]HH:mm:ss'),
				TypeCode,
			},
		});
	}
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
