import { defineStore } from 'pinia' import { ref, computed } from 'vue' import { api } from '../composables/useApi' export const usePlansStore = defineStore('plans', () => { const plans = ref([]) const teachers = ref([]) const shoppingList = ref([]) const activePlan = computed(() => plans.value.find(p => p.status === 'active')) const pendingPlans = computed(() => plans.value.filter(p => p.status === 'pending')) async function loadPlans() { const res = await api('/api/oil-plans') if (res.ok) plans.value = await res.json() } async function loadTeachers() { const res = await api('/api/teachers') if (res.ok) teachers.value = await res.json() } async function createPlan(healthDesc, teacherId) { const res = await api('/api/oil-plans', { method: 'POST', body: JSON.stringify({ health_desc: healthDesc, teacher_id: teacherId }), }) if (!res.ok) throw new Error('创建失败') await loadPlans() return res.json() } async function updatePlan(planId, data) { await api(`/api/oil-plans/${planId}`, { method: 'PUT', body: JSON.stringify(data), }) await loadPlans() } async function addRecipe(planId, recipeName, ingredients, timesPerMonth) { await api(`/api/oil-plans/${planId}/recipes`, { method: 'POST', body: JSON.stringify({ recipe_name: recipeName, ingredients, times_per_month: timesPerMonth }), }) await loadPlans() } async function removeRecipe(planId, recipeId) { await api(`/api/oil-plans/${planId}/recipes/${recipeId}`, { method: 'DELETE' }) await loadPlans() } async function loadShoppingList(planId) { const res = await api(`/api/oil-plans/${planId}/shopping-list`) if (res.ok) shoppingList.value = await res.json() } return { plans, teachers, shoppingList, activePlan, pendingPlans, loadPlans, loadTeachers, createPlan, updatePlan, addRecipe, removeRecipe, loadShoppingList, } })