import { addDialog } from '@/components/BaseDialog/index'; import SavingGoalDialog from '@/views/financial/budget-saving/saving-goal/saving-goal-dialog.vue'; import { useSavingGoalStore } from '@/store/financial/savingGoal'; import { h, ref } from 'vue'; import { message, messageBox } from '@/utils/message'; import type { FormItemProps } from '@/views/financial/budget-saving/saving-goal/utils/types'; import { $t } from '@/plugins/i18n'; import DeleteBatchDialog from '@/components/Table/DeleteBatchDialog.vue'; export const formRef = ref(); // 删除ids export const deleteIds = ref([]); const savingGoalStore = useSavingGoalStore(); /** 搜索初始化用户储值 */ export async function onSearch() { savingGoalStore.loading = true; await savingGoalStore.getSavingGoalList(); savingGoalStore.loading = false; } /** 添加用户储值 */ export function onAdd() { addDialog({ title: `${$t('addNew')}${$t('savingGoal')}`, width: '30%', props: { formInline: { userId: undefined, statusType: undefined, savingGoalName: undefined, amount: undefined, duration: undefined, }, }, draggable: true, fullscreenIcon: true, closeOnClickModal: false, contentRenderer: () => h(SavingGoalDialog, { ref: formRef }), beforeSure: (done, { options }) => { const form = options.props.formInline as FormItemProps; formRef.value.formRef.validate(async (valid: any) => { if (!valid) return; // 格式化开始时间和结束时间 form.startDuration = form.duration[0]; form.endDuration = form.duration[1]; const result = await savingGoalStore.addSavingGoal(form); if (!result) return; done(); await onSearch(); }); }, }); } /** 更新用户储值 */ export function onUpdate(row: any) { addDialog({ title: `${$t('modify')}${$t('savingGoal')}`, width: '30%', props: { formInline: { userId: row.userId, statusType: row.statusType, savingGoalName: row.savingGoalName, amount: row.amount, duration: [row.startDuration, row.endDuration], }, }, draggable: true, fullscreenIcon: true, closeOnClickModal: false, contentRenderer: () => h(SavingGoalDialog, { ref: formRef }), beforeSure: (done, { options }) => { const form = options.props.formInline as FormItemProps; formRef.value.formRef.validate(async (valid: any) => { if (!valid) return; // 格式化开始时间和结束时间 form.startDuration = form.duration[0]; form.endDuration = form.duration[1]; const result = await savingGoalStore.updateSavingGoal({ ...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 savingGoalStore.deleteSavingGoal([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 savingGoalStore.deleteSavingGoal(ids); await onSearch(); done(); } else message($t('deleteBatchTip'), { type: 'warning' }); }); }, }); };