"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { updateWaterEntry, deleteWaterEntry } from "@/actions/water-log/admin"; import { AdminInput, AdminTextInput, AdminTextarea, AdminSelect, AdminButton } from "./design-system"; type WaterEntry = { id: string; headgate_id: string; user_id: string; headgate_name: string; user_name: string; measurement: number; unit: string; notes: string | null; submitted_via: string; logged_at: string; headgate_unit?: string; }; const UNIT_OPTIONS = ["CFS", "GPM", "gal", "ac-in", "ac-ft"]; type Props = { entry: WaterEntry; brandId: string; backHref?: string; }; export default function WaterLogEntryEditForm({ entry, backHref = "/admin/water-log" }: Props) { const router = useRouter(); const [measurement, setMeasurement] = useState(entry.measurement); const [unit, setUnit] = useState(entry.unit); const [notes, setNotes] = useState(entry.notes ?? ""); const [saving, setSaving] = useState(false); const [deleting, setDeleting] = useState(false); const [error, setError] = useState(null); async function handleSave(e: React.FormEvent) { e.preventDefault(); setSaving(true); setError(null); const result = await updateWaterEntry(entry.id, measurement, notes || null, unit); if (result.success) { router.push(backHref); } else { setError(result.error ?? "Failed to save"); setSaving(false); } } async function handleDelete() { if (!window.confirm(`Delete this entry from ${entry.user_name} at ${entry.headgate_name}? This cannot be undone.`)) return; setDeleting(true); const result = await deleteWaterEntry(entry.id); if (result.success) { router.push(backHref); } else { setError(result.error ?? "Failed to delete"); setDeleting(false); } } return (
{error && (
{error}
)}

{entry.headgate_name}

{entry.user_name}

setMeasurement(parseFloat(e.target.value) || 0)} /> setUnit(e.target.value)} options={UNIT_OPTIONS.map((u) => ({ value: u, label: u }))} />

{entry.submitted_via}

{new Date(entry.logged_at).toLocaleDateString("en-US", { month: "short", day: "numeric", hour: "numeric", minute: "2-digit", })}

setNotes(e.target.value)} rows={3} placeholder="Optional notes..." />
Delete Entry Save Changes
); }