<template>
	<div class="statistics-order-time-source-container layout-pd">
		<el-card shadow="never">
			<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 label-width="0">
					<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-List" class="mr5" /> 列表明细 </el-button>
				</el-form-item>
			</el-form>
			<el-row :gutter="20">
				<el-col :xs="24" :sm="24" :md="24" :lg="16" :xl="16">
					<v-chart class="chart" :option="option" :loading="state.loading" autoresize />
				</el-col>
				<el-col :xs="24" :sm="24" :md="24" :lg="8" :xl="8">
					<div style="height: 70vh">
						<vxe-grid v-bind="gridOptions"> </vxe-grid>
					</div>
				</el-col>
			</el-row>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="statisticsOrderTimeSource">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { orderSourceTime, orderSourceTimeExport } from '@/api/statistics/order';
import { useRouter } from 'vue-router';
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, //
		StartTime: null,
		EndTime: null,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	callForwardingSource: [],
});
/** 搜索按钮操作 */
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: 'statisticsOrderTimeSource',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	columns: [],
	data: [],
	params: {
		exportMethod: orderSourceTimeExport,
		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 orderSourceTime(requestParams.value);
		let columns = res.result.titleData?.map((item: any) => {
			return {
				prop: item.key,
				label: item.value,
				field: item.key,
				title: item.value,
				sortable: true,
				align: 'center',
				minWidth: 130,
			};
		});
		// 计算横轴小计
		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;
		// 图标数据与组装
		const legendData = res.result.titleData?.map((item: any) => {
			return item.value;
		});
		const xData = res.result.item?.map((item: any) => {
			return item.Hour;
		});
		let seriesData: EmptyArrayType = [];
		// 将数据转换成柱状图数据
		for (let i of res.result.titleData) {
			seriesData.push({
				name: i.value,
				type: 'bar',
				stack: 'total',
				data: res.result.item?.map((item: any) => {
					return item[i.key];
				}),
			});
		}
		setOption(legendData, xData, seriesData);
		state.loading = false;
		gridOptions.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 option = ref<any>({});
// 信件来源统计
const setOption = (legendData: string[], xData: any, seriesData: any) => {
	option.value = {
		title: {
			text: '信件来源分时统计',
			left: 'center',
			top: '0',
		},
		tooltip: {
			trigger: 'axis',
			height: '50px',
			padding: [15, 15],
			enterable: true, //滚动条
			extraCssText: 'max-width:60%;max-height:83%; overflow: auto; ', //滚动条
			confine: true,
		},
		legend: {
			top: '5%',
			data: legendData,
		},
		grid: {
			left: '0',
			right: '1%',
			bottom: '0%',
			top: '25%',
			containLabel: true,
		},
		xAxis: {
			axisLabel: {
				rotate: 45, //设置的值大于0向右倾斜，小于0向左
				fontSize: 10,
			},
			data: xData,
		},
		yAxis: {
			type: 'value',
		},
		series: seriesData,
		animationEasing: 'elasticOut',
		animationDuration: 2000,
	};
};
// 查看列表明细
const router = useRouter();
const onDetail = () => {
	router.push({
		name: 'statisticsOrderDetailSourceTime',
	});
};
onMounted(() => {
	queryList();
});
</script>
<style lang="scss" scoped>
.chart {
	height: 70vh;
	margin-top: 10px;
}
</style>
