<template>
	<div class="snapshot-statistics-hotspot-container layout-pd">
		<el-card shadow="never">
			<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 label="来电主体" prop="TypeId">
					<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>
			<el-row :gutter="20">
				<el-col :xs="24" :sm="24" :md="18" :lg="18" :xl="18">
					<v-chart class="chart" :option="option" :loading="state.loading" autoresize />
				</el-col>
				<el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
					<div style="height: 70vh">
						<vxe-grid v-bind="gridOptions" ref="gridRef"> </vxe-grid>
					</div>
				</el-col>
			</el-row>
		</el-card>
	</div>
</template>
<script setup lang="tsx" name="snapshotStatisticsHotspot">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { FormInstance } from 'element-plus';
import { defaultDate } from '@/utils/constants';
import Other from '@/utils/other';
import { getSSPStatisticsHotSpotType, getSSPStatisticsHotSpotTypeExport } from '@/api/snapshot/statistics';

const StatisticalTime = defineAsyncComponent(() => import('@/components/StatisticalTime/index.vue')); // 日期类型选择组件
// 定义变量内容
const ruleFormRef = ref<RefType>(); // 表单ref
const state = reactive<any>({
	queryParams: {
		// 查询条件
		crTime: defaultDate, //
		StartTime: null,
		EndTime: null,
		TypeId: 0, // 0 全部 1市民 2企业
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
});
/** 搜索按钮操作 */
const handleQuery = () => {
	queryList();
};

const requestParams = ref<EmptyObjectType>({});
const gridOptions = reactive<any>({
	loading: false,
	border: true,
	showOverflow: true,
	columnConfig: {
		resizable: true,
	},
	scrollY: {
		enabled: true,
		gt: 100,
	},
	toolbarConfig: {
		custom: true,
		refresh: {
			queryMethod: () => {
				handleQuery();
			},
		},
		tools: [{ toolRender: { name: 'exportAll' } }],
	},
	customConfig: {
		storage: true,
	},
	id: 'snapshotStatisticsHotspot',
	rowConfig: { isHover: true, height: 30, isCurrent: true, useKey: true },
	height: 'auto',
	align: 'center',
	columns: [
		{ field: 'name', title: '热点类型' },
		{ field: 'orderCount', title: '数量' },
	],
	data: [],
	params: {
		exportMethod: getSSPStatisticsHotSpotTypeExport,
		exportParams: requestParams,
	},
});
/** 获取列表 */
const dataTable = ref([]);
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 { result } = await getSSPStatisticsHotSpotType(requestParams.value);
		state.tableData = result ?? [];
		gridOptions.data = result ?? [];
		const legendData = state.tableData.map((item: any) => {
			return item.name;
		});
		dataTable.value = state.tableData.map((item: any) => {
			return {
				name: item.name,
				value: item.orderCount,
				...item,
			};
		});
		setOption(legendData, dataTable.value);
		state.loading = false;
		gridOptions.loading = false;
	} catch (e) {
		console.log(e);
		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 option = ref<EmptyObjectType>({});
// 热点类型统计表
const setOption = (legendData: string[], data: any) => {
	option.value = {
		title: {
			text: '热点类型统计表',
			left: 'center',
		},
		tooltip: {
			formatter: '{b0}: {c0} ({d}%)',
		},
		legend: [
			{
				left: 'left',
				top: '40',
				orient: 'vertical',
				data: legendData,
			},
		],
		series: [
			{
				type: 'pie',
				radius: ['0%', '60%'],
				top: '20%',
				label: {
					show: true,
					overflow: 'none',
					formatter: (params: any) => {
						if (params.name !== '') {
							return `${params.name}:${params.data.value}（${params.percent}%）`;
						}
					},
				},
				data: data,
			},
		],
	};
};
onMounted(() => {
	queryList();
});
</script>
<style lang="scss" scoped>
.chart {
	height: 70vh;
	margin-top: 10px;
}
</style>
