<script lang="ts" setup>
|
import type { ProblemFormType } from '../data';
|
|
import type { AftersalesProblemApi } from '#/api/aftersales/problem';
|
|
import { computed, ref } from 'vue';
|
|
import { useVbenModal } from '@vben/common-ui';
|
import { Button, message } from 'ant-design-vue';
|
|
import { useVbenForm } from '#/adapter/form';
|
import {
|
createProblem,
|
getProblem,
|
updateProblem,
|
} from '#/api/aftersales/problem';
|
|
import { useProblemFormSchema } from '../data';
|
|
defineOptions({ name: 'AftersalesProblemForm' });
|
|
const emit = defineEmits(['success']);
|
|
const formType = ref<ProblemFormType>('create');
|
const id = ref<number>();
|
const submitting = ref(false);
|
|
const isEdit = computed(() => formType.value !== 'detail');
|
const title = computed(() => {
|
if (formType.value === 'detail') return '查看售后问题';
|
if (id.value) return '编辑售后问题';
|
return '新增售后问题';
|
});
|
|
// 主表单
|
const [Form, formApi] = useVbenForm({
|
commonConfig: { componentProps: { class: 'w-full' }, formItemClass: 'col-span-1', labelWidth: 100 },
|
layout: 'horizontal',
|
schema: [],
|
showDefaultActions: false,
|
wrapperClass: 'grid-cols-2',
|
});
|
|
/** 把问题对象映射成表单值(附件转为 FileUpload 结构) */
|
function problemToForm(p: AftersalesProblemApi.Problem): Record<string, unknown> {
|
const form: Record<string, unknown> = { ...p };
|
if (p.attachmentList?.length) {
|
form.blobIds = p.attachmentList.map((item) => ({
|
uid: String(item.id),
|
name: item.name || '',
|
url: item.url || '',
|
status: 'done',
|
id: item.id,
|
}));
|
}
|
return form;
|
}
|
|
/** 把表单里的附件值规整成 blobId 数组 */
|
function normalizeBlobIds(value: unknown): number[] {
|
const arr = Array.isArray(value) ? value : [];
|
return arr
|
.map((x) =>
|
typeof x === 'number'
|
? x
|
: x && typeof x === 'object' && 'id' in (x as Record<string, unknown>)
|
? Number((x as Record<string, unknown>).id)
|
: Number(x),
|
)
|
.filter((n) => Number.isFinite(n));
|
}
|
|
// Modal
|
const [Modal, modalApi] = useVbenModal({
|
async onOpenChange(isOpen: boolean) {
|
if (!isOpen) {
|
id.value = undefined;
|
return;
|
}
|
const data = modalApi.getData<{ formType: ProblemFormType; id?: number }>();
|
formType.value = data.formType;
|
id.value = data.id;
|
formApi.setState({ schema: useProblemFormSchema(formType.value) });
|
formApi.setDisabled(formType.value === 'detail');
|
if (data.id) {
|
modalApi.lock();
|
try {
|
const detail = await getProblem(data.id);
|
await formApi.setValues(problemToForm(detail));
|
} finally {
|
modalApi.unlock();
|
}
|
} else {
|
await formApi.resetForm();
|
}
|
},
|
});
|
|
async function handleSubmit() {
|
const { valid } = await formApi.validate();
|
if (!valid) {
|
return;
|
}
|
submitting.value = true;
|
try {
|
const data = (await formApi.getValues()) as Record<string, unknown>;
|
const blobIds = normalizeBlobIds(data.blobIds);
|
const payload = { ...data, blobIds } as AftersalesProblemApi.Problem;
|
if (id.value) {
|
await updateProblem({ ...payload, id: id.value });
|
} else {
|
await createProblem(payload);
|
}
|
message.success('操作成功');
|
await modalApi.close();
|
emit('success');
|
} catch (e: unknown) {
|
message.error((e as Error).message || '提交失败');
|
} finally {
|
submitting.value = false;
|
}
|
}
|
</script>
|
|
<template>
|
<Modal :title="title" class="w-2/3">
|
<Form class="mx-4" />
|
|
<template #footer>
|
<div class="flex justify-end gap-2">
|
<Button @click="modalApi.close()">取消</Button>
|
<Button v-if="isEdit" type="primary" :loading="submitting" @click="handleSubmit">
|
确认
|
</Button>
|
<Button v-else type="primary" @click="modalApi.close()">关闭</Button>
|
</div>
|
</template>
|
</Modal>
|
</template>
|