import { defineStore } from 'pinia' import { ref } from 'vue' import type { PageResult } from '@/types/api' import type { Bill, BillQuery, FinanceStats, Invoice, InvoiceQuery, InvoiceTitle, InvoiceTitlePayload, PayMethod, PayOrderResponse, Payment, PaymentResult, } from '@/types/business/finance' import { applyInvoice as applyInvoiceApi, createAlipayPageOrder as createAlipayPageOrderApi, createInvoiceTitle, createPayment as createPaymentApi, createWechatNativeOrder as createWechatNativeOrderApi, deleteInvoiceTitle, exportReport as exportReportApi, getBillDetail, getBillList, getCheckoutBills, getFinanceStats, getInvoiceList, getInvoiceTitles, getPlanPayResult, getPayResult, setDefaultTitle as setDefaultTitleApi, switchPaymentMethod as switchPaymentMethodApi, updateInvoiceTitle, } from '@/api/finance' function createDefaultBillQuery(): BillQuery { return { keyword: '', status: 'all', page: 1, pageSize: 6 } } function createDefaultInvoiceQuery(): InvoiceQuery { return { keyword: '', status: 'all', page: 1, pageSize: 6 } } export const useFinanceStore = defineStore('finance', () => { const stats = ref(null) const list = ref([]) const total = ref(0) const query = ref(createDefaultBillQuery()) const currentBill = ref(null) const payment = ref(null) const result = ref(null) const checkoutBills = ref([]) const checkoutLoading = ref(false) const payOrder = ref(null) const invoices = ref([]) const invoiceTotal = ref(0) const invoiceQuery = ref(createDefaultInvoiceQuery()) const titles = ref([]) async function fetchStats(period?: string): Promise { stats.value = await getFinanceStats(period) } async function fetchList(): Promise { const pageData: PageResult = await getBillList(query.value) list.value = pageData.list total.value = pageData.total } function setQuery(next: BillQuery): void { query.value = next } async function search(): Promise { query.value = { ...query.value, page: 1 } await fetchList() } async function resetQuery(): Promise { query.value = createDefaultBillQuery() await fetchList() } async function changePage(page: number): Promise { query.value = { ...query.value, page } await fetchList() } async function fetchDetail(id: string): Promise { currentBill.value = await getBillDetail(id) } async function createPayment(billId: string, method: PayMethod): Promise { payment.value = await createPaymentApi(billId, method) } async function switchPaymentMethod(billId: string, method: PayMethod): Promise { payment.value = await switchPaymentMethodApi(billId, method) } async function fetchResult(billId: string, requirementId = ''): Promise { result.value = requirementId ? await getPlanPayResult(requirementId, billId) : await getPayResult(billId) } async function loadCheckout(requirementId: string, ids: string[] = []): Promise { checkoutLoading.value = true try { checkoutBills.value = await getCheckoutBills(requirementId, ids) } finally { checkoutLoading.value = false } } async function payByWechatNative(orderNo: string, amount: number, billIds: string[]): Promise { payOrder.value = await createWechatNativeOrderApi(orderNo, amount, billIds) } async function payByAlipayPage(orderNo: string, amount: number, billIds: string[]): Promise { payOrder.value = await createAlipayPageOrderApi(orderNo, amount, billIds) } function clearPayOrder(): void { payOrder.value = null } async function fetchInvoices(): Promise { const pageData: PageResult = await getInvoiceList(invoiceQuery.value) invoices.value = pageData.list invoiceTotal.value = pageData.total } function setInvoiceQuery(next: InvoiceQuery): void { invoiceQuery.value = next } async function searchInvoices(): Promise { invoiceQuery.value = { ...invoiceQuery.value, page: 1 } await fetchInvoices() } async function changeInvoicePage(page: number): Promise { invoiceQuery.value = { ...invoiceQuery.value, page } await fetchInvoices() } async function applyInvoice(id: string): Promise { const updated = await applyInvoiceApi(id) const index = invoices.value.findIndex((item) => item.id === id) if (index >= 0) invoices.value[index] = updated } async function fetchTitles(): Promise { titles.value = await getInvoiceTitles() } async function createTitle(payload: InvoiceTitlePayload): Promise { await createInvoiceTitle(payload) await fetchTitles() } async function updateTitle(id: string, payload: InvoiceTitlePayload): Promise { await updateInvoiceTitle(id, payload) await fetchTitles() } async function deleteTitle(id: string): Promise { await deleteInvoiceTitle(id) await fetchTitles() } async function setDefaultTitle(id: string): Promise { titles.value = await setDefaultTitleApi(id) } async function exportReport(): Promise { await exportReportApi() } return { stats, list, total, query, currentBill, payment, result, checkoutBills, checkoutLoading, payOrder, invoices, invoiceTotal, invoiceQuery, titles, fetchStats, fetchList, setQuery, search, resetQuery, changePage, fetchDetail, createPayment, switchPaymentMethod, fetchResult, loadCheckout, payByWechatNative, payByAlipayPage, clearPayOrder, fetchInvoices, setInvoiceQuery, searchInvoices, changeInvoicePage, applyInvoice, fetchTitles, createTitle, updateTitle, deleteTitle, setDefaultTitle, export: exportReport, } })