'use client' import { useState } from 'react' import { zodResolver } from '@hookform/resolvers/zod' import { useForm } from 'react-hook-form' import * as z from 'zod' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form' import { Input } from '@/components/ui/input' import { Textarea } from '@/components/ui/textarea' import { Button } from '@/components/ui/button' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Checkbox } from '@/components/ui/checkbox' import { toast } from 'sonner' import { Loader2, Package, Building, Phone, Mail, MessageCircle } from 'lucide-react' const inquirySchema = z.object({ // Company Information companyName: z.string().min(2, 'Company name must be at least 2 characters'), contactPerson: z.string().min(2, 'Contact person name must be at least 2 characters'), designation: z.string().min(2, 'Designation is required'), email: z.string().email('Please enter a valid email address'), phone: z.string().min(10, 'Please enter a valid phone number'), // Business Details businessType: z.string().min(1, 'Please select business type'), gstNumber: z.string().optional(), address: z.string().min(10, 'Please provide complete address'), // Product Requirements quantityRequired: z.string().min(1, 'Quantity is required'), quantityUnit: z.string().default('tons'), deliveryLocation: z.string().min(2, 'Delivery location is required'), expectedDeliveryDate: z.string().optional(), // Additional Information message: z.string().min(10, 'Please provide detailed requirements (minimum 10 characters)'), hearAboutUs: z.string().optional(), // Terms agreedToTerms: z.boolean().refine(val => val === true, { message: 'You must agree to the terms and conditions' }) }) type InquiryFormData = z.infer interface B2BInquiryFormProps { isOpen: boolean onOpenChange: (open: boolean) => void product?: { id: string name: string category: { name: string } price: number weight?: string } } export default function B2BInquiryForm({ isOpen, onOpenChange, product }: B2BInquiryFormProps) { const [isSubmitting, setIsSubmitting] = useState(false) const form = useForm({ resolver: zodResolver(inquirySchema), defaultValues: { companyName: '', contactPerson: '', designation: '', email: '', phone: '', businessType: '', gstNumber: '', address: '', quantityRequired: '', quantityUnit: 'tons', deliveryLocation: '', expectedDeliveryDate: '', message: product ? `I am interested in bulk procurement of ${product.name}. Please provide detailed quotation including pricing, minimum order quantity, and delivery terms.` : '', hearAboutUs: '', agreedToTerms: false } }) const onSubmit = async (data: InquiryFormData) => { setIsSubmitting(true) try { const formData = { ...data, productId: product?.id, productName: product?.name, productCategory: product?.category?.name, productPrice: product?.price, submissionType: 'b2b_inquiry', submittedAt: new Date().toISOString() } const response = await fetch('/api/inquiries', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(formData), }) if (!response.ok) { throw new Error('Failed to submit inquiry') } const result = await response.json() toast.success('Inquiry submitted successfully!', { description: 'Our team will contact you within 24 hours with a detailed quotation.' }) form.reset() onOpenChange(false) } catch (error) { console.error('Error submitting inquiry:', error) toast.error('Failed to submit inquiry', { description: 'Please try again or contact us directly.' }) } finally { setIsSubmitting(false) } } return ( B2B Inquiry Form {product ? ( <>Requesting quote for: {product.name} ) : ( 'Fill out this form to get a detailed quotation for bulk orders' )}
{/* Company Information Section */}

Company Information

( Company Name * )} /> ( Business Type * )} />
( Contact Person * )} /> ( Designation * )} />
( Email Address * )} /> ( Phone Number * )} />
( GST Number (Optional) )} />
( Complete Business Address *