"use client" import { useState } from "react" import { useRouter } from "next/navigation" import { Plus, Trash2, Phone, Mail, Wrench, X, Pencil, Check } from "lucide-react" import { Select } from "@/components/ui/select" import { toast } from "sonner" const TRADES = [ { value: "plumber", label: "Plumber" }, { value: "electrician", label: "Electrician" }, { value: "hvac", label: "HVAC" }, { value: "handyman", label: "Handyman" }, { value: "cleaner", label: "Cleaner" }, { value: "landscaper", label: "Landscaper" }, { value: "roofer", label: "Roofer" }, { value: "painter", label: "Painter" }, { value: "contractor", label: "Contractor" }, { value: "other", label: "Other" }, ] const tradeColors: Record = { plumber: "text-blue-400 bg-blue-500/10", electrician: "text-yellow-400 bg-yellow-500/10", hvac: "text-cyan-400 bg-cyan-500/10", handyman: "text-orange-400 bg-orange-500/10", cleaner: "text-green-400 bg-green-500/10", landscaper: "text-emerald-400 bg-emerald-500/10", roofer: "text-amber-400 bg-amber-500/10", painter: "text-purple-400 bg-purple-500/10", contractor: "text-indigo-400 bg-indigo-500/10", other: "text-white/40 bg-white/5", } export function VendorManager({ vendors: initial, properties }: { vendors: any[]; properties: any[] }) { const router = useRouter() const [vendors, setVendors] = useState(initial) const [showForm, setShowForm] = useState(false) const [loading, setLoading] = useState(false) const [editingId, setEditingId] = useState(null) const [editForm, setEditForm] = useState({}) const [form, setForm] = useState({ name: "", trade: "handyman", phone: "", email: "", notes: "", property_id: "" }) const propertyOptions = [ { value: "", label: "All properties" }, ...properties.map((p: any) => ({ value: p.id, label: p.name })), ] async function addVendor() { if (!form.name.trim()) return setLoading(true) const res = await fetch("/api/vendors", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form), }) const data = await res.json() setLoading(false) if (!res.ok) { toast.error(data.error ?? "Failed to add vendor"); return } setVendors(v => [...v, data]) setForm({ name: "", trade: "handyman", phone: "", email: "", notes: "", property_id: "" }) setShowForm(false) toast.success("Vendor added") } function startEdit(v: any) { setEditingId(v.id) setEditForm({ name: v.name, trade: v.trade, phone: v.phone ?? "", email: v.email ?? "", notes: v.notes ?? "" }) } async function saveEdit(id: string) { const res = await fetch(`/api/vendors/${id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(editForm), }) const data = await res.json() if (!res.ok) { toast.error(data.error ?? "Failed to update"); return } setVendors(v => v.map(x => x.id === id ? { ...x, ...data } : x)) setEditingId(null) toast.success("Vendor updated") } async function deleteVendor(id: string) { await fetch(`/api/vendors/${id}`, { method: "DELETE" }) setVendors(v => v.filter(x => x.id !== id)) toast.success("Vendor removed") } const cls = "w-full rounded-lg border border-white/10 bg-white/5 px-3 py-2.5 text-sm text-white placeholder-white/30 outline-none focus:border-indigo-500/50 focus:ring-1 focus:ring-indigo-500 transition" return (
{/* Add button */} {!showForm && ( )} {/* Add form */} {showForm && (

New Vendor

setForm(f => ({ ...f, name: e.target.value }))} placeholder="John's Plumbing" className={cls} />
setForm(f => ({ ...f, phone: e.target.value }))} placeholder="+1 555 000 0000" className={cls} />
setForm(f => ({ ...f, email: e.target.value }))} placeholder="vendor@email.com" className={cls} />
setForm(f => ({ ...f, notes: e.target.value }))} placeholder="Reliable, good rates..." className={cls} />
)} {/* Vendor list */} {vendors.length === 0 ? (

No vendors yet

Add contractors and service providers for quick access

) : (
{vendors.map((v: any) => (
{editingId === v.id ? (
setEditForm((f: any) => ({ ...f, name: e.target.value }))} placeholder="Name" className={cls} /> setEditForm((f: any) => ({ ...f, phone: e.target.value }))} placeholder="Phone" className={cls} /> setEditForm((f: any) => ({ ...f, email: e.target.value }))} placeholder="Email" className={cls} />
setEditForm((f: any) => ({ ...f, notes: e.target.value }))} placeholder="Notes" className={cls} />
) : (
{v.name[0]}

{v.name}

{v.trade}
{v.phone && {v.phone}} {v.email && {v.email}}
{v.notes &&

{v.notes}

}
)}
))}
)}
) }