<template>
	<div class="snapshot-statistics-point-list-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-grid v-bind="gridOptions" v-on="gridEvents" ref="gridRef">
				<template #form>
					<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline :disabled="state.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>
							<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 setup lang="tsx" name="snapshotStatisticsPointList">
import { defineAsyncComponent, onMounted, reactive, ref, computed } from 'vue';
import { ElMessage, FormInstance } from 'element-plus';
import Other from '@/utils/other';
import { getSSPStatisticsIntegralArea, getSSPStatisticsIntegralAreaExport } from '@/api/snapshot/statistics';
import { defaultDate } from '@/utils/constants';
import { useRouter } from 'vue-router';

// 引入组件
const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询条件
		SortField: null,
		SortRule: null,
		crTime: defaultDate,
		StartTime: null,
		EndTime: null,
	},
	tableData: [], //表单
	loading: false, // 加载
});
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: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }],
	},
	params: {
		exportMethod: getSSPStatisticsIntegralAreaExport,
		exportParams: requestParams,
	},
	customConfig: {
		storage: true,
	},
	id: 'snapshotStatisticsPointList',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{
			field: 'countyName',
			title: '区域',
			slots: {
				default: ({ row }) => {
					return (
						<el-button onClick={() => onDetail(row)} link type="primary">
							{row.countyName}
						</el-button>
					);
				},
			},
		},
		{ field: 'points', title: '历史总积分' },
		{ field: 'exchangePoints', title: '已兑换积分' },
	],
	data: [],
	sortConfig: {
		remote: true,
	},
});
const gridEvents = {
	sortChange(val: any) {
		state.queryParams.SortField = val.order ? val.field : null;
		// 0 升序 1 降序
		state.queryParams.SortRule = val.order ? (val.order == 'desc' ? 1 : 0) : null;
		handleQuery();
	},
};
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
// 改变页码
const queryList = () => {
	return new Promise((resolve, reject) => {
		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'); // 删除无用的参数
		state.loading = true;
		gridOptions.loading = true;
		getSSPStatisticsIntegralArea(requestParams.value)
			.then((response: any) => {
				gridOptions.data = response?.result ?? [];
				state.loading = false;
				gridOptions.loading = false;
				resolve(response);
			})
			.catch(() => {
				state.loading = false;
				gridOptions.loading = false;
				reject();
			});
	});
};
/** 重置按钮操作 */
const ruleFormRef = ref<FormInstance>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 查看详情
const router = useRouter();
const onDetail = (row: any) => {
	router.push({
		path: '/snapshot/statistic/detailPointList',
		query: {
			County: row.countyName,
			StartTime: requestParams.value.StartTime,
			EndTime: requestParams.value.EndTime,
			tagsViewName: `${row.countyName}积分详情`,
		},
	});
};
onMounted(() => {
	queryList();
});
</script>
