<template>
	<div class="statistics-department-detail-dp-accept-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="crTime">
					<el-date-picker
						v-model="state.queryParams.crTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="handleQuery"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:default-time="defaultTimeStartEnd"
						:clearable="false"
					/>
				</el-form-item>
				<el-form-item label="部门类别" prop="OrgType">
					<el-select v-model="state.queryParams.OrgType" 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-popover :width="500" trigger="click">
						<template #reference>
							<el-button type="primary" title="字段说明"><SvgIcon name="ele-QuestionFilled" class="mr5" />字段说明</el-button>
						</template>
						<el-descriptions title="" :column="1" border style="max-height: 400px; overflow: auto">
							<el-descriptions-item label="部门名称">办理工单的部门名称</el-descriptions-item>
							<el-descriptions-item label="部门类别">办理工单的部门类别</el-descriptions-item>
							<el-descriptions-item label="受理类型">工单受理类型</el-descriptions-item>
						</el-descriptions>
					</el-popover>
					<el-button type="primary" @click="onDetailList" :loading="state.loading"> <SvgIcon name="ele-List" 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="statisticsDepartmentDetailDpAccept"
					: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">
						<template #default="scope">
							{{ scope.row.orgName }}
						</template>
					</vxe-column>
					<vxe-column field="orgTypeText" title="部门类别" min-width="100" fixed="left"> </vxe-column>
					<vxe-column title="咨询" field="zxAllCount" min-width="100">
						<template #default="scope">
							<el-button
								type="primary"
								link
								@click="linkDetail(scope, scope.row.zxAcceptanceTypeCode)"
								v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)"
							>
								{{ scope.row.zxAllCount }}
							</el-button>
							<span v-else>{{ scope.row.zxAllCount }}</span>
						</template>
					</vxe-column>
					<vxe-column title="举报" field="jbAllCount" min-width="100">
						<template #default="scope">
							<el-button
								type="primary"
								link
								@click="linkDetail(scope, scope.row.jbAcceptanceTypeCode)"
								v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)"
							>
								{{ scope.row.jbAllCount }}
							</el-button>
							<span v-else>{{ scope.row.jbAllCount }}</span>
						</template>
					</vxe-column>
					<vxe-column title="投诉" field="tsAllCount" min-width="100">
						<template #default="scope">
							<el-button
								type="primary"
								link
								@click="linkDetail(scope, scope.row.tsAcceptanceTypeCode)"
								v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)"
							>
								{{ scope.row.tsAllCount }}
							</el-button>
							<span v-else>{{ scope.row.tsAllCount }}</span>
						</template>
					</vxe-column>
					<vxe-column title="求助" field="qzAllCount" min-width="100">
						<template #default="scope">
							<el-button
								type="primary"
								link
								@click="linkDetail(scope, scope.row.qzAcceptanceTypeCode)"
								v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)"
							>
								{{ scope.row.qzAllCount }}
							</el-button>
							<span v-else>{{ scope.row.qzAllCount }}</span>
						</template>
					</vxe-column>
					<vxe-column title="建议" field="jyAllCount" min-width="100">
						<template #default="scope">
							<el-button
								type="primary"
								link
								@click="linkDetail(scope, scope.row.jbAcceptanceTypeCode)"
								v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)"
							>
								{{ scope.row.jyAllCount }}
							</el-button>
							<span v-else>{{ scope.row.jyAllCount }}</span>
						</template>
					</vxe-column>
