import Link from "next/link"; import { redirect } from "next/navigation"; import { createAuftrag, createOrtUndAuswaehlen } from "./actions"; import { listOrte, listFahrzeuge } from "@/lib/auftraege/queries"; import { loadProfile } from "@/lib/auth/profile"; import { Card } from "@/components/ui/Card"; import { Button } from "@/components/ui/Button"; import { Alert } from "@/components/ui/Alert"; import { FormField, FIELD_CLASSES } from "@/components/ui/FormField"; function buildOrtFormularUrl( ziel: "abholort" | "zielort", params: { vorgaengerAuftragId?: string; abholortId?: string; zielortId?: string; fahrzeugId?: string } ): string { const query = new URLSearchParams({ zeige_ort_formular: ziel }); if (params.vorgaengerAuftragId) query.set("vorgaenger_auftrag_id", params.vorgaengerAuftragId); if (params.abholortId) query.set("abholort_id", params.abholortId); if (params.zielortId) query.set("zielort_id", params.zielortId); if (params.fahrzeugId) query.set("fahrzeug_id", params.fahrzeugId); return `/auftraege/neu?${query.toString()}`; } export default async function NeuerAuftragPage({ searchParams, }: { searchParams: Promise<{ error?: string; vorgaenger_auftrag_id?: string; abholort_id?: string; zielort_id?: string; fahrzeug_id?: string; zeige_ort_formular?: string; }>; }) { const profileResult = await loadProfile(); // Seitenebenen-Gate zusaetzlich zur INSERT-RLS-Policy auf `auftraege` // (admin/disponent-only): ohne diesen Check wuerde die Seite das Formular // auch Fahrern/Readonly anzeigen, obwohl ein Submit dann ohnehin an der // RLS-Policy scheitern wuerde -- das ist verwirrende UX statt eines echten // Sicherheitslecks, aber ein frueher Redirect ist die klarere Loesung. if ( profileResult.status !== "ok" || (profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent") ) { redirect(`/auftraege?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege anlegen.")}`); } const { error, vorgaenger_auftrag_id, abholort_id, zielort_id, fahrzeug_id, zeige_ort_formular } = await searchParams; const [orte, fahrzeuge] = await Promise.all([listOrte(), listFahrzeuge()]); const createOrtFuerAbholortAction = createOrtUndAuswaehlen.bind(null, "abholort"); const createOrtFuerZielortAction = createOrtUndAuswaehlen.bind(null, "zielort"); return (