financial-web/src/views/financial-user/account-bill/bill/utils/hooks.ts

199 lines
5.1 KiB
TypeScript

import { addDialog, closeDialog } from '@/components/BaseDialog/index';
import BillDialog from '@/views/financial-user/account-bill/bill/bill-dialog.vue';
import { useBillUserStore } from '@/store/financialUser/billUser';
import { h, ref } from 'vue';
import { message, messageBox } from '@/utils/message';
import type { FormItemProps } from '@/views/financial-user/account-bill/bill/utils/types';
import { $t } from '@/plugins/i18n';
import DeleteBatchDialog from '@/components/Table/DeleteBatchDialog.vue';
import dayjs from 'dayjs';
import ExportBill from '@/views/financial-user/account-bill/bill/export-bill.vue';
import { currentMouth } from '@/enums/dateEnums';
export const formRef = ref();
// 删除ids
export const deleteIds = ref([]);
const billStore = useBillUserStore();
/** 搜索初始化账单信息 */
export async function onSearch() {
billStore.loading = true;
await billStore.getBillList();
billStore.loading = false;
}
/** 添加账单信息 */
export function onAdd() {
addDialog({
title: `${$t('addNew')}${$t('bill')}`,
width: '30%',
props: {
formInline: {
type: -1,
amount: undefined,
description: undefined,
transactionDate: dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss'),
categoryId: undefined,
},
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(BillDialog, { ref: formRef }),
footerButtons: [
{
label: $t('cancel'),
text: true,
bg: true,
btnClick: async ({ dialog: { options, index } }) => {
closeDialog(options, index);
await onSearch();
},
},
{
label: $t('buttons.pureConfirm'),
type: 'primary',
text: true,
bg: true,
btnClick: ({ dialog: { options, index } }) => {
const form = options.props.formInline as FormItemProps;
formRef.value.formRef.validate(async (valid: any) => {
if (!valid) return;
const result = await billStore.addBill(form);
if (!result) return;
// 添加成功
formRef.value.formRef.resetFields();
closeDialog(options, index);
await onSearch();
});
},
},
{
label: $t('continue_adding'),
type: 'success',
text: true,
bg: true,
btnClick: ({ dialog: { options } }) => {
const form = options.props.formInline as FormItemProps;
formRef.value.formRef.validate(async (valid: any) => {
if (!valid) return;
const result = await billStore.addBill(form);
if (!result) return;
// 添加成功
options.props.formInline.amount = undefined;
await onSearch();
});
},
},
],
});
}
/** 更新账单信息 */
export function onUpdate(row: any) {
addDialog({
title: `${$t('modify')}${$t('bill')}`,
width: '30%',
props: {
formInline: {
type: row.type,
amount: row.amount,
description: row.description,
transactionDate: row.transactionDate,
categoryId: row.categoryId,
},
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(BillDialog, { ref: formRef }),
beforeSure: (done, { options }) => {
const form = options.props.formInline as FormItemProps;
formRef.value.formRef.validate(async (valid: any) => {
if (!valid) return;
const result = await billStore.updateBill({ ...form, id: row.id });
if (!result) return;
done();
await onSearch();
});
},
});
}
/** 删除账单信息 */
export const onDelete = async (row: any) => {
const id = row.id;
// 是否确认删除
const result = await messageBox({
title: $t('confirmDelete'),
showMessage: false,
confirmMessage: undefined,
cancelMessage: $t('cancel_delete'),
});
if (!result) return;
// 删除数据
await billStore.deleteBill([id]);
await onSearch();
};
/** 批量删除 */
export const onDeleteBatch = async () => {
const ids = deleteIds.value;
const formDeletedBatchRef = ref();
addDialog({
title: $t('deleteBatchTip'),
width: '30%',
props: { formInline: { confirmText: '' } },
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(DeleteBatchDialog, { ref: formDeletedBatchRef }),
beforeSure: (done, { options }) => {
formDeletedBatchRef.value.formDeletedBatchRef.validate(async (valid: any) => {
if (!valid) return;
const text = options.props.formInline.confirmText.toLowerCase();
if (text === 'yes' || text === 'y') {
// 删除数据
await billStore.deleteBill(ids);
await onSearch();
done();
} else message($t('deleteBatchTip'), { type: 'warning' });
});
},
});
};
/** 导出账单 */
export const onExportBill = () => {
const formExportBillRef = ref();
addDialog({
title: $t('exportBill'),
width: '30%',
props: { formInline: { transactionDate: currentMouth } },
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(ExportBill, { ref: formExportBillRef }),
beforeSure: (done, { options }) => {
formExportBillRef.value.formRef.validate(async (valid: any) => {
if (!valid) return;
const result = billStore.exportBill(options.props.formInline.transactionDate);
if (!result) return;
done();
});
},
});
};