"use client" import { useEffect, useState } from "react" import { useForm } from "react-hook-form" import { CheckCircle, AlertCircle } from "lucide-react" import Link from "next/link" import { FormInput } from "./form-input" import { FormButton } from "./form-button" import { Checkbox } from "@/components/ui/checkbox" import { Label } from "@/components/ui/label" import { WebhookClient } from "@/lib/webhook-client" interface RequestMachineFormData { firstName: string lastName: string email: string phone: string company: string employeeCount: string machineType: string machineCount: string message?: string marketingConsent: boolean termsAgreement: boolean source?: string page?: string confirmEmail?: string } interface SubmittedRequestMachineFormData extends RequestMachineFormData { timestamp: string url: string } interface RequestMachineFormProps { onSubmit?: (data: SubmittedRequestMachineFormData) => void className?: string } export function RequestMachineForm({ onSubmit, className }: RequestMachineFormProps) { const [isSubmitting, setIsSubmitting] = useState(false) const [isSubmitted, setIsSubmitted] = useState(false) const [submitError, setSubmitError] = useState(null) const { register, handleSubmit, formState: { errors }, reset, setValue, watch, } = useForm({ defaultValues: { source: "website", page: "", marketingConsent: false, termsAgreement: false, }, }) const watchedValues = watch() useEffect(() => { if (typeof window !== 'undefined') { setValue("page", window.location.pathname) } }, [setValue]) const onFormSubmit = async (data: RequestMachineFormData) => { setIsSubmitting(true) setSubmitError(null) try { // Add URL and timestamp const formData: SubmittedRequestMachineFormData = { ...data, timestamp: new Date().toISOString(), url: window.location.href, } // Call the webhook client directly const result = await WebhookClient.submitRequestMachineForm(formData) if (!result.success) { throw new Error(result.error || "Failed to submit form") } setIsSubmitted(true) reset() // Call custom onSubmit handler if provided if (onSubmit) { onSubmit(formData) } } catch (error) { console.error("Form submission error:", error) setSubmitError("Failed to submit form. Please try again.") } finally { setIsSubmitting(false) } } const onFormReset = () => { setIsSubmitted(false) setSubmitError(null) reset() } if (isSubmitted) { return (

Thank You!

We've received your request and will call you within 24 hours to discuss your vending machine needs.

) } return (
{/* Honeypot field for spam prevention */}
{/* Name Fields */}
{/* Contact Information */}
{ // Remove all non-digit characters const phoneDigits = value.replace(/\D/g, '') return phoneDigits.length >= 10 && phoneDigits.length <= 15 || "Please enter a valid phone number (10-15 digits)" }, })} />
{/* Company Information */} {/* Employee Count */} { const num = parseInt(value) return num >= 1 && num <= 10000 || "Please enter a valid number between 1 and 10,000" }, })} /> {/* Marketing Consent */}
setValue("marketingConsent", !!checked)} className="mt-0.5" />
{errors.marketingConsent && (

{errors.marketingConsent.message}

)} {/* Terms Agreement */}
setValue("termsAgreement", !!checked)} className="mt-0.5" />
{errors.termsAgreement && (

{errors.termsAgreement.message}

)} {/* Hidden form fields for validation */} {/* Machine Type */}
{ const currentTypes = watchedValues.machineType ? watchedValues.machineType.split(',').filter(Boolean) : [] const newTypes = checked ? [...currentTypes, 'snack'] : currentTypes.filter(type => type !== 'snack') setValue("machineType", newTypes.join(',')) }} />
{ const currentTypes = watchedValues.machineType ? watchedValues.machineType.split(',').filter(Boolean) : [] const newTypes = checked ? [...currentTypes, 'beverage'] : currentTypes.filter(type => type !== 'beverage') setValue("machineType", newTypes.join(',')) }} />
{ const currentTypes = watchedValues.machineType ? watchedValues.machineType.split(',').filter(Boolean) : [] const newTypes = checked ? [...currentTypes, 'combo'] : currentTypes.filter(type => type !== 'combo') setValue("machineType", newTypes.join(',')) }} />
{ const currentTypes = watchedValues.machineType ? watchedValues.machineType.split(',').filter(Boolean) : [] const newTypes = checked ? [...currentTypes, 'cold-food'] : currentTypes.filter(type => type !== 'cold-food') setValue("machineType", newTypes.join(',')) }} />
{ const currentTypes = watchedValues.machineType ? watchedValues.machineType.split(',').filter(Boolean) : [] const newTypes = checked ? [...currentTypes, 'hot-food'] : currentTypes.filter(type => type !== 'hot-food') setValue("machineType", newTypes.join(',')) }} />
{ const currentTypes = watchedValues.machineType ? watchedValues.machineType.split(',').filter(Boolean) : [] const newTypes = checked ? [...currentTypes, 'coffee'] : currentTypes.filter(type => type !== 'coffee') setValue("machineType", newTypes.join(',')) }} />
{ const currentTypes = watchedValues.machineType ? watchedValues.machineType.split(',').filter(Boolean) : [] const newTypes = checked ? [...currentTypes, 'micro-market'] : currentTypes.filter(type => type !== 'micro-market') setValue("machineType", newTypes.join(',')) }} />
{ const currentTypes = watchedValues.machineType ? watchedValues.machineType.split(',').filter(Boolean) : [] const newTypes = checked ? [...currentTypes, 'other'] : currentTypes.filter(type => type !== 'other') setValue("machineType", newTypes.join(',')) }} />
{errors.machineType && (

{errors.machineType.message}

)}
{/* Machine Count */} { const num = parseInt(value) return num >= 1 && num <= 100 || "Please enter a number between 1 and 100" }, })} /> {/* Optional Message */} {/* Submit Error */} {submitError && (
{submitError}
)} {/* Submit Button */} {isSubmitting ? "Submitting..." : "Get Your FREE Consultation"} ) }