StoreConfigModal.tsx
3.75 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
import { useEffect, useMemo, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { UForm, UModal, useUMessage } from '@uj/ui';
import { CombiForm, type FieldSchema } from '@/components';
import { liveCodeApi, storeConfigApi } from '@/api';
import { errorDetail } from '@/utils/error';
export type StoreConfigRow = Record<string, unknown> & { id?: string | number };
type FormValues = { storeId: string; pricingSpecialistId: string };
const text = (value: unknown) =>
value === undefined || value === null || value === '' ? '-' : String(value);
/** 新增/编辑4S店配置共用弹窗。字段、选项和保存逻辑集中维护。 */
export default function StoreConfigModal({
record,
onClose,
onSaved,
}: {
record: StoreConfigRow | null;
onClose: () => void;
onSaved: () => void;
}) {
const [form] = UForm.useForm<FormValues>();
const message = useUMessage();
const [saving, setSaving] = useState(false);
const stores = useQuery({
queryKey: ['live-code-car-dealers'],
queryFn: () => liveCodeApi.getCarDealerList<StoreConfigRow[]>(),
});
const staff = useQuery({
queryKey: ['live-code-pricing-staff'],
queryFn: () => storeConfigApi.getPricingStaffList<StoreConfigRow[]>(),
});
useEffect(() => {
if (record)
form.setFieldsValue({
storeId: text(record.storeId),
pricingSpecialistId: text(record.pricingSpecialistId),
});
else form.resetFields();
}, [form, record]);
const fields: FieldSchema<FormValues>[] = useMemo(
() => [
{
name: 'storeId',
label: '4S店',
type: 'select',
options: (stores.data ?? []).map((item) => ({
value: text(item.mainMerchantId),
label: text(item.mainMerchantName),
})),
selectProps: { showSearch: true, optionFilterProp: 'label', disabled: Boolean(record?.id) },
rules: [{ required: true, message: '请选择4S店' }],
},
{
name: 'pricingSpecialistId',
label: '总部定价师',
type: 'select',
options: (staff.data ?? []).map((item) => ({
value: text(item.staffId ?? item.id),
label: `${text(item.staffName)}(${text(item.staffPhone)})`,
})),
selectProps: { showSearch: true, optionFilterProp: 'label' },
rules: [{ required: true, message: '请选择总部定价师' }],
},
],
[record, staff.data, stores.data],
);
const save = async (values: FormValues) => {
const store = (stores.data ?? []).find((item) => text(item.mainMerchantId) === values.storeId);
const person = (staff.data ?? []).find(
(item) => text(item.staffId ?? item.id) === values.pricingSpecialistId,
);
setSaving(true);
try {
const payload = {
...values,
storeName: text(store?.mainMerchantName),
pricingSpecialistName: text(person?.staffName),
staffPhone: text(person?.staffPhone),
};
if (record?.id) await storeConfigApi.updateStoreConfig({ ...payload, id: record.id });
else await storeConfigApi.addStoreConfig(payload);
message.success('保存成功');
onSaved();
onClose();
} catch (error) {
message.error(errorDetail(error));
} finally {
setSaving(false);
}
};
return (
<UModal
open={Boolean(record)}
title={record?.id ? '修改配置' : '新增配置'}
confirmLoading={saving}
onCancel={onClose}
onOk={() => form.submit()}
>
<p className="uj-page-tip">此配置用于决定该4S店收取的线索默认分配给哪位总部定价师。</p>
<CombiForm
mode="form"
form={form}
layout="horizontal"
labelWidth={120}
fields={fields}
onSubmit={(values) => void save(values)}
/>
</UModal>
);
}