"use client" import { useState } from "react" import { useRouter } from "next/navigation" import { toast } from "sonner" import { Select } from "@/components/ui/select" const STATUSES = [ { value: "pending", label: "Pending" }, { value: "paid", label: "Paid" }, { value: "overdue", label: "Overdue" }, { value: "partial", label: "Partial" }, { value: "waived", label: "Waived" }, ] const PAYMENT_METHODS = [ { value: "", label: "Select method…" }, { value: "cash", label: "Cash" }, { value: "bank_transfer", label: "Bank Transfer" }, { value: "check", label: "Check" }, { value: "stripe", label: "Stripe" }, ] export function RentPaymentForm({ tenants }: { tenants: any[] }) { const router = useRouter() const [loading, setLoading] = useState(false) const [error, setError] = useState("") const [selectedTenantId, setSelectedTenantId] = useState("") const tenantOptions = [ { value: "", label: "Select tenant…" }, ...tenants.map((t: any) => ({ value: t.id, label: `${t.first_name} ${t.last_name}${t.property?.name ? ` — ${t.property.name}` : ""}${t.unit ? ` (Unit ${t.unit.unit_number})` : ""}`, })), ] const selectedTenant = tenants.find((t: any) => t.id === selectedTenantId) const cls = "w-full rounded-lg border border-white/10 bg-white/5 px-4 py-2.5 text-sm text-white placeholder-white/30 outline-none ring-indigo-500 transition focus:border-indigo-500/50 focus:ring-1" const lbl = "mb-1.5 block text-sm font-medium text-white/70" async function handleSubmit(e: React.FormEvent) { e.preventDefault() setLoading(true) setError("") const formData = new FormData(e.currentTarget) const body = { tenant_id: formData.get("tenant_id"), property_id: selectedTenant?.property?.id, unit_id: selectedTenant?.unit_id || undefined, amount: Number(formData.get("amount")), due_date: formData.get("due_date"), paid_date: formData.get("paid_date") || undefined, status: formData.get("status"), payment_method: formData.get("payment_method") || undefined, notes: formData.get("notes") || undefined, } const res = await fetch("/api/rent", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }) const data = await res.json() setLoading(false) if (!res.ok) { setError(typeof data.error === "string" ? data.error : "Something went wrong") return } toast.success("Payment recorded") router.push("/rent") router.refresh() } const today = new Date().toISOString().slice(0, 10) return (
{error && (
{error}
)}