<template>
	<div class="statistics-call-seats-date-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>
				<template #toolbar_buttons>
					<el-popover :width="500" trigger="click">
						<template #reference>
							<el-button type="primary" title="口径说明"><SvgIcon name="ele-QuestionFilled" class="mr5"/>口径说明</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="呼入IVR挂断"> 呼入但是在IVR中挂断的总量 </el-descriptions-item>
							<el-descriptions-item label="呼出接通"> 呼出已接通的总量 </el-descriptions-item>
							<el-descriptions-item label="呼出未接通">呼出未接通的总量 </el-descriptions-item>
						</el-descriptions>
					</el-popover>
				</template>
			</vxe-grid>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsCallSeatsDate">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { callDateSimple, callDateSimpleExport } 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, // 总数
	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' } }],
		slots: {
			buttons: 'toolbar_buttons',
		},
	},
	customConfig: {
		storage: true,
	},
	id: 'statisticsCallSeatsDate',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{
			field: 'date',
			title: '日期',
		},
		{
			field: 'inTotal',
			title: '呼入总量',
			slots:{
				default (scope:any) {
					return (
						<el-button type="primary" onClick={() => linkDetail(scope)} link>
							{scope.row.inTotal}
						</el-button>
					)
				}
			}
		},
		{
			field: 'inConnectionRate',
			title: '接通率',
		},
		{
			field: 'inConnectionQuantity',
			title: '呼入接通',
			slots:{
				default (scope:any) {
					return (
						<el-button type="primary" onClick={() => linkDetail(scope)} link>
							{scope.row.inConnectionQuantity}
						</el-button>
					)
				}
			}
		},
		{
			field: 'inNotAnswered',
			title: '挂机量',
			slots:{
				default (scope:any) {
					return (
						<el-button type="primary" onClick={() => linkDetail(scope)} link>
							{scope.row.inNotAnswered}
						</el-button>
					)
				}
			}
		},
		{
			field: 'notAcceptedHang',
			title: '呼入队列挂断',
			slots:{
				default (scope:any) {
					return (
						<el-button type="primary" onClick={() => linkDetail(scope)} link>
							{scope.row.notAcceptedHang}
						</el-button>
					)
				}
			}
		},
		{
			field: 'ivrByeCount',
			title: '呼入IVR挂断',
			slots:{
				default (scope:any) {
					return (
						<el-button type="primary" onClick={() => linkDetail(scope)} link>
							{scope.row.ivrByeCount}
						</el-button>
					)
				}
			}
		},
		{
			field: 'outConnectionQuantity',
			title: '呼出接通',
			slots:{
				default (scope:any) {
					return (
						<el-button type="primary" onClick={() => linkDetail(scope)} link>
							{scope.row.outConnectionQuantity}
						</el-button>
					)
				}
			}
		},
		{
			field: 'outNotAnswered',
			title: '呼出未接通',
			slots:{
				default (scope:any) {
					return (
						<el-button type="primary" onClick={() => linkDetail(scope)} link>
							{scope.row.outNotAnswered}
						</el-button>
					)
				}
			}
		},
	],
	data: [],
	params: {
		exportMethod: callDateSimpleExport,
		exportParams: requestParams,
	},
	sortConfig: {
		remote: true,
	},
	showFooter:true,
	footerMethod:  ({ columns, data }) => {
		return [
			columns.map((column: any, columnIndex: number) => {
				if (columnIndex === 0) {
					return '合计';
				}
				// 后端返回了数据集合 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 callDateSimple(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();
};
// 查看详情
const router = useRouter();
const linkDetail = (scope: any) => {
	router.push({
		path: '/statistics/call/detailSeatDate',
		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'),
			FieldName:scope.column.property
		},
	});
};
onMounted(() => {
	queryList();
});
</script>
