<template>
	<div class="statistics-call-call-out-detail-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="gridOptions.loading">
						<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-form-item>
					</el-form>
				</template>
			</vxe-grid>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsCallCallOutDetail">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { callOutDetailList, callOutDetailListExport } from '@/api/statistics/call';
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, // 总数
	callForwardingSource: [],
	totalCount: {},
});

const requestParams = ref<EmptyObjectType>({});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		tools: [{ toolRender: { name: 'exportAll' } }],
	},
	customConfig: {
		storage: true,
	},
	id: 'statisticsCallCallOutDetail',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	align: 'center',
	columns: [
		{
			field: 'date',
			title: '日期',
		},
		{
			field: 'callOutTotal',
			title: '呼出总量',
		},
		{
			title:'呼入类型',
			children:[
				{
					title: '个人服务呼出总量',
					field: 'personCallOutCount',
				},
				{
					field: 'enterpriseCallOutCount',
					title: '企业服务呼出总量'
				},
				{
					field: 'aiVisitCallOutCount',
					title: '智能回访呼出量'
				},
				{
					field: 'aiCallOutCount',
					title: '智能外呼呼出量'
				},
			]
		},
		{
			field: 'callOutPutthroughCount',
			title: '呼出接通总量',
		},
		{
			title:'呼出接通类型',
			children:[
				{
					title: '个人服务呼出接通量',
					field: 'personCallOutPutthroughCount',
				},
				{
					title: '企业服务呼出接通量',
					field: 'enterpriseCallOutPutthroughCount',
				},
				{
					title: '智能回访呼出接通量',
					field: 'aiVisitCallOutPutthroughCount',
				},
				{
					title: '智能外呼呼出接通量',
					field: 'aiCallOutPutthroughCount',
				}
			]
		},
		{
			title: '呼出接通率',
			field: 'callOutPutthroughRateText',
		},
		{
			title: '各类呼出接通率',
			children:[
				{
					title: '个人服务呼出接通率',
					field: 'personCallOutPutthroughRateText',
				},
				{
					title: '企业服务呼出接通率',
					field: 'enterpriseCallOutPutthroughRateText',
				},
				{
					title: '智能回访呼出接通率',
					field: 'aiVisitCallOutPutthroughRatetext',
				},
				{
					title: '智能外呼呼出接通率',
					field: 'aiCallOutPutthroughRateText',
				}
			]
		}
	],
	data: [],
	params: {
		exportMethod: callOutDetailListExport,
		exportParams: requestParams,
	},
	sortConfig: {
		remote: true,
	},
	showFooter:true,
	footerMethod:  ({ columns, data }) => {
		return [
			columns.map((column: any, columnIndex: number) => {
				// 后端返回了数据集合 state.totalCount 所以不需要计算 直接进行赋值
				return XEUtils.get(state.totalCount, column.property);
			}),
		];
	}
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = async () => {
	state.loading = true;
	gridOptions.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 callOutDetailList(requestParams.value);
		state.tableData = result.list ?? [];
		state.totalCount = result.total;
		gridOptions.data = state.tableData;
		state.loading = false;
		gridOptions.loading = false;
	} catch (e) {
		state.loading = false;
		gridOptions.loading = false;
		console.log(e);
	}
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
onMounted(() => {
	queryList();
});
</script>
