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

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

if (isset($_GET['details']) && trim((string) $_GET['details']) !== "") {
    header(
        "Location: order.php?id=" .
            urlencode(trim((string) $_GET['details'])),
    );
    exit();
}

$pageTitle = "Bestellungen";

$orders = getOrders();
usort($orders, function ($left, $right) {
    return strcmp($right["created_at"], $left["created_at"]);
});

$filter = trim((string) ($_GET['filter'] ?? "all"));
$searchOrderId = trim((string) ($_GET['order_id'] ?? ""));

if ($searchOrderId !== "") {
    $orders = array_values(
        array_filter($orders, function ($order) use ($searchOrderId) {
            return stripos($order["id"], $searchOrderId) !== false;
        }),
    );
}

if ($filter !== "all") {
    $orders = array_values(
        array_filter($orders, function ($order) use ($filter) {
            switch ($filter) {
                case "open":
                    return $order["status"] === "open";
                case "partial":
                    return $order["status"] === "partial";
                case "processed":
                    return $order["status"] === "processed";
                case "cancelled":
                    return $order["status"] === "cancelled";
            }
            return true;
        }),
    );
}

$bodyClass = "admin-page";
include __DIR__ . "/../includes/header.php";
?>

<div class="admin-header">
    <h2>Bestellungen</h2>
    <div>
        <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
    </div>
</div>

<div class="panel">
    <form method="GET" class="admin-filter-form">
        <div class="admin-filter-field admin-filter-field-wide">
            <label for="order_id">Bestellnummer suchen</label>
            <input type="text" id="order_id" name="order_id" value="<?php echo escape(
                $searchOrderId,
            ); ?>" placeholder="z. B. FWFS-2026-001">
        </div>
        <div>
            <label for="filter">Filter</label>
            <select id="filter" name="filter">
                <option value="all" <?php echo $filter === "all"
                    ? "selected"
                    : ""; ?>>Alle</option>
                <option value="open" <?php echo $filter === "open"
                    ? "selected"
                    : ""; ?>>Offen</option>
                <option value="partial" <?php echo $filter === "partial"
                    ? "selected"
                    : ""; ?>>Teilweise bearbeitet</option>
                <option value="processed" <?php echo $filter === "processed"
                    ? "selected"
                    : ""; ?>>Bearbeitet</option>
                <option value="cancelled" <?php echo $filter === "cancelled"
                    ? "selected"
                    : ""; ?>>Storniert</option>
            </select>
        </div>
        <div class="admin-filter-actions">
            <button type="submit" class="btn">Filtern</button>
            <a href="orders.php" class="btn btn-secondary">Zurücksetzen</a>
        </div>
    </form>
</div>

<?php if (empty($orders)): ?>
    <div class="alert alert-info">
        <p>Keine Bestellungen gefunden.</p>
    </div>
<?php else: ?>
    <div class="table-responsive">
        <table class="responsive-table">
            <thead>
                <tr>
                    <th>Bestellnummer</th>
                    <th>Name</th>
                    <th>Organisation</th>
                    <th>Artikel</th>
                    <th>Erstellt</th>
                    <th>Status</th>
                    <th>Aktionen</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($orders as $order): ?>
                    <tr>
                        <td data-label="Bestellnummer"><strong><?php echo escape(
                            $order["id"],
                        ); ?></strong></td>
                        <td data-label="Name"><?php echo escape(
                            $order["customer_name"],
                        ); ?></td>
                        <td data-label="Organisation"><?php echo escape(
                            $order["organization_label"],
                        ); ?></td>
                        <td data-label="Artikel"><?php echo count(
                            $order["items"],
                        ); ?></td>
                        <td data-label="Erstellt"><?php echo escape(
                            formatDate($order["created_at"]),
                        ); ?></td>
                        <td data-label="Status"><span class="status <?php echo escape(
                            getOrderStatusClass($order),
                        ); ?>"><?php echo escape(
    getOrderStatusLabel($order),
); ?></span></td>
                        <td data-label="Aktionen">
                            <a href="order.php?id=<?php echo urlencode(
                                $order["id"],
                            ); ?>" class="btn btn-small">Details</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>

<?php include __DIR__ . "/../includes/footer.php"; ?>
