SunshineFormModal.tsx
4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { UForm, UModal, useUMessage } from '@uj/ui';
import { CombiForm, type FieldSchema } from '@/components';
import { useAppStore } from '@/store/useAppStore';
import { dashboardApi, operationApi, type StoreItem, type Sunshine } from '@/api';
import { errorDetail } from '@/utils/error';
interface Props {
record: Sunshine | null;
onClose: () => void;
onSaved: () => void;
}
type SunshineFormValues = {
sellerMerchantId: string;
carType: string;
sunlightInspiresPrice: string;
};
/**
* 源车辆类型字典 constants.js vehicleCategoryOpt:value 存 confId,展示 confValue。
* 与源 add.vue 的 carType 下拉一致:提交 id、列表按字典翻译。
*/
const CAR_TYPES = [
{ value: '1', label: '置换车' },
{ value: '2', label: '报废车' },
];
/** 新增 / 修改激励字段及校验的唯一配置位置。 */
function buildFields(stores: StoreItem[], editing: boolean): FieldSchema<SunshineFormValues>[] {
return [
{
name: 'sellerMerchantId',
label: '4S门店',
type: 'select',
options: stores.map((s) => ({ value: s.merchantId, label: s.merchantName })),
// 源 add.vue:编辑时门店不可改(isDisabled: paramsObj.id)。
selectProps: { showSearch: true, optionFilterProp: 'label', disabled: editing },
rules: [{ required: true, message: '请选择4S门店' }],
},
{
name: 'carType',
label: '车辆类型',
type: 'select',
options: CAR_TYPES,
rules: [{ required: true, message: '请选择车辆类型' }],
},
{
name: 'sunlightInspiresPrice',
label: '激励价格(元)',
type: 'input',
inputProps: { inputMode: 'decimal', maxLength: 10 },
rules: [
{ required: true, message: '请输入激励价格' },
{ pattern: /^\d+(\.\d{1,2})?$/, message: '请输入最多两位小数的正数' },
],
},
];
}
/** 新增与修改共用的独立表单弹窗。 */
export default function SunshineFormModal({ record, onClose, onSaved }: Props) {
const [form] = UForm.useForm<SunshineFormValues>();
const message = useUMessage();
const { currentUser } = useAppStore();
const [saving, setSaving] = useState(false);
const editing = Boolean(record?.id);
// 4S 门店下拉:源 add.vue 用 getServiceStore 的 storeIdList(merchantId/merchantName)。
const stores = useQuery({
queryKey: ['usedcar', 'service-store', 7],
queryFn: () => dashboardApi.getServiceStore({ scene: 7 }),
});
useEffect(() => {
if (!record) return;
form.setFieldsValue({
sellerMerchantId: record.sellerMerchantId ?? '',
carType: record.carType ?? '',
sunlightInspiresPrice:
record.sunlightInspiresPrice === undefined ? '' : String(record.sunlightInspiresPrice),
});
}, [form, record]);
const save = async (values: SunshineFormValues) => {
const store = (stores.data ?? []).find((s) => s.merchantId === values.sellerMerchantId);
const payload: Record<string, unknown> = {
sellerMerchantId: values.sellerMerchantId,
sellerMerchantName: store?.merchantName ?? record?.sellerMerchantName ?? '',
carType: values.carType,
sunlightInspiresPrice: Number(values.sunlightInspiresPrice),
};
// 源 add.vue:229/231:新增带 createdBy、编辑带 updatedBy(取当前登录用户名)。
if (editing) payload.updatedBy = currentUser.name;
else payload.createdBy = currentUser.name;
setSaving(true);
try {
if (editing) await operationApi.updateSunshine({ ...payload, id: record!.id });
else await operationApi.createSunshine(payload);
message.success('操作成功');
onSaved();
onClose();
} catch (error) {
message.error(errorDetail(error));
} finally {
setSaving(false);
}
};
return (
<UModal
open={Boolean(record)}
title={editing ? '修改激励' : '新增激励'}
confirmLoading={saving}
onCancel={onClose}
onOk={() => form.submit()}
>
<CombiForm
mode="form"
form={form}
fields={buildFields(stores.data ?? [], editing)}
layout="vertical"
onSubmit={(values) => void save(values)}
/>
</UModal>
);
}