<template>
	<div class="statistics-order-hotspot-container layout-padding">
    <div class="layout-padding-auto layout-padding-view pd20">
      <div class="table-search-content">
        <el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
					<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>
      </div>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-grid v-bind="gridOptions"> </vxe-grid>
			</div>
		</div>
	</div>
</template>
<script setup lang="ts" name="statisticsOrderOrgHotspot">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { departmentHotDetail } from '@/api/statistics/order';
import { defaultDate, } from "@/utils/constants";
import XEUtils from 'xe-utils';


const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive(<any>{
	queryParams: {
		// 查询条件
		crTime: defaultDate, // 时间默认今天开始到今天结束
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	printConfig: {},
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				queryList();
			},
		},
	},
	customConfig: {
		storage: true
	},
	id: 'statisticsOrderOrgHotspot',
	rowConfig: { isHover: true, height: 30,isCurrent:true,useKey:true },
	height: 'auto',
	columns: [],
	data: [],
	showFooter: true,
	footerMethod: ({ columns, data }) => {
		return [
			columns.map((column: any, columnIndex: number) => {
				if (columnIndex === 0) {
					return '合计';
				}
				return XEUtils.sum(data, column.property);
			}),
		];
	},
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	gridOptions.loading = true;
	let request:EmptyObjectType = {};
	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');
	departmentHotDetail(request)
		.then((res: any) => {
			const columns = res.result?.hotSpot ?? [];
			gridOptions.columns = columns.map((item: any) => {
				return {
					minWidth: 120,
					align: 'center',
					id: item.id,
					field: item.id,
					title: item.hotSpotName,
				};
			});
			gridOptions.columns.unshift(
				{
					title: '部门名称',
					field: 'OrgName',
					minWidth: 200,
				},
				{
					width: 55,
					align: 'center',
					field: 'sum',
					title: '小计',
				}
			);
			let tableData = res.result?.data ?? [];
			// 判断部门名称是否重复 重复的合并数据 并去除重复数据
			const arr = <any>[];
			tableData.forEach((item: any) => {
				const index = arr.findIndex((v: any) => v.OrgName === item.OrgName);
				if (index === -1) {
					arr.push(item);
				} else {
					arr[index] = Object.assign(arr[index], item);
				}
			});
			// 添加合计
			arr.forEach((item: any) => {
				item.sum = 0;
				gridOptions.columns.forEach((v: any) => {
					if (v.id !== 'sum' && v.id !== 'OrgName') {
						item.sum += item[v.id] ?? 0;
					}
				});
			});
			gridOptions.data = arr;
			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();
};
onMounted(() => {
	queryList();
});
</script>
