<template>
	<div class="statistics-department-handle-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 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="OrgName">
					<el-input v-model="state.queryParams.OrgName" 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>
			<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="statisticsDepartmentDetailHandleOrg"
					:custom-config="{
						storage: true,
					}"
					showHeaderOverflow
					:params="{ exportMethod: departmentOrderOrgExport, exportParams: requestParams }"
					show-footer
					:footer-method="footerMethod"
				>
					<vxe-column field="orgName" title="部门名称" min-width="200" fixed="left">
						<template #default="scope">
							<el-button type="primary" link @click="linkDetail(scope)">
								{{ scope.row.orgName }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="orgTypeText" title="部门类别" min-width="100" fixed="left"> </vxe-column>
					<vxe-column field="orderCountNum" title="信件总量" min-width="90" fixed="left"> </vxe-column>
					<vxe-colgroup title="办件信息" align="center">
						<vxe-column title="已办案件" field="ybOrderCountNum" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '0')">
									{{ scope.row.ybOrderCountNum }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="在办案件" field="zbOrderCountNum" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '1')">
									{{ scope.row.zbOrderCountNum }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="办结率" field="orderCompletionRate" min-width="100"> </vxe-column>
						<vxe-column title="按时办结率" field="completeOnTimeRate" min-width="100"></vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="延期次数" align="center">
						<vxe-column title="延期次数" field="orderDelayCount" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '2')">
									{{ scope.row.orderDelayCount }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="延期率" field="orderDelayRate" min-width="100"></vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="超期信息" align="center">
						<vxe-column title="已办超期" field="ybOverdue" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '3')">
									{{ scope.row.ybOverdue }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="待办超期" field="zbOverdue" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '4')">
									{{ scope.row.zbOverdue }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="会签已办超期" field="hqybOverdue" min-width="110">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '5')">
									{{ scope.row.hqybOverdue }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="会签待办超期" field="hqzbOverdue" min-width="110">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '6')">
									{{ scope.row.hqzbOverdue }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="超期件数" field="subtotalOverdue" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '7')">
									{{ scope.row.subtotalOverdue }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="延期率" field="hqOverdueRate" min-width="100"></vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="会签信息" align="center">
						<vxe-column title="会签已办" field="delayEnd" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '8')">
									{{ scope.row.delayEnd }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="会签待办" field="delayWait" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '9')">
									{{ scope.row.delayWait }}
								</el-button>
							</template>
						</vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="归档信息" align="center">
						<vxe-column title="待归档" field="toBeArchived" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '10')">
									{{ scope.row.toBeArchived }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="已归档" field="archived" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '11')">
									{{ scope.row.archived }}
								</el-button>
							</template>
						</vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="发布信息" align="center">
						<vxe-column title="待发布" field="waitPublished" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '12')">
									{{ scope.row.waitPublished }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="公开件" field="publishedOpen" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '13')">
									{{ scope.row.publishedOpen }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="不公开件" field="publishedNoOpen" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '14')">
									{{ scope.row.publishedNoOpen }}
								</el-button>
							</template>
						</vxe-column>
					</vxe-colgroup>
					<vxe-colgroup title="甄别件" align="center">
						<vxe-column title="申请总量" field="screenCount" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '15')">
									{{ scope.row.screenCount }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="待甄别" field="screenApproval" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '16')">
									{{ scope.row.screenApproval }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="已同意" field="screenPass" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '17')">
									{{ scope.row.screenPass }}
								</el-button>
							</template>
						</vxe-column>
						<vxe-column title="未同意" field="screenNotPass" min-width="100">
							<template #default="scope">
								<el-button type="primary" link @click="linkDetail(scope, '18')">
									{{ scope.row.screenNotPass }}
								</el-button>
							</template>
						</vxe-column>
					</vxe-colgroup>
					<vxe-column field="satisfactionRate" title="总满意率" min-width="100"> </vxe-column>
				</vxe-table>
			</div>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsDepartmentDetailHandleOrg">
import { onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentOrderOrg, departmentOrderOrgExport } from '@/api/statistics/department';
import { useRoute, useRouter } from 'vue-router';
import XEUtils from 'xe-utils';

// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive({
	queryParams: {
		// 查询条件
		OrgName: null, // 部门名称
		TypeId: 0,
	},
	tableData: [], //表单
	loading: false, // 加载
	totalCount: 0, // 总计
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const route = useRoute();
const routeQueryParams = route.query;
const requestParams = ref<EmptyObjectType>({});
const queryList = () => {
	state.loading = true;
	const StartTime = routeQueryParams.StartTime;
	const EndTime = routeQueryParams.EndTime;
	const isProvince = routeQueryParams.isProvince;
	const OrgCode = routeQueryParams.id;
	requestParams.value = {
		StartTime,
		EndTime,
		OrgName: state.queryParams.OrgName,
		OrgCode,
		isProvince,
		TypeId: state.queryParams.TypeId,
	};
	departmentOrderOrg(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.list ?? [];
			state.totalCount = res.result?.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 计算合计
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 router = useRouter();
const linkDetail = (scope, key?: string) => {
	const StartTime = routeQueryParams.StartTime;
	const EndTime = routeQueryParams.EndTime;
	if (key) {
		// 点击数字
		router.push({
			name: 'statisticsDepartmentDetailHandle',
			query: {
				StartTime,
				EndTime,
				OrgCode: scope.row.orgCode,
				StatisticsType: key,
				isProvince: routeQueryParams.isProvince,
				ParentOrgCode: routeQueryParams.id,
			},
		});
	} else {
		// 点击部门
		router.push({
			name: 'statisticsDepartmentDetailHandleOrg',
			query: {
				StartTime,
				EndTime,
				isProvince: routeQueryParams.isProvince,
				id: scope.row.orgCode,
				ParentOrgCode: routeQueryParams.id,
			},
		});
	}
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
