<template>
	<div class="statistics-order-hotspot-subclass-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 prop="TypeId" label="来电主体">
							<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>
							<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>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
				:tools="[{ toolRender: { name: 'exportAll' } }]"
			>
			</vxe-toolbar>
		</div>
	</div>
</template>
<script setup lang="tsx" name="statisticsOrderHotspotSubclass">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentHotSmall, departmentHotSmallExport } from '@/api/statistics/order';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { formatDate } from '@/utils/formatTime';
import XEUtils from 'xe-utils';
import { useRouter } from 'vue-router';

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		TypeId: 0, // 关键词
		time: formatDate(new Date(), 'YYYY-MM-DD'), // 时间默认今天
		crTime: defaultDate, // 时间默认今天开始到今天结束
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});

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: 'statisticsOrderHotspotSubclass',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [
		{ field: 'hotspotName', title: '热点名称', treeNode: true },
		{
			field: 'sumCount',
			title: '分类统计',
				slots:{
				default ({row}) {
					return (
						<el-button type="primary" onClick={() => linkDetail(row)} link>
							{row.sumCount}
						</el-button>
					)
				}
			}
		},
	],
	data: [],
	params: {
		exportMethod: departmentHotSmallExport,
		exportParams: requestParams,
	},
	sortConfig: {
		remote: true,
	},
	treeConfig: {
		transform: true,
		rowField: 'hotspotCode',
		parentField: 'parentId',
		lazy: true,
		hasChild: 'hasChild',
		loadMethod({ row }) {
			// 异步加载子节点
			return fetchChildListApi(row);
		},
	},
	showFooter: true,
	footerMethod: ({ columns, data }) => {
		return [
			columns.map((column: any, columnIndex: number) => {
				if (columnIndex === 0) {
					return '合计';
				}
				if (['sumCount'].includes(column.property)) {
					return XEUtils.sum(data, column.property);
				}
				return null;
			}),
		];
	},
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	state.tableData = [];
	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');
	departmentHotSmall(requestParams.value)
		.then((res: any) => {
			state.tableData = res.result ?? [];
			state.tableData = state.tableData.map((item: any) => {
				item.children = [];
				return item;
			});
			gridOptions.data = state.tableData;
			state.loading = false;
			gridOptions.loading = false;
		})
		.catch(() => {
			state.loading = false;
			gridOptions.loading = false;
		});
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
const fetchChildListApi = (row: any) => {
	return new Promise((resolve) => {
		const request = Other.deepClone(state.queryParams);
		request.StartTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[0];
		request.EndTime = state.queryParams.crTime === null ? null : state.queryParams.crTime[1];
		Reflect.deleteProperty(request, 'crTime');
		request.HotspotCode = row.hotspotCode;
		departmentHotSmall(request)
			.then((res: any) => {
				resolve(res.result ?? []);
			})
			.catch(() => {});
	});
};
// 跳转详情
const router = useRouter();
const linkDetail = (row: any) => {
	router.push({
		path: '/statistics/order/detailHotspotArea',
		query: {
			StartTime: requestParams.value.StartTime,
			EndTime: requestParams.value.EndTime,
			HotspotCode: row.hotspotCode,
			tagsViewName: `热点类型小类统计明细`,
			type: 'hotspotSubclass',
		},
	});
};
onMounted(() => {
	queryList();
});
</script>
