<template>
	<div class="statistics-center-order-num-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>
						<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>
							<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>
				</template>
			</vxe-grid>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsCenterOrderNum">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { centerOrderBusiness, centerOrderBusinessExport } from '@/api/statistics/center';
import { useRouter } from 'vue-router';
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, // 时间默认今天开始到今天结束
	},
	loading: false, // 加载
	total: 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: 'statisticsCenterOrderNum',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	align: 'center',
	columns: [
		{ field: 'userName', title: '姓名' },
		{
			title: '回访量',
			children: [
				{
					field: 'visitCenter',
					title: '中心归档量',
					slots: {
						default: ({ row }) => {
							return (
								<el-button link type="primary" onClick={() => linkDetail(row, 'visitCenter')}>
									{row.visitCenter}
								</el-button>
							);
						},
					},
				},
				{
					field: 'visitOrg',
					title: '部门归档量',
					slots: {
						default: ({ row }) => {
							return (
								<el-button link type="primary" onClick={() => linkDetail(row, 'visitOrg')}>
									{row.visitOrg}
								</el-button>
							);
						},
					},
				},
			],
		},
		{
			title: '发布量',
			children: [
				{
					field: 'publishCenter',
					title: '中心发布量',
					slots: {
						default: ({ row }) => {
							return (
								<el-button link type="primary" onClick={() => linkDetail(row, 'publishCenter')}>
									{row.publishCenter}
								</el-button>
							);
						},
					},
				},
				{
					field: 'publishOrg',
					title: '部门发布量',
					slots: {
						default: ({ row }) => {
							return (
								<el-button link type="primary" onClick={() => linkDetail(row, 'publishOrg')}>
									{row.publishOrg}
								</el-button>
							);
						},
					},
				},
				{
					field: 'publishOpen',
					title: '公开发布量',
					slots: {
						default: ({ row }) => {
							return (
								<el-button link type="primary" onClick={() => linkDetail(row, 'publishOpen')}>
									{row.publishOpen}
								</el-button>
							);
						},
					},
				},
			],
		},
		{
			title: '派单量',
			children: [
				{
					field: 'dispatchCenter',
					title: '中心派单量',
					slots: {
						default: ({ row }) => {
							return (
								<el-button link type="primary" onClick={() => linkDetail(row, 'dispatchCenter')}>
									{row.dispatchCenter}
								</el-button>
							);
						},
					},
				},
				{
					field: 'dispatchOrg',
					title: '部门派单量',
					slots: {
						default: ({ row }) => {
							return (
								<el-button link type="primary" onClick={() => linkDetail(row, 'dispatchOrg')}>
									{row.dispatchOrg}
								</el-button>
							);
						},
					},
				},
			],
		},
		{
			field: 'reTransact',
			title: '重办信件量',
			slots: {
				default: ({ row }) => {
					return (
						<el-button link type="primary" onClick={() => linkDetail(row, 'reTransact')}>
							{row.reTransact}
						</el-button>
					);
				},
			},
		},
	],
	data: [],
	params: {
		exportMethod: centerOrderBusinessExport,
		exportParams: requestParams,
	},
	sortConfig: {
		remote: true,
	},
	showFooter: true,
	footerMethod: ({ columns, data }) => {
		return [
			columns.map((column: any, columnIndex: number) => {
				if (columnIndex === 0) {
					return '合计';
				}
				if (
					['visitCenter', 'visitOrg', 'publishCenter', 'publishOrg', 'publishOpen', 'dispatchCenter', 'dispatchOrg', 'reTransact'].includes(
						column.property
					)
				) {
					return XEUtils.sum(data, column.property);
				}
				return null;
			}),
		];
	},
});
/** 搜索按钮操作 */
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');
	centerOrderBusiness(requestParams.value)
		.then((res: any) => {
			gridOptions.data = res.result ?? [];
			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 = (row: any, FieldType: string) => {
	if (row) {
		router.push({
			path: '/statistics/center/detailOrderNum',
			query: {
				tagsViewName: '工单业务量统计明细',
				StartTime: state.queryParams.crTime[0],
				EndTime: state.queryParams.crTime[1],
				FieldType,
				UserID: row.userId,
			},
		});
	}
};
onMounted(() => {
	queryList();
});
</script>
