"use client" import { useState } from "react" import { ClipboardList, Plus, X, CheckCircle2, Clock, Trash2 } from "lucide-react" import { Select } from "@/components/ui/select" import { formatDate } from "@/lib/utils" import { toast } from "sonner" const INSPECTION_TYPES = [ { value: "move_in", label: "Move-In" }, { value: "move_out", label: "Move-Out" }, { value: "routine", label: "Routine" }, ] const typeColors: Record = { move_in: "text-emerald-400 bg-emerald-500/10 border-emerald-500/20", move_out: "text-rose-400 bg-rose-500/10 border-rose-500/20", routine: "text-blue-400 bg-blue-500/10 border-blue-500/20", } const statusIcon: Record = { draft: Clock, complete: CheckCircle2, } export function InspectionManager({ inspections: initial, properties }: { inspections: any[]; properties: any[] }) { const [inspections, setInspections] = useState(initial) const [showForm, setShowForm] = useState(false) const [loading, setLoading] = useState(false) const [selectedProp, setSelectedProp] = useState("") const [form, setForm] = useState({ type: "move_in", unit_id: "", date: new Date().toISOString().slice(0, 10), notes: "" }) const propertyOptions = [ { value: "", label: "Select property…" }, ...properties.map((p: any) => ({ value: p.id, label: p.name })), ] const units = properties.find((p: any) => p.id === selectedProp)?.units ?? [] const unitOptions = [ { value: "", label: "No specific unit" }, ...units.map((u: any) => ({ value: u.id, label: `Unit ${u.unit_number}` })), ] 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" async function toggleStatus(id: string, current: string) { const next = current === "complete" ? "draft" : "complete" const res = await fetch(`/api/inspections/${id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ status: next }), }) if (res.ok) { setInspections(v => v.map(i => i.id === id ? { ...i, status: next } : i)) toast.success(next === "complete" ? "Marked complete" : "Marked draft") } } async function deleteInspection(id: string) { await fetch(`/api/inspections/${id}`, { method: "DELETE" }) setInspections(v => v.filter(i => i.id !== id)) toast.success("Inspection deleted") } async function create() { if (!selectedProp) { toast.error("Select a property"); return } setLoading(true) const res = await fetch("/api/inspections", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ property_id: selectedProp, ...form }), }) const data = await res.json() setLoading(false) if (!res.ok) { toast.error(data.error ?? "Failed"); return } setInspections(v => [data, ...v]) setShowForm(false) setSelectedProp("") setForm({ type: "move_in", unit_id: "", date: new Date().toISOString().slice(0, 10), notes: "" }) toast.success("Inspection created") } return (
{!showForm && ( )} {showForm && (

New Inspection

setForm(f => ({ ...f, date: e.target.value }))} className={cls} />
setForm(f => ({ ...f, unit_id: v }))} options={unitOptions} />
)}
setForm(f => ({ ...f, notes: e.target.value }))} placeholder="Optional notes…" className={cls} />
)} {inspections.length === 0 ? (

No inspections yet

Create move-in and move-out checklists for each unit

) : (

{inspections.length} Inspection{inspections.length !== 1 ? "s" : ""}

{inspections.map((ins: any) => { const StatusIcon = statusIcon[ins.status] ?? Clock const typeColor = typeColors[ins.type] ?? typeColors.routine const typeLabel = INSPECTION_TYPES.find(t => t.value === ins.type)?.label ?? ins.type return (

{typeLabel} Inspection

{typeLabel}

{ins.property?.name}{ins.unit ? ` · Unit ${ins.unit.unit_number}` : ""} · {formatDate(ins.date)}

) })}
)}
) }