<template>
	<div class="statistics-order-yb-visit-count-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" ref="gridRef">
				<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="gridOptions.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="gridOptions.loading">
								<SvgIcon name="ele-Search" class="mr5" />查询
							</el-button>
							<el-button @click="resetQuery(ruleFormRef)" class="default-button" :loading="gridOptions.loading">
								<SvgIcon name="ele-Refresh" class="mr5" />重置
							</el-button>
						</el-form-item>
					</el-form>
					<el-row :gutter="20" class="mt10">
						<el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
							<el-card shadow="hover" v-loading="gridOptions.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="hover" v-loading="gridOptions.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="hover" v-loading="gridOptions.loading" class="statistics-item cursor-pointer" @click="linkDetail">
								<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="hover" v-loading="gridOptions.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>
				</template>
			</vxe-grid>
		</div>
	</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';
import { useRouter } from 'vue-router';

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 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: 'statisticsOrderYBVisitCount',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{
			field: 'visitName',
			title: '回访员',
			minWidth: 100,
		},
		{
			field: 'callVisitCount',
			title: '电话回访量',
			minWidth: 100,
			sortable: true,
		},
		{
			field: 'artificialVisitCount',
			title: '人工回访量',
			minWidth: 100,
			sortable: true,
		},
		{
			field: 'sumCount',
			title: '总回访量',
			minWidth: 100,
			sortable: true,
		},
	],
	data: [],
	params: {
		exportMethod: departmentVisitExport,
		exportParams: requestParams,
	},
	showFooter: true,
	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 handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	gridOptions.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 ?? {};
			gridOptions.data = res.result?.visitMeasureStatisticsModelList ?? [];
			gridOptions.loading = false;
		})
		.catch(() => {
			gridOptions.loading = false;
		});
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
// 查看详情
const router = useRouter();
const linkDetail = () => {
	router.push({
		path: '/statistics/order/detailVisitContent',
		query: {
			tagsViewName: '智能回访不满意明细',
			StartTime: requestParams.value.StartTime,
			EndTime: requestParams.value.EndTime,
		},
	});
};
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;
}
.statistics-item {
	border: var(--el-border);
}
</style>
