financial-web/src/views/financial/debt-repayment-plan/utils/hooks.ts

135 lines
3.8 KiB
TypeScript

import { addDialog } from '@/components/BaseDialog/index';
import DebtRepaymentPlanDialog from '@/views/financial/debt-repayment-plan/debt-repayment-plan-dialog.vue';
import { useDebtRepaymentPlanStore } from '@/store/financial/debtRepaymentPlan';
import { h, ref } from 'vue';
import { message, messageBox } from '@/utils/message';
import type { FormItemProps } from '@/views/financial/debt-repayment-plan/utils/types';
import { $t } from '@/plugins/i18n';
import DeleteBatchDialog from '@/components/Table/DeleteBatchDialog.vue';
export const formRef = ref();
// 删除ids
export const deleteIds = ref([]);
const debtRepaymentPlanStore = useDebtRepaymentPlanStore();
/** 搜索初始化债务还款计划表 */
export async function onSearch() {
debtRepaymentPlanStore.loading = true;
await debtRepaymentPlanStore.getDebtRepaymentPlanList();
debtRepaymentPlanStore.loading = false;
}
/** 添加债务还款计划表 */
export function onAdd() {
addDialog({
title: `${$t('addNew')}${$t('debt-repayment-plan')}`,
width: '30%',
props: {
formInline: {
debtId: undefined,
installmentNumber: undefined,
installmentAmount: undefined,
dueDate: undefined,
paidAmount: undefined,
paymentStatus: undefined,
},
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(DebtRepaymentPlanDialog, { 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 debtRepaymentPlanStore.addDebtRepaymentPlan(form);
if (!result) return;
done();
await onSearch();
});
},
});
}
/** 更新债务还款计划表 */
export function onUpdate(row: any) {
addDialog({
title: `${$t('modify')}${$t('debt-repayment-plan')}`,
width: '30%',
props: {
formInline: {
debtId: row.debtId,
installmentNumber: row.installmentNumber,
installmentAmount: row.installmentAmount,
dueDate: row.dueDate,
paidAmount: row.paidAmount,
paymentStatus: row.paymentStatus,
},
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(DebtRepaymentPlanDialog, { 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 debtRepaymentPlanStore.updateDebtRepaymentPlan({ ...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 debtRepaymentPlanStore.deleteDebtRepaymentPlan([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 debtRepaymentPlanStore.deleteDebtRepaymentPlan(ids);
await onSearch();
done();
} else message($t('deleteBatchTip'), { type: 'warning' });
});
},
});
};