<template>
	<div class="snapshot-statistics-area-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 :disabled="gridOptions.loading">
						<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="IndustryId">
							<el-select v-model="state.queryParams.IndustryId" class="w100" placeholder="请选择行业" @change="queryList" clearable>
								<el-option v-for="item in industry" :key="item.id" :label="item.name" :value="item.id" />
							</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>
				</template>
			</vxe-grid>
		</div>
	</div>
</template>

<script lang="tsx" setup name="snapshotStatisticsArea">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import { getSSPStatisticsArea, getSSPStatisticsAreaExport, getSSPStatisticsBaseData } from '@/api/snapshot/statistics';
import Other from '@/utils/other';
import XEUtils from 'xe-utils';

// 引入组件
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件

// 定义变量内容
const state = reactive<any>({
	loading: false,
	queryParams: {
		crTime: defaultDate, // 时间默认今天开始到今天结束
		StartTime: null,
		EndTime: null,
		IndustryId: null,
	},
});

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: 'snapshotStatisticsArea',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	align: 'center',
	columns: [
		{
			field: 'county',
			title: '区域',
		},
		{
			field: 'juBaoZongShu',
			title: '举报总数',
		},
		{
			field: 'yiBanJieShu',
			title: '已办结数',
		},
		{
			field: 'banJieLv',
			title: '办结率',
		},
		{
			field: 'shiMinShenHeTongGuoShu',
			title: '市民审核通过数',
		},
		{
			field: 'shiMinZongJinE',
			title: '市民总金额',
		},
		{
			field: 'shiMinYiLing',
			title: '市民已领',
		},
		{
			field: 'shiMinDaiLing',
			title: '市民待领',
		},
		{
			field: 'shiMinDaiBuLing',
			title: '市民待补领',
		},
		{
			field: 'wangGeYuanZongJianShu',
			title: '网格员总件数',
		},
		{
			field: 'wangGeYuanShenHeTongGuoShu',
			title: '网格员审核通过数',
		},
		{
			field: 'wangGeYuanZongJinE',
			title: '网格员总金额',
		},
		{
			field: 'wangGeYuanYiLing',
			title: '网格员已领',
		},
		{
			field: 'wangGeYuanDaiLing',
			title: '网格员待领',
		},
		{
			field: 'wangGeYuianDaiBuLing',
			title: '网格员待补领',
		},
	],
	data: [],
	params: {
		exportMethod: getSSPStatisticsAreaExport,
		exportParams: requestParams,
	},
	showFooter: true,
	footerMethod: ({ columns, data }) => {
		return [
			columns.map((column: any, columnIndex: number) => {
				if (columnIndex === 0) {
					return '合计';
				}
				if (
					[
						'juBaoZongShu',
						'yiBanJieShu',
						'shiMinShenHeTongGuoShu',
						'shiMinZongJinE',
						'shiMinYiLing',
						'shiMinDaiLing',
						'shiMinDaiBuLing',
						'wangGeYuanZongJianShu',
						'wangGeYuanShenHeTongGuoShu',
						'wangGeYuanZongJinE',
						'wangGeYuanYiLing',
						'wangGeYuanDaiLing',
						'wangGeYuianDaiBuLing',
					].includes(column.property)
				) {
					return XEUtils.sum(data, column.property);
				}
				return null;
			}),
		];
	},
});
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 获取参数列表
const queryList = () => {
	state.loading = true;
	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'); // 删除无用的参数
	getSSPStatisticsArea(requestParams.value)
		.then((res) => {
			state.loading = false;
			gridOptions.data = res.result ?? [];
			gridOptions.loading = false;
		})
		.finally(() => {
			state.loading = false;
			gridOptions.loading = false;
		});
};
// 重置表单
const ruleFormRef = ref<any>(null); // 表单ref
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
const industry = ref<EmptyArrayType>([]);
// 获取基础数据
const getBaseData = () => {
	getSSPStatisticsBaseData()
		.then(({ result }) => {
			industry.value = result.industry ?? [];
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	queryList();
	getBaseData();
});
</script>