<!--					<vxe-column title="意见" field="yjAllCount" min-width="100">
						<template #default="scope">
							<el-button
								type="primary"
								link
								@click="linkDetail(scope, scope.row.jyAcceptanceTypeCode)"
								v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)"
							>
								{{ scope.row.yjAllCount }}
							</el-button>
							<span v-else>{{ scope.row.yjAllCount }}</span>
						</template>
					</vxe-column>
					<vxe-column title="惠民帮助" field="hmAllCount" min-width="100">
						<template #default="scope">
							<el-button
								type="primary"
								link
								@click="linkDetail(scope, scope.row.hmAcceptanceTypeCode)"
								v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)"
							>
								{{ scope.row.hmAllCount }}
							</el-button>
							<span v-else>{{ scope.row.hmAllCount }}</span>
						</template>
					</vxe-column>
					<vxe-column title="大气污染举报" field="dqAllCount" min-width="100">
						<template #default="scope">
							<el-button
								type="primary"
								link
								@click="linkDetail(scope, scope.row.dqAcceptanceTypeCode)"
								v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)"
							>
								{{ scope.row.dqAllCount }}
							</el-button>
							<span v-else>{{ scope.row.dqAllCount }}</span>
						</template>
					</vxe-column>-->
					<vxe-column title="表扬" field="byAllCount" min-width="100">
						<template #default="scope">
							<el-button
								type="primary"
								link
								@click="linkDetail(scope, scope.row.byAcceptanceTypeCode)"
								v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)"
							>
								{{ scope.row.byAllCount }}
							</el-button>
							<span v-else>{{ scope.row.byAllCount }}</span>
						</template>
					</vxe-column>
<!--					<vxe-column title="其他" field="qtAllCount" min-width="100">
						<template #default="scope">
							<el-button
								type="primary"
								link
								@click="linkDetail(scope, scope.row.qtAcceptanceTypeCode)"
								v-if="!['市直合计', '区县合计'].includes(scope.row.orgName)"
							>
								{{ scope.row.qtAllCount }}
							</el-button>
							<span v-else>{{ scope.row.qtAllCount }}</span>
						</template>
					</vxe-column>-->
					<vxe-column title="小计" field="allCount" min-width="100"> </vxe-column>
				</vxe-table>
			</div>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsDepartmentDetailDpAccept">
import { onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentAcceptType, departmentAcceptTypeExport } from '@/api/statistics/department';
import { defaultDate, defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { useRoute, useRouter } from 'vue-router';
import Other from '@/utils/other';
import XEUtils from 'xe-utils';

// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		crTime: defaultDate, // 时间默认今天开始到今天
		OrgType: 0, // 部门类别结束
		TypeId: 0, // 来电主体  0：全部 ，1：市民，2：企业
		OrgName: null, // 部门名称
	},
	tableData: [], //表单
	loading: false, // 加载
	totalCount: 0, // 总计
	IdentityType: 0,
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const route = useRoute();
const routeQueryParams = route.query;
const queryList = () => {
	requestParams.value = Other.deepClone(state.queryParams);
	requestParams.value = {
		...requestParams.value,
		StartTime: routeQueryParams.StartTime,
		EndTime: routeQueryParams.EndTime,
		OrgCode: routeQueryParams.OrgCode,
	};
	state.loading = true;
	departmentAcceptType(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result?.dataList ?? [];
			if (res.result.dataList.length) {
				state.tableData.push(res.result.citySumModel);
				state.tableData.push(res.result.countySumModel);
			}
			state.totalCount = res.result?.sumModel;
			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: any, key?: string) => {
	let StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	let EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	// 点击数字
	router.push({
		path: '/statistics/department/detailDpAcceptList',
		query: {
			StartTime,
			EndTime,
			OrgCode: scope.row.orgCode,
			AcceptTypeCode: key,
			TypeCode: state.queryParams.TypeCode,
			TypeId: state.queryParams.TypeId,
			OrgType: state.queryParams.OrgType,
		},
	});
};
// 跳转列表明细
const onDetailList = () => {
	router.push({
		path: '/statistics/department/detailDpAcceptList',
	});
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	if (routeQueryParams.StartTime && routeQueryParams.EndTime) {
		state.queryParams.crTime = [routeQueryParams.StartTime, routeQueryParams.EndTime];
	}
});
</script>
