<template>
	<div v-loading="!settingsStore.inited" style="height: 100%;width: 100%;">
		<router-view />
	</div>
</template>

<script setup>
	import useSettingsStore from '@/store/modules/settings'
	import {
		handleThemeStyle
	} from '@/utils/theme'
	import {
		listConvert
	} from '@/api/system/convert.js';
	const settingsStore = useSettingsStore()

	const unitData = ref([]);
	const queryParams = ref({
		pageNum: 1,
		pageSize: 1000,
		unitType: null,
		unitName: null,
		baseUnit: null,
		conversionFactor: null,
		unitTypeName: null,
		status: null,
		unitOrder: null
	});
	// 保留原有单位换算方法
	const groupByUnitType = (data) => {
		return data.reduce((acc, unit) => {
			const type = unit.unitType;
			if (!acc[type]) acc[type] = [];
			acc[type].push({
				id: unit.id,
				unitType: unit.unitType,
				unitName: unit.unitName,
				conversionFactor: unit.conversionFactor,
				unitOrder: unit.unitOrder,
				baseUnit: unit.baseUnit,
				status: unit.status,
				unitTypeName: unit.unitTypeName
			});
			return acc;
		}, {});
	};
	const getList = async () => {
		try {
			const response = await listConvert(queryParams.value);
			const unitDataGrouped = groupByUnitType(response.rows);
			localStorage.setItem('unitData', JSON.stringify(unitDataGrouped));
		} catch (error) {
			console.error('获取单位数据失败:', error);
		}
	};

	onMounted(() => {
		getList();
		nextTick(() => {
			settingsStore.initSetting(() => {
				// 初始化主题样式
				handleThemeStyle(settingsStore.theme)
			})
		})
	})
</script>