<?php
require_once __DIR__ . "/../config.php";
require_once __DIR__ . "/../includes/functions.php";

if (empty($_SESSION['admin_logged_in'])) {
    header("Location: login.php");
    exit();
}

$orderId = trim((string) ($_GET['id'] ?? ""));
if ($orderId === "") {
    http_response_code(400);
    header("Content-Type: text/plain; charset=UTF-8");
    echo "Keine Bestellnummer angegeben.";
    exit();
}

$order = getOrderById($orderId);
if ($order === null) {
    http_response_code(404);
    header("Content-Type: text/plain; charset=UTF-8");
    echo "Bestellung nicht gefunden.";
    exit();
}

streamOrderPdf(
    $order,
    "bestellung-" . strtolower($order["id"]) . ".pdf",
    true,
);
