<template>
	<div class="statistics-center-seats-back-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="statisticsCenterSeatsBack">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { centerRefundSeatCenter, centerRefundSeatCenterExport } from '@/api/statistics/center';
import { useRouter } from 'vue-router';

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: 'statisticsCenterSeatsBack',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ field: 'userName', title: '姓名' },
		{
			field: 'sendOrderNum',
			title: '派单件总量',
			slots: {
				default: ({ row }) => {
					return (
						<el-button link type="primary" onClick={() => linkDetail(row, 'sendOrderNum')}>
							{row.sendOrderNum}
						</el-button>
					);
				},
			},
			sortable: true,
		},
		{
			field: 'sendOrderBackNum',
			title: '派单件退办信件数',
			sortable: true,
			slots: {
				default: ({ row }) => {
					return (
						<el-button link type="primary" onClick={() => linkDetail(row, 'sendOrderBackNum')}>
							{row.sendOrderBackNum}
						</el-button>
					);
				},
			},
		},
		{
			field: 'sendOrderBackRate',
			title: '派单件退办信件率',
		},
		{
			field: 'sendOrderBackNumber',
			title: '退回次数',
			sortable: true,
			slots: {
				default: ({ row }) => {
					return (
						<el-button link type="primary" onClick={() => linkDetail(row, 'sendOrderBackNumber')}>
							{row.sendOrderBackNumber}
						</el-button>
					);
				},
			},
		},
		{
			field: 'centreFileNum',
			title: '话务部归档件总量',
			sortable: true,
			slots: {
				default: ({ row }) => {
					return (
						<el-button link type="primary" onClick={() => linkDetail(row, 'centreFileNum')}>
							{row.centreFileNum}
						</el-button>
					);
				},
			},
		},
		{
			field: 'centreFileBackNum',
			title: '发布退回件数',
			sortable: true,
			slots: {
				default: ({ row }) => {
					return (
						<el-button link type="primary" onClick={() => linkDetail(row, 'centreFileBackNum')}>
							{row.centreFileBackNum}
						</el-button>
					);
				},
			},
		},
		{
			field: 'centreFileBackRate',
			title: '发布退回件占比',
		},
		{
			field: 'centreFileBackNumber',
			title: '发布退回次数',
			sortable: true,
			slots: {
				default: ({ row }) => {
					return (
						<el-button link type="primary" onClick={() => linkDetail(row, 'centreFileBackNumber')}>
							{row.centreFileBackNumber}
						</el-button>
					);
				},
			},
		},
	],
	data: [],
	params: {
		exportMethod: centerRefundSeatCenterExport,
		exportParams: requestParams,
	},
});
/** 搜索按钮操作 */
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');
	centerRefundSeatCenter(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, type?: string) => {
	if (row) {
		router.push({
			path: '/statistics/center/detailSeatsBack',
			query: {
				tagsViewName: '坐席退回统计明细',
				UserId: row.userId,
				StartTime: state.queryParams.crTime[0],
				EndTime: state.queryParams.crTime[1],
				StatisticsType: type,
			},
		});
	}
};
onMounted(() => {
	queryList();
});
</script>
