<script>
	import {
		ref,
	} from 'vue';
	import {
		listConvert
	} from '@/api/system/unitConverter/sysUnitConverter.js';
	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);
			// #ifdef APP || APP-PLUS
			uni.setStorageSync({
				key: 'unitData',
				data: JSON.stringify(unitDataGrouped),
				success: function() {
					console.log('缓存成功');
				}
			});
			// #endif
			// #ifdef H5
			localStorage.setItem('unitData', JSON.stringify(unitDataGrouped))
			// #endif

		} catch (error) {
			console.error('获取单位数据失败:', error);
		}
	};


	export default {
		onLaunch: function() {
			getList();
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},



	}
</script>

<style lang="scss">
	@import "uview-plus/index.scss";
	@import '@/static/scss/index.scss';
</style>