import { defineStore } from 'pinia'; import { computed, ref } from 'vue'; import { ScenarioService } from '../service/ScenarioService'; import { LoadingStore } from './LoadingStore'; export const ScenarioExecutionStore = defineStore('scenario_execution_store', () => { const lstScenarioExecution = ref([]); const selectedExecScenario = ref(null); const loadingStore = LoadingStore(); const totalRecords = ref(0); // Numero totale di record const currentPage = ref(0); // Pagina corrente const pageSize = ref(10); // Numero di record per pagina const filters = ref({}); // Oggetto per memorizzare i filtri // Funzione per recuperare le esecuzioni con filtri e paginazione async function fetchScenariosExecution(page = 0, size = 10, filters, sortField, sortOrder) { loadingStore.scenario_loading = true; try { console.log("Fetching executions with filters:", page, size, filters, sortField, sortOrder); const resp = await ScenarioService.getExecScenariosByUser(page, size, filters, sortField, sortOrder); // Passiamo anche i filtri lstScenarioExecution.value = resp.data.content; totalRecords.value = resp.data.totalElements; currentPage.value = page; pageSize.value = size; } catch (error) { console.error("Error fetching executions:", error); } finally { loadingStore.scenario_loading = false; } } const scenariosExecution = computed(() => { return lstScenarioExecution.value; }); const getSelectedExecScenario = computed(() => { return selectedExecScenario.value; }); const getCurrentPage = computed(() => { return currentPage.value; }); const getPageSize = computed(() => { return pageSize.value; }); const getTotalRecords = computed(() => { return totalRecords.value; }); const setSelectedExecScenario = (execScenario) => { selectedExecScenario.value = execScenario; }; // Funzione per aggiornare i filtri const updateFilters = (newFilters) => { console.log("New filters:", newFilters); filters.value = newFilters; filters.value = encodeURIComponent(JSON.stringify(newFilters)); fetchScenariosExecution(currentPage.value, pageSize.value, filters.value); // Ricarica con i nuovi filtri }; return { getCurrentPage, getPageSize, getTotalRecords, fetchScenariosExecution, selectedExecScenario, lstScenarioExecution, scenariosExecution, getSelectedExecScenario, setSelectedExecScenario, updateFilters, // Aggiunto per aggiornare i filtri filters // Rende disponibile i filtri come parte dello store }; });