import React, { useState } from 'react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { Button } from '@/client/components/ui/button'; import { Input } from '@/client/components/ui/input'; import { Textarea } from '@/client/components/ui/textarea'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/client/components/ui/card'; import { Label } from '@/client/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/client/components/ui/select'; import { toast } from 'react-toastify'; import { publicConsultationRequestClient } from '@/client/api'; import type { InferRequestType } from 'hono/client'; // 表单验证Schema const ConsultationRequestFormSchema = z.object({ customerName: z.string().min(2, '姓名至少2个字符').max(255), companyName: z.string().max(255).optional(), phone: z.string().regex(/^1[3-9]\d{9}$/, '请输入正确的手机号'), email: z.string().email('请输入正确的邮箱地址').optional(), projectType: z.string().min(1, '请选择项目类型'), projectDescription: z.string().min(10, '项目描述至少10个字符').max(2000), budgetRange: z.string().max(100).optional(), timeline: z.string().max(100).optional(), isGuest: z.boolean().default(true) }); type ConsultationRequestFormData = z.infer; // 项目类型选项 const projectTypes = [ { value: '企业ERP系统', label: '企业ERP系统' }, { value: '智慧政务平台', label: '智慧政务平台' }, { value: '医疗信息化系统', label: '医疗信息化系统' }, { value: '教育信息化平台', label: '教育信息化平台' }, { value: '电商平台', label: '电商平台' }, { value: '移动应用开发', label: '移动应用开发' }, { value: '大数据分析平台', label: '大数据分析平台' }, { value: '人工智能应用', label: '人工智能应用' }, { value: '物联网系统', label: '物联网系统' }, { value: '其他', label: '其他' } ]; // 预算范围选项 const budgetRanges = [ { value: '10万以下', label: '10万以下' }, { value: '10-50万', label: '10-50万' }, { value: '50-100万', label: '50-100万' }, { value: '100-500万', label: '100-500万' }, { value: '500万以上', label: '500万以上' } ]; // 时间要求选项 const timelines = [ { value: '1-3个月', label: '1-3个月' }, { value: '3-6个月', label: '3-6个月' }, { value: '6-12个月', label: '6-12个月' }, { value: '12个月以上', label: '12个月以上' } ]; interface ConsultationRequestFormProps { onSuccess?: () => void; onCancel?: () => void; className?: string; } export default function ConsultationRequestForm({ onSuccess, onCancel, className = '' }: ConsultationRequestFormProps) { const [isSubmitting, setIsSubmitting] = useState(false); const { register, handleSubmit, formState: { errors }, setValue, watch } = useForm({ resolver: zodResolver(ConsultationRequestFormSchema), defaultValues: { isGuest: true } }); const onSubmit = async (data: ConsultationRequestFormData) => { setIsSubmitting(true); try { const response = await publicConsultationRequestClient.$post({ json: data }); if (response.status === 200) { const result = await response.json(); toast.success(result.message || '客户需求提交成功!'); onSuccess?.(); } else { const error = await response.json(); toast.error(error.message || '提交失败,请稍后重试'); } } catch (error) { console.error('提交客户需求失败:', error); toast.error('网络错误,请检查网络连接后重试'); } finally { setIsSubmitting(false); } }; return ( 项目咨询需求 请填写您的项目需求信息,我们将尽快与您联系并提供专业的咨询服务
{/* 基本信息 */}
{errors.customerName && (

{errors.customerName.message}

)}
{errors.phone && (

{errors.phone.message}

)}
{errors.email && (

{errors.email.message}

)}
{/* 项目信息 */}
{errors.projectType && (

{errors.projectType.message}

)}