import Link from "next/link"; import { redirect } from "next/navigation"; import { createAuftrag, createOrtUndAuswaehlen, createFahrzeugUndAuswaehlen } from "./actions"; import { listOrte, listFahrzeuge } from "@/lib/auftraege/queries"; import { loadProfile } from "@/lib/auth/profile"; import { fahrzeugBezeichnung } from "@/lib/orte-fahrzeuge/fahrzeug-bezeichnung"; 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"; import { ZeitplanungFeld } from "@/components/auftraege/ZeitplanungFeld"; import { FinFeld } from "@/components/fahrzeuge/FinFeld"; import { berechneVorbefuelltenZeitpunkt } from "@/lib/auftraege/zeitplanung"; 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()}`; } function buildFahrzeugFormularUrl(params: { vorgaengerAuftragId?: string; abholortId?: string; zielortId?: string; }): string { const query = new URLSearchParams({ zeige_fahrzeug_formular: "1" }); 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); 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; zeige_fahrzeug_formular?: string; }>; }) { const profileResult = await loadProfile(); 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, zeige_fahrzeug_formular, } = await searchParams; const [orte, fahrzeuge] = await Promise.all([listOrte(), listFahrzeuge()]); const createOrtFuerAbholortAction = createOrtUndAuswaehlen.bind(null, "abholort"); const createOrtFuerZielortAction = createOrtUndAuswaehlen.bind(null, "zielort"); const vorbefuelltesDatum = berechneVorbefuelltenZeitpunkt(); return (