<template>
	<div class="statistics-order-hotspot-count-container layout-pd">
		<!-- 搜索  -->
		<el-card shadow="never">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item prop="dateType" label="时间单位">
					<el-select v-model="state.queryParams.dateType" placeholder="部门名称" @change="queryList">
						<el-option label="年" value="year" />
						<el-option label="月" value="month" />
						<el-option label="日" value="date" />
						<el-option label="自定义" value="datetimerange" />
					</el-select>
				</el-form-item>
				<el-form-item prop="crTime" v-if="state.queryParams.dateType === 'datetimerange'" label="时间">
					<el-date-picker
						v-model="state.queryParams.crTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="queryList"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:clearable="false"
					/>
				</el-form-item>
				<el-form-item prop="hbTime" v-if="state.queryParams.dateType === 'datetimerange'" label="环比时间段">
					<el-date-picker
						v-model="state.queryParams.hbTime"
						type="datetimerange"
						unlink-panels
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						:shortcuts="shortcuts"
						@change="queryList"
						value-format="YYYY-MM-DD[T]HH:mm:ss"
						:clearable="false"
					/>
				</el-form-item>
				<el-form-item prop="time" v-else label="时间">
					<el-date-picker
						v-model="state.queryParams.time"
						:type="state.queryParams.dateType"
						placeholder="选择时间"
						@change="queryList"
						:value-format="valueFormat"
						:clearable="false"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList" :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>
		</el-card>
		<el-card shadow="never">
			<ProTable
				ref="proTableRef"
				:columns="columns"
				:data="state.tableData"
				:loading="state.loading"
				row-key="id"
				lazy
				:load="load"
				:tree-props="{ children: 'children', hasChildren: 'sublevel' }"
				show-summary
				:summary-method="getSummaries"
			>
			</ProTable>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="statisticsOrderHotspotCount">
import { computed, onMounted, reactive, ref } from 'vue';
import { ElButton, FormInstance } from 'element-plus';
import { throttle } from '@/utils/tools';
import { departmentHot } from '@/api/statistics/order';
import { shortcuts } from '@/utils/constants';
import dayjs from 'dayjs';

const columns = ref<any>([
	{ prop: 'name', label: '热点名称' },
	{ prop: 'num', label: '工单数' },
	{ prop: 'chainNum', label: '环比工单数' },
	{ prop: 'chainRate', label: '日环比' },
]); // 表头
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive({
	queryParams: {
		// 查询条件
		dateType: 'date', // 关键词
		time: dayjs(new Date()).format('YYYY-MM-DD'), // 时间默认今天
		crTime: [dayjs(new Date()).startOf('date').format('YYYY-MM-DD[T]HH:mm:ss'), dayjs(new Date()).endOf('date').format('YYYY-MM-DD[T]HH:mm:ss')], // 时间默认今天开始到今天结束
		hbTime: [dayjs(new Date()).startOf('date').format('YYYY-MM-DD[T]HH:mm:ss'), dayjs(new Date()).endOf('date').format('YYYY-MM-DD[T]HH:mm:ss')], // 时间默认今天开始到今天结束
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	totalCount: {},
});
const valueFormat = computed(() => {
	return state.queryParams.dateType === 'date' ? 'YYYY-MM-DD' : state.queryParams.dateType === 'month' ? 'YYYY-MM' : 'YYYY';
});
// 设置当前时间的映射值
const getType = computed(() => {
	const statusMap: any = {
		date: '1',
		year: '3',
		month: '2',
		datetimerange: '0',
	};
	return statusMap[state.queryParams.dateType] || '';
});

/** 获取列表 */
const queryList = throttle(() => {
	state.loading = true;
	let StartTime = null;
	let EndTime = null;
	let ChainStartTime = null;
	let ChainEndTime = null;
	switch (state.queryParams.dateType) {
		case 'date':
			StartTime = dayjs(state.queryParams.time).startOf('date').format('YYYY-MM-DD[T]HH:mm:ss');
			EndTime = dayjs(state.queryParams.time).endOf('date').format('YYYY-MM-DD[T]HH:mm:ss');
			break;
		case 'month':
			StartTime = dayjs(state.queryParams.time).startOf('month').format('YYYY-MM-DD[T]HH:mm:ss');
			EndTime = dayjs(state.queryParams.time).endOf('month').format('YYYY-MM-DD[T]HH:mm:ss');
			break;
		case 'year':
			StartTime = dayjs(state.queryParams.time).startOf('year').format('YYYY-MM-DD[T]HH:mm:ss');
			EndTime = dayjs(state.queryParams.time).endOf('year').format('YYYY-MM-DD[T]HH:mm:ss');
			break;
		case 'datetimerange':
			StartTime = state.queryParams?.crTime[0];
			EndTime = state.queryParams?.crTime[1];
			ChainStartTime = state.queryParams?.hbTime[0];
			ChainEndTime = state.queryParams?.hbTime[1];
			break;
	}
	const request = {
		Type: getType.value,
		StartTime,
		EndTime,
		ChainStartTime,
		ChainEndTime,
	};
	departmentHot(request)
		.then((res: any) => {
			state.tableData = res.result.list ?? [];
			state.totalCount = res.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
}, 300);
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
}, 300);
// 懒加载
const load = (row: any, treeNode: unknown, resolve: (date: any[]) => void) => {
	let StartTime = null;
	let EndTime = null;
	let ChainStartTime = null;
	let ChainEndTime = null;
	switch (state.queryParams.dateType) {
		case 'date':
			StartTime = dayjs(state.queryParams.time).startOf('date').format('YYYY-MM-DD[T]HH:mm:ss');
			EndTime = dayjs(state.queryParams.time).endOf('date').format('YYYY-MM-DD[T]HH:mm:ss');
			break;
		case 'month':
			StartTime = dayjs(state.queryParams.time).startOf('month').format('YYYY-MM-DD[T]HH:mm:ss');
			EndTime = dayjs(state.queryParams.time).endOf('month').format('YYYY-MM-DD[T]HH:mm:ss');
			break;
		case 'year':
			StartTime = dayjs(state.queryParams.time).startOf('year').format('YYYY-MM-DD[T]HH:mm:ss');
			EndTime = dayjs(state.queryParams.time).endOf('year').format('YYYY-MM-DD[T]HH:mm:ss');
			break;
		case 'datetimerange':
			StartTime = state.queryParams?.crTime[0];
			EndTime = state.queryParams?.crTime[1];
			ChainStartTime = state.queryParams?.hbTime[0];
			ChainEndTime = state.queryParams?.hbTime[1];
			break;
	}
	const request = {
		Type: getType.value,
		StartTime,
		EndTime,
		ChainStartTime,
		ChainEndTime,
		Id: row.id,
	};
	departmentHot(request)
		.then((res: any) => {
			state.totalCount = res.result.total;
			resolve(res.result.list ?? []);
		})
		.catch(() => {});
};
// 合计
const getSummaries = (param: any) => {
	const { columns } = param;
	const sums: string[] = [];
	columns.forEach((column: { property: string }, index: number) => {
		switch (column.property) {
			case 'name':
				sums[index] = state.totalCount?.name;
				break;
			case 'num':
				sums[index] = state.totalCount?.num;
				break;
			case 'chainNum':
				sums[index] = state.totalCount?.chainNum;
				break;
			case 'chainRate':
				sums[index] = state.totalCount?.chainRate;
				break;
			default:
				sums[index] = '';
				break;
		}
	});
	return sums;
};
onMounted(() => {
	queryList();
});
</script>
