<script lang="ts" setup>
|
import type { FormType } from '../data';
|
import type { FinanceReimbursementApi } from '#/api/finance/reimbursement';
|
|
import { computed, ref } from 'vue';
|
|
import { useVbenModal } from '@vben/common-ui';
|
import { $t } from '@vben/locales';
|
|
import { message } from 'ant-design-vue';
|
import dayjs from 'dayjs';
|
|
import { useVbenForm } from '#/adapter/form';
|
import {
|
createReimbursement,
|
getReimbursement,
|
updateReimbursement,
|
} from '#/api/finance/reimbursement';
|
|
import { toSubmitItems, useFormSchema } from '../data';
|
import ItemForm from './item-form.vue';
|
|
const emit = defineEmits(['success']);
|
|
const formData = ref<FinanceReimbursementApi.Reimbursement>({
|
title: '',
|
totalAmount: 0,
|
applyDate: '',
|
remark: '',
|
items: [],
|
});
|
|
const formType = ref<FormType>('create'); // 表单类型:'create' | 'edit' | 'detail'
|
const itemFormRef = ref<InstanceType<typeof ItemForm>>();
|
|
/**
|
* 明细表格的回显数据源:只在打开弹窗时赋值(编辑回显 / 新增置空)。
|
* 用户在表格里的增删改只通过 emit 单向流到表单值,绝不回写到这里,
|
* 否则 prop 变化会让表格反复重载(且与子组件 watcher 形成死循环)。
|
*/
|
const initialItems = ref<FinanceReimbursementApi.ReimbursementItem[]>([]);
|
|
const getTitle = computed(() => {
|
if (formType.value === 'create') {
|
return $t('ui.actionTitle.create', ['报销单']);
|
} else if (formType.value === 'edit') {
|
return $t('ui.actionTitle.edit', ['报销单']);
|
} else {
|
return '报销单详情';
|
}
|
});
|
|
const [Form, formApi] = useVbenForm({
|
commonConfig: {
|
componentProps: {
|
class: 'w-full',
|
},
|
labelWidth: 120,
|
},
|
wrapperClass: 'grid-cols-2',
|
layout: 'vertical',
|
schema: useFormSchema(formType.value),
|
showDefaultActions: false,
|
});
|
|
/** 同步明细行到表单值,保证随主表一次性提交 */
|
function handleUpdateItems(items: FinanceReimbursementApi.ReimbursementItem[]) {
|
// 第二个参数为 false:跳过合并算法,否则数组类型的值会被过滤掉
|
formApi.setValues({ items }, false);
|
}
|
|
/** 明细金额变化时回填主表报销总金额(用户仍可手动修改) */
|
function handleUpdateTotalAmount(totalAmount: number) {
|
formApi.setValues({ totalAmount }, false);
|
}
|
|
/** 创建或更新报销单 */
|
const [Modal, modalApi] = useVbenModal({
|
async onConfirm() {
|
const { valid } = await formApi.validate();
|
if (!valid) {
|
return;
|
}
|
const itemFormInstance = Array.isArray(itemFormRef.value)
|
? itemFormRef.value[0]
|
: itemFormRef.value;
|
try {
|
itemFormInstance?.validate();
|
} catch (error: any) {
|
message.error(error.message || '报销明细验证失败');
|
return;
|
}
|
|
modalApi.lock();
|
const data =
|
(await formApi.getValues()) as FinanceReimbursementApi.Reimbursement;
|
// 发票附件为全量替换语义:清空全部发票时必须显式传空数组,
|
// 传 null 后端会视为「不动附件」,导致删除操作不生效
|
data.blobIds = data.blobIds ?? [];
|
try {
|
await (formType.value === 'create'
|
? createReimbursement(data)
|
: updateReimbursement(data));
|
await modalApi.close();
|
emit('success');
|
message.success($t('ui.actionMessage.operationSuccess'));
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
async onOpenChange(isOpen: boolean) {
|
if (!isOpen) {
|
formData.value = {} as FinanceReimbursementApi.Reimbursement;
|
initialItems.value = [];
|
return;
|
}
|
const data = modalApi.getData<{ formType: FormType; id?: number }>();
|
formType.value = data.formType;
|
formApi.setDisabled(formType.value === 'detail');
|
formApi.updateSchema(useFormSchema(formType.value));
|
if (!data || !data.id) {
|
initialItems.value = [];
|
// 新建时默认申请日期为今天
|
await formApi.setValues(
|
{ applyDate: dayjs().format('YYYY-MM-DD') },
|
false,
|
);
|
return;
|
}
|
modalApi.lock();
|
try {
|
formData.value = await getReimbursement(data.id);
|
// 明细表格的行 key 用数据库 id,故回显数据源保留原 id
|
initialItems.value = formData.value.items ?? [];
|
// 已有发票转成上传组件可识别的文件对象,提交时按 valueKey=id 提取为 blobId
|
const blobIds = (formData.value.attachmentList ?? []).map((item) => ({
|
uid: String(item.id),
|
name: item.name || '',
|
url: item.url || '',
|
status: 'done',
|
id: item.id,
|
}));
|
await formApi.setValues(
|
{
|
...formData.value,
|
// 表单里的 items 只用于提交,必须剥掉行 id(后端「先删后增」会与软删行主键冲突)
|
items: toSubmitItems(formData.value.items),
|
blobIds,
|
},
|
false,
|
);
|
} finally {
|
modalApi.unlock();
|
}
|
},
|
});
|
</script>
|
|
<template>
|
<Modal
|
:title="getTitle"
|
class="w-3/4"
|
:close-on-click-modal="false"
|
:show-confirm-button="formType !== 'detail'"
|
>
|
<Form class="mx-3">
|
<template #items>
|
<ItemForm
|
ref="itemFormRef"
|
:items="initialItems"
|
:disabled="formType === 'detail'"
|
class="w-full"
|
@update:items="handleUpdateItems"
|
@update:total-amount="handleUpdateTotalAmount"
|
/>
|
</template>
|
</Form>
|
</Modal>
|
</template>
|