<template>
	<div class="statistics-order-department-accept-type-container layout-padding">
    <div class="layout-padding-auto layout-padding-view pd20">
			<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="TypeId">
					<el-select v-model="state.queryParams.TypeId" placeholder="请选择来电主体" @change="handleQuery">
						<el-option :value="0" label="全部" />
						<el-option :value="1" label="市民" />
						<el-option :value="2" label="企业" />
					</el-select>
				</el-form-item>
				<el-form-item label="归档类型" prop="TypeCode">
					<el-select v-model="state.queryParams.TypeCode" placeholder="请选择归档类型" @change="handleQuery">
						<el-option label="全部" value="0" />
						<el-option label="中心归档" value="1" />
						<el-option label="部门归档" value="2" />
					</el-select>
				</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>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
				:tools="[{ toolRender: { name: 'exportAll' } }]"
			>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<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"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="statisticsOrderDepartmentAcceptType"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
					:params="{ exportMethod: departmentAcceptTypeExport, exportParams: requestParams }"
					show-footer
					:footer-method="footerMethod"
				>
					<vxe-column field="orgName" title="部门名称" min-width="200" fixed="left">
					</vxe-column>
					<vxe-column field="orgType" title="部门类别" min-width="100" fixed="left"> </vxe-column>
					<vxe-colgroup title="所有类型" align="center">
						<vxe-column title="总件数" field="allCount" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row, scope)">
									{{ scope.row.allCount }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="总时长" field="allTimes" min-width="100"> </vxe-column>
						<vxe-column title="总平均" field="averageTime" min-width="100"></vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="咨询类" align="center">
						<vxe-column title="咨询件数" field="zxAllCount" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row, scope)">
									{{ scope.row.zxAllCount }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="咨询时长" field="zxAllTimes" min-width="100"></vxe-column>
						<vxe-column title="咨询平均" field="zxAverageTime" min-width="100"></vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="举报类" align="center">
						<vxe-column title="举报件数" field="jbAllCount" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row, scope)">
									{{ scope.row.jbAllCount }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="举报时长" field="jbAllTimes" min-width="100"></vxe-column>
						<vxe-column title="举报平均" field="jbAverageTime" min-width="100"></vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="投诉类" align="center">
						<vxe-column title="投诉件数" field="tsAllCount" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row, scope)">
									{{ scope.row.tsAllCount }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="投诉时长" field="tsAllTimes" min-width="100"></vxe-column>
						<vxe-column title="投诉平均" field="tsAverageTime" min-width="100"></vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="求助类" align="center">
						<vxe-column title="求助件数" field="qzAllCount" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row, scope)">
									{{ scope.row.qzAllCount }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="求助时长" field="qzAllTimes" min-width="100"></vxe-column>
						<vxe-column title="求助平均" field="qzAverageTime" min-width="100"></vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="建议类" align="center">
						<vxe-column title="建议件数" field="jyAllCount" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row, scope)">
									{{ scope.row.jyAllCount }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="建议时长" field="jyAllTimes" min-width="100"></vxe-column>
						<vxe-column title="建议平均" field="jyAverageTime" min-width="100"></vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="意见类" align="center">
						<vxe-column title="意见件数" field="yjAllCount" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope.row, scope)">
									{{ scope.row.yjAllCount }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="意见时长" field="yjAllTimes" min-width="100"></vxe-column>
						<vxe-column title="意见平均" field="yjAverageTime" min-width="100"></vxe-column>
					</vxe-colgroup>
				</vxe-table>
			</div>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsOrderDepartmentAcceptType">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentAcceptType, departmentAcceptTypeExport } from '@/api/statistics/order';
import { defaultDate } from '@/utils/constants';
import { useRouter } from 'vue-router';
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: {
		// 查询条件
		TypeCode: '0', // 关键词
		crTime: defaultDate,
    StartTime:null,
    EndTime:null,
    TypeId:0
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	totalCount: {},
});
/** 搜索按钮操作 */
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');
	departmentAcceptType(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.list ?? [];
			state.totalCount = res.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 计算合计
const footerMethod = ({ columns, data }) => {
	return [
		columns.map((column: any, columnIndex: number) => {
			if (columnIndex === 0) {
				return '合计';
			}
			// 后端返回了数据集合 state.totalCount 所以不需要计算 直接进行赋值
			return XEUtils.get(state.totalCount, column.property);
		}),
	];
};
/** 重置按钮操作 */
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, scope: any) => {
	const prefix = scope.column.property.slice(0, 2);
	const AcceptTypeCode = row[`${prefix}AcceptanceTypeCode`] ?? null; // 受理类型
	const StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	const EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	router.push({
		name: 'statisticsDepartmentDetailAcceptType',
		query: {
			StartTime,
			EndTime,
			AcceptTypeCode,
			TypeCode: state.queryParams.TypeCode,
			OrgCode: row?.orgCode,
		},
	});
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
