import { defineStore } from "pinia"; import { ref } from "vue"; import { apiService } from "../services/api"; export const useTreasuryStore = defineStore("treasury", () => { const registers = ref([]); const isLoading = ref(false); const error = ref(null); const pagination = ref({ page: 0, rows: 50, totalRecords: 0, }); const totals = ref({ total_incasari: 0, total_plati: 0, }); const loadBankCashRegister = async (companyId, filters = {}) => { isLoading.value = true; error.value = null; try { const params = { company: companyId, page: pagination.value.page + 1, page_size: pagination.value.rows, ...filters, }; const response = await apiService.get("/treasury/bank-cash-register", { params, }); registers.value = response.data.registers || []; pagination.value.totalRecords = response.data.total_count || 0; totals.value = { total_incasari: response.data.total_incasari, total_plati: response.data.total_plati, }; return { success: true }; } catch (err) { error.value = err.response?.data?.detail || "Failed to load register"; console.error("Failed to load register:", err); return { success: false, error: error.value }; } finally { isLoading.value = false; } }; const setPagination = (newPagination) => { pagination.value = { ...pagination.value, ...newPagination }; }; const reset = () => { registers.value = []; isLoading.value = false; error.value = null; pagination.value = { page: 0, rows: 50, totalRecords: 0, }; }; return { registers, isLoading, error, pagination, totals, loadBankCashRegister, setPagination, reset, }; });