import { useState, useEffect } from "react"; import { useNavigate, useParams, useSearchParams } from "react-router-dom"; import api from "../api/client"; const CATEGORIES = ["general", "maintenance", "installation", "issue", "action_item", "other"]; export default function NoteForm() { const { id } = useParams(); const isEdit = Boolean(id); const navigate = useNavigate(); const [searchParams] = useSearchParams(); const [title, setTitle] = useState(""); const [content, setContent] = useState(""); const [category, setCategory] = useState("general"); const [deviceId, setDeviceId] = useState(searchParams.get("device_id") || ""); const [userId, setUserId] = useState(searchParams.get("user_id") || ""); const [devices, setDevices] = useState([]); const [users, setUsers] = useState([]); const [loading, setLoading] = useState(false); const [saving, setSaving] = useState(false); const [error, setError] = useState(""); useEffect(() => { loadOptions(); if (isEdit) loadNote(); }, [id]); const loadOptions = async () => { try { const [devData, usrData] = await Promise.all([ api.get("/devices"), api.get("/users"), ]); setDevices(devData.devices || []); setUsers(usrData.users || []); } catch { // Non-critical — dropdowns will just be empty } }; const loadNote = async () => { setLoading(true); try { const note = await api.get(`/equipment/notes/${id}`); setTitle(note.title || ""); setContent(note.content || ""); setCategory(note.category || "general"); setDeviceId(note.device_id || ""); setUserId(note.user_id || ""); } catch (err) { setError(err.message); } finally { setLoading(false); } }; const handleSubmit = async (e) => { e.preventDefault(); setSaving(true); setError(""); try { const body = { title, content, category, device_id: deviceId || null, user_id: userId || null, }; let noteId = id; if (isEdit) { await api.put(`/equipment/notes/${id}`, body); } else { const created = await api.post("/equipment/notes", body); noteId = created.id; } navigate(`/equipment/notes/${noteId}`); } catch (err) { setError(err.message); } finally { setSaving(false); } }; if (loading) { return