<template>
	<div class="judicial-statistics-area-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>
					<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="judicialStatisticsArea"
					:custom-config="{ storage: true }"
					:params="{ exportMethod: areaStatisticsExport, exportParams: requestParams }"
					showHeaderOverflow
					show-footer
					:footer-method="footerMethod"
				>
					<vxe-column field="areaName" title="区域"></vxe-column>
					<vxe-column field="orderCountNum" title="办件总量">
						<template #default="{ row }">
							<el-button type="primary" link @click="linkDetail(row, 'orderCountNum')">
								{{ row.orderCountNum }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="theClueIsTrue" title="线索属实量">
						<template #default="{ row }">
							<el-button type="primary" link @click="linkDetail(row, 'theClueIsTrue')">
								{{ row.theClueIsTrue }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="theClueIsNotTrue" title="线索不属实量">
						<template #default="{ row }">
							<el-button type="primary" link @click="linkDetail(row, 'theClueIsNotTrue')">
								{{ row.theClueIsNotTrue }}
							</el-button>
						</template>
					</vxe-column>
					<vxe-column field="enforcementOrder" title="行政执法工单">
						<template #default="{ row }">
							<el-button type="primary" link @click="linkDetail(row, 'enforcementOrder')">
								{{ row.enforcementOrder }}
							</el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
		</div>
	</div>
</template>
<script setup lang="tsx" name="judicialStatisticsArea">
import { onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDateTime, defaultTimeStartEnd, shortcuts } from '@/utils/constants';
import { areaStatistics, areaStatisticsExport } from '@/api/judicial';
import { useRouter } from 'vue-router';
import XEUtils from 'xe-utils';

const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		VisitName: null, // 关键词
		crTime: defaultDateTime, // 时间默认今天开始到今天结束
	},
	tableData: [],
	loading: false, // 加载
	total: 0, // 总数
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const requestParams = ref({});
const queryList = () => {
	state.loading = true;
	const StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	const EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	requestParams.value = {
		StartTime,
		EndTime,
	};
	areaStatistics(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 跳转详情
const router = useRouter();
const linkDetail = (row: any, type: string) => {
	const StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
	const EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
	switch (type) {
		case 'orderCountNum':
			router.push({
				name: 'judicialDetailArea',
				query: {
					StartTime,
					EndTime,
					type: 'orderCountNum',
					areaCode: row.areaCode,
				},
			});
			break;
		case 'theClueIsTrue':
			router.push({
				name: 'judicialDetailArea',
				query: {
					StartTime,
					EndTime,
					type: 'theClueIsTrue',
					areaCode: row.areaCode,
				},
			});
			break;
		case 'theClueIsNotTrue':
			router.push({
				name: 'judicialDetailArea',
				query: {
					StartTime,
					EndTime,
					type: 'theClueIsNotTrue',
					areaCode: row.areaCode,
				},
			});
			break;
		case 'enforcementOrder':
			router.push({
				name: 'judicialDetailArea',
				query: {
					StartTime,
					EndTime,
					type: 'enforcementOrder',
					areaCode: row.areaCode,
				},
			});
			break;
	}
};
// 计算合计
const footerMethod = ({ columns, data }) => {
	return [
		columns.map((column: any, columnIndex: number) => {
			if (columnIndex === 0) {
				return '合计';
			}
			if (['orderCountNum', 'theClueIsTrue', 'theClueIsNotTrue', 'enforcementOrder'].includes(column.property)) {
				return XEUtils.sum(data, column.property);
			}
		}),
	];
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
