<template>
	<div class="statistics-order-yb-visit-count-container layout-pd">
		<el-card shadow="never">
			<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 label="回访员" prop="VisitName">
					<el-input v-model="state.queryParams.VisitName" placeholder="回访员名称" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</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>
		</el-card>
		<el-row :gutter="20">
			<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
				<el-card shadow="never" v-loading="state.loading" class="statistics-item">
					<el-statistic :value="state.data.aiVisitCount">
						<template #title>
							<span class="color-info font14">智能回访量</span>
						</template>
					</el-statistic>
				</el-card>
			</el-col>
			<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
				<el-card shadow="never" v-loading="state.loading" class="statistics-item">
					<el-statistic :value="state.data.aiVisitSatisfiedCount">
						<template #title>
							<span class="color-info font14">智能回访满意量</span>
						</template>
					</el-statistic>
				</el-card>
			</el-col>
			<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
				<el-card shadow="never" v-loading="state.loading" class="statistics-item">
					<el-statistic :value="state.data.aiVisitNoSatisfiedCount">
						<template #title>
							<span class="color-info font14">智能回访不满意量</span>
						</template>
					</el-statistic>
				</el-card>
			</el-col>
			<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
				<el-card shadow="never" v-loading="state.loading" class="statistics-item">
					<el-statistic :value="state.data.aiVisitFailCount">
						<template #title>
							<span class="color-info font14">智能回访失败</span>
						</template>
					</el-statistic>
				</el-card>
			</el-col>
		</el-row>
		<el-card shadow="never">
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
				:tools="[{ toolRender: { name: 'exportAll' } }]"
			>
			</vxe-toolbar>
			<vxe-table
				border
				:loading="state.loading"
				:data="state.tableData"
				:column-config="{ resizable: true }"
				:row-config="{ isCurrent: true, isHover: true, height: 30, useKey:true }"
				ref="tableRef"
				auto-resize
				:scrollY="{ enabled: true, gt: 100 }"
				show-overflow
				id="statisticsOrderYBVisitCount"
				:custom-config="{ storage: true }"
				show-footer
				:footer-method="footerMethod"
				max-height="600px"
				:params="{ exportMethod: departmentVisitExport, exportParams: requestParams }"
				showHeaderOverflow
			>
				<vxe-column field="visitName" title="回访员"></vxe-column>
				<vxe-column field="callVisitCount" title="电话回访量" sortable> </vxe-column>
				<vxe-column field="artificialVisitCount" title="人工回访量" sortable></vxe-column>
				<vxe-column field="sumCount" title="总回访量" sortable></vxe-column>
			</vxe-table>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="statisticsOrderYBVisitCount">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentVisit, departmentVisitExport } from '@/api/statistics/order';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import XEUtils from 'xe-utils';

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		VisitName: null, // 关键词
		crTime: defaultDate, // 时间默认今天开始到今天结束
	},
	tableData: [],
	loading: false, // 加载
	total: 0, // 总数
	data: {
		aiVisitCount: 0,
		aiVisitSatisfiedCount: 0,
		aiVisitNoSatisfiedCount: 0,
		aiVisitFailCount: 0,
	},
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	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');
	departmentVisit(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.visitMeasureStatisticsModelList ?? [];
			state.data = res.result ?? {};
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
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 '合计';
			}
			if (['callVisitCount', 'artificialVisitCount', 'sumCount'].includes(column.property)) {
				return XEUtils.sum(data, column.property);
			}
		}),
	];
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
<style scoped lang="scss">
:deep(.el-statistic__content) {
	font-size: 32px;
	font-weight: bold;
	margin-top: 15px;
}
.statistics-item {
	margin-bottom: 10px;
}
</style>
