'use client' import React, { useState } from 'react' import { Input } from '../ui/input' import { Button } from '../ui/button' import { Label } from '../ui/label' import { toast } from 'sonner' import { Address, Vendor } from '@prisma/client' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select' import { Textarea } from '../ui/textarea' import { Checkbox } from '../ui/checkbox' interface CreateVendorFormProps { onSuccess?: (newVendorId?: string) => void } const VENDOR_TYPES = [ 'VENUE', 'CATERER', 'PHOTOGRAPHER', 'VIDEOGRAPHER', 'FLORIST', 'BAKERY', 'DJ', 'BAND', 'OFFICIANT', 'HAIR_MAKEUP', 'TRANSPORTATION', 'RENTALS', 'DECOR', 'PLANNER', 'STATIONERY', 'OTHER' ] as const const VENDOR_STATUSES = [ 'RESEARCHING', 'CONTACTING', 'RESPONDED', 'PROPOSAL_RECEIVED', 'NEGOTIATING', 'CONTRACT_SENT', 'CONTRACT_SIGNED', 'DECLINED', 'BACKUP' ] as const const defaultFormValues = { name: '', type: '' as typeof VENDOR_TYPES[number], description: '', website: '', contactPerson: '', email: '', phone: '', street: '', city: '', state: '', postalCode: '', country: 'United States', status: 'CONTACTING' as typeof VENDOR_STATUSES[number], isBooked: false, bookedDate: '', quotedPrice: '', finalCost: '', depositPaid: '', depositDueDate: '', finalPaymentDue: '', paymentNotes: '', notes: '', contractUrl: '', proposalUrl: '', } export default function CreateVendorForm({ onSuccess }: CreateVendorFormProps) { const [formData, setFormData] = useState(defaultFormValues) const [loading, setLoading] = useState(false) function handleChange(e: React.ChangeEvent) { const { name, value, type } = e.target setFormData(prev => ({ ...prev, [name]: type === 'number' ? parseFloat(value) || 0 : value })) } function handleSelectChange(name: string, value: string) { setFormData(prev => ({ ...prev, [name]: value })) } function handleCheckboxChange(name: string, checked: boolean) { setFormData(prev => ({ ...prev, [name]: checked })) } async function handleSubmit(e: React.FormEvent) { e.preventDefault() if (!formData.name || !formData.type) { toast.error('Please fill in required fields (Name and Type)') return } setLoading(true) try { const res = await fetch('/api/vendors/create', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...formData, quotedPrice: formData.quotedPrice ? parseFloat(formData.quotedPrice) : null, finalCost: formData.finalCost ? parseFloat(formData.finalCost) : null, depositPaid: formData.depositPaid ? parseFloat(formData.depositPaid) : null, bookedDate: formData.bookedDate || null, depositDueDate: formData.depositDueDate || null, finalPaymentDue: formData.finalPaymentDue || null, }) }) if (!res.ok) { const error = await res.json() throw new Error(error.message || 'Failed to create vendor') } const data = await res.json() const newVendorId = data?.id toast.success('Vendor created successfully!') setFormData(defaultFormValues) if (onSuccess) onSuccess(newVendorId) } catch (err) { console.error(err) toast.error(err instanceof Error ? err.message : 'Something went wrong') } finally { setLoading(false) } } return (
{/* Basic Information */}

Basic Information