import React, { useState, useEffect } from 'react'; import { Form, Input, Select, InputNumber, Button, message, Row, Col, Radio, Typography, Grid } from 'antd'; import 'react-image-crop/dist/ReactCrop.css'; import type { FormInstance } from 'antd'; import './CustomForm.css'; import KeyValueList from './KeyValueList.tsx'; import ImageInputGroup from './ImageInputGroup.tsx'; import { createCustom, updateCustom, type Custom } from '../apis'; const { TextArea } = Input; const { useBreakpoint } = Grid; interface CustomFormProps { initialData?: Partial; hideSubmitButton?: boolean; onFormReady?: (form: FormInstance) => void; onSuccess?: () => void; } const CustomForm: React.FC = ({ initialData, hideSubmitButton = false, onFormReady, onSuccess }) => { const [form] = Form.useForm(); const [loading, setLoading] = useState(false); useEffect(() => { if (initialData) { // 需要处理一些数据转换,例如 birth -> age (如果后端给的是 birth year) // 这里假设 initialData 里已经处理好了,或者先直接透传 const formData = { ...initialData }; // 如果有 birth,转换为 age 显示 if (formData.birth) { // 如果 birth 小于 100,假设直接存的年龄,直接显示 if (formData.birth < 100) { // @ts-expect-error: 临时给 form 设置 age 字段 formData.age = formData.birth; } else { // 否则假设是年份,计算年龄 const currentYear = new Date().getFullYear(); // @ts-expect-error: 临时给 form 设置 age 字段 formData.age = currentYear - formData.birth; } } // images 数组处理,确保是字符串数组 if (formData.images && Array.isArray(formData.images)) { if (formData.images.length === 0) { formData.images = ['']; } } else { formData.images = ['']; } form.setFieldsValue(formData); } else { // 初始化空状态 form.setFieldsValue({ images: [''] }); } }, [initialData, form]); useEffect(() => { onFormReady?.(form); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const onFinish = async (values: Custom & { age?: number }) => { setLoading(true); try { const currentYear = new Date().getFullYear(); const birth = currentYear - (values.age || 0); const customData: Custom = { name: values.name, gender: values.gender, birth: birth, phone: values.phone || undefined, email: values.email || undefined, height: values.height || undefined, weight: values.weight || undefined, images: values.images?.filter((url: string) => !!url) || [], scores: values.scores || undefined, degree: values.degree || undefined, academy: values.academy || undefined, occupation: values.occupation || undefined, income: values.income || undefined, assets: values.assets || undefined, current_assets: values.current_assets || undefined, house: values.house || undefined, car: values.car || undefined, registered_city: values.registered_city || undefined, live_city: values.live_city || undefined, native_place: values.native_place || undefined, original_family: values.original_family || undefined, is_single_child: values.is_single_child, match_requirement: values.match_requirement || undefined, introductions: values.introductions || {}, custom_level: values.custom_level || '普通', comments: values.comments || {}, }; console.log('提交客户数据:', customData); let response; if (initialData?.id) { response = await updateCustom(initialData.id, customData); } else { response = await createCustom(customData); } if (response.error_code === 0) { message.success(initialData?.id ? '客户信息已更新!' : '客户信息已成功提交到后端!'); if (!initialData?.id) { form.resetFields(); } onSuccess?.(); } else { message.error(response.error_info || '提交失败,请重试'); } } catch (e) { const err = e as { status?: number; message?: string }; if (err.status === 422) { message.error('表单数据格式有误,请检查输入内容'); } else if ((err.status ?? 0) >= 500) { message.error('服务器错误,请稍后重试'); } else { message.error(err.message || '提交失败,请重试'); } } finally { setLoading(false); } }; const screens = useBreakpoint(); const isMobile = !screens.md; const scoresNode = ( ); const heightNode = ( ); const weightNode = ( ); const degreeNode = ( ); const academyNode = ( ); return (
{!hideSubmitButton && ( <> 客户录入 录入客户基本信息 )}
{/* Row 1: 姓名、性别、年龄 */} {/* Row 3: Image Group */} {/* Row 4: Physical Info */} {isMobile ? ( // Mobile Layout
{/* 1. Scores (Full width) */} {scoresNode} {/* 2. Height & Weight */} {heightNode} {weightNode} {/* 3. Degree & Academy */} {degreeNode} {academyNode}
) : ( // PC Layout {scoresNode} {heightNode} {weightNode} )} {/* Row 5a: 学历、院校 */} {degreeNode} {academyNode} {/* Row 5b: 职业、收入 */} {/* Row 5c: 资产、流动资产 */} {/* Row 5d: 房产情况、汽车情况 */} ({ label: v, value: v }))} /> {/* Row 6: 户口城市、常住城市、籍贯 */} {/* Row 7: 是否独生子 */} {/* Row 8: 原生家庭 */}