import { defineStore } from 'pinia' import { ref } from 'vue' import * as api from '@/api/budget' import { getCurrentMonth } from '@/utils/format' import { useGroupStore } from '@/stores/group' // 重新导出类型以保持向后兼容 export type Budget = api.Budget export const useBudgetStore = defineStore('budget', () => { const budget = ref(null) const loading = ref(false) async function fetchBudget(month?: string) { loading.value = true try { const groupStore = useGroupStore() budget.value = await api.getBudget(month || getCurrentMonth(), groupStore.currentGroupId) } catch { budget.value = null } finally { loading.value = false } } async function setBudget(amount: number, month?: string) { await api.setBudget(amount, month || getCurrentMonth()) await fetchBudget(month) } return { budget, loading, fetchBudget, setBudget } })