<template>
	<div class="statistics-order-detail-hotspot-time-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 label="来电主体" prop="IdentityType">
						<el-select v-model="state.queryParams.IdentityType" 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-button type="primary" @click="onDetail" :loading="state.loading"> <SvgIcon name="ele-PieChart" 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="tsx" name="statisticsOrderDetailHotspotTime">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { orderHotTime, orderHotTimeExport } from '@/api/statistics/order';
import { defaultDate } from '@/utils/constants';
import { useRouter } from 'vue-router';
import Other from '@/utils/other';
import XEUtils from 'xe-utils';

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		IdentityType: 0,
		// 查询条件
		crTime: defaultDate,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
/** 搜索按钮操作 */
const handleQuery = () => {
	// state.queryParams.PageIndex = 1;
	queryList();
};
const requestParams = ref<EmptyObjectType>({});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	printConfig: {},
	columnConfig: {
		resizable: true,
	},
	toolbarConfig: {
		zoom: true,
		custom: true,
		refresh: {
			queryMethod: () => {
				queryList();
			},
		},
		tools: [{ toolRender: { name: 'exportAll' } }],
	},
	customConfig: {
		storage: true,
	},
	showFooter: true,
	footerMethod: ({ columns, data }) => {
		return [
			columns.map((column: any, columnIndex: number) => {
				if (columnIndex === 0) {
					return '合计';
				}
				return XEUtils.sum(data, column.property);
			}),
		];
	},
	id: 'statisticsOrderDetailHotspotTime',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [],
	data: [],
	params: {
		exportMethod: orderHotTimeExport,
		exportParams: requestParams,
		isSpecialExport: true,
	},
});
/** 获取列表 */
const queryList = async () => {
	state.loading = true;
	gridOptions.loading = true;
	try {
		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');
		const res = await orderHotTime(requestParams.value);
		let columns = res.result.titleData?.map((item: any) => {
			return {
				prop: item.key,
				label: item.value,
				field: item.key,
				title: item.value,
				align: 'center',
				sortable: true,
				minWidth: 110,
			};
		});
		// 计算横轴小计
		const tableData = res.result?.item.map((item: any) => {
			let subtotal = 0;
			for (let i of res.result.titleData) {
				subtotal += item[i.key];
			}
			return {
				...item,
				subtotal,
			};
		});
		columns.unshift(
			{
				prop: 'Hour',
				label: '时间段',
				field: 'Hour',
				title: '时间段',
				align: 'center',
				minWidth: 120,
				fixed: 'left',
			},
			{
				prop: 'subtotal',
				label: '小计',
				align: 'center',
				field: 'subtotal',
				title: '小计',
				minWidth: 120,
				sortable: true,
			}
		);
		gridOptions.data = tableData;
		gridOptions.columns = columns;
		gridOptions.loading = false;
		state.loading = false;
	} catch (e) {
		state.loading = false;
		gridOptions.loading = false;
		console.log(e);
	}
};
/** 重置按钮操作 */
const statisticalTimeRef = ref<RefType>();
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	statisticalTimeRef.value.reset();
	queryList();
};
// 查看图表
const router = useRouter();
const onDetail = () => {
	router.push({
		name: 'statisticsOrderTimeHotspot',
	});
};
onMounted(() => {
	queryList();
});
</script>
