<?php
session_start();

$results = $_SESSION["search_results"] ?? [];
$query = $_SESSION["search_query"] ?? "";
$resultCount = count($results);

?>
<!--
    Seite: Suchergebnisse
    Inhalt: Zeigt die Ergebnisse einer Suche an
-->
<div class="s-res-layout-grid">

    <!-- Links: Seitenleiste für Filter und Suche -->
    <aside class="s-res-sidebar">

        <!-- Suchleiste Box -->
        <div class="s-res-sidebar-box">
            <h3 class="s-res-sidebar-title">Suche anpassen</h3>
            <form action="#" method="GET" class="s-res-search-form">
                <input type="search" id="site-search" name="q" placeholder="Suchen..." class="nav__search">
                <button type="submit" class="nav__search-button">Suchen</button>
            </form>
        </div>

        <!-- Sortierfuntion Box -->
        <div class="s-res-sidebar-box">
            <h3 class="s-res-sidebar-title">Sortierung</h3>
            <div class="s-res-filter-group">
                <label class="s-res-filter-option">
                    <input type="radio" name="sort" value="alphabet" checked>
                    <span>Alphabetisch</span>
                </label>
                <label class="s-res-filter-option">
                    <input type="radio" name="sort" value="likes">
                    <span>Beliebtheit (Likes)</span>
                </label>
                <label class="s-res-filter-option">
                    <input type="radio" name="sort" value="newest">
                    <span>Neueste Beiträge</span>
                </label>
                <label class="s-res-filter-option">
                    <input type="radio" name="sort" value="oldest">
                    <span>Älteste Beiträge</span>
                </label>
            </div>
        </div>

    </aside>

    <main class="s-res-main-content">

        <div class="s-res-header">
            <h1 class="s-res-main-title">Suchergebnisse</h1>
            <p class="s-res-meta"><?php echo $resultCount; ?> Treffer für Ihre Suchanfrage "<?php echo htmlspecialchars($query); ?>"</p>
        </div>

        <!-- Ergebnisliste -->
        <div class="s-res-list">

            <?php
            if(isset($_SESSION['status']) && $_SESSION['search_status'] == "new_search_results"): ?>
            
            <?php if ($resultCount === 0): ?>
                    <p>Keine Beiträge zu diesem Suchbegriff gefunden.</p>
                <?php else: ?>
                    <?php foreach ($results as $item): ?>
                        <div class="s-res-item">
                            <div class="s-res-content">
                                <h2 class="s-res-item-title">
                                    <a href="<?php echo $item['link']; ?>" class="s-res-link"><?php echo $item['title']; ?></a>
                                </h2>
                                <p class="s-res-author">Kategorie: <span class="s-res-author-name">Beitrag</span></p>
                            </div>
                            <div class="s-res-arrow">&rarr;</div>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>

            <?php elseif (isset($_SESSION["message"]) && $_SESSION["message"] == "missing_parameters"): ?>
            <p>
                Bitte überprüfe deine Sucheingabe und versuche es erneut!
            </p>
            <?php endif; ?>
            <?php
            unset($_SESSION["search_status"]);
            ?>


        </div>
        <div class="s-res-pagination-footer">

            <!-- Auswahl der Ergebnisse pro Seite -->
            <div class="s-res-limit-selector">
                <label for="s-res-per-page" class="s-res-limit-label">Ergebnisse pro Seite:</label>
                <select id="s-res-per-page" name="limit" class="s-res-limit-select">
                    <option value="10" selected>10</option>
                    <option value="20">20</option>
                    <option value="50">50</option>
                    <option value="100">100</option>
                </select>
            </div>

            <div class="s-res-page-navigation">
                <button type="button" class="s-res-page-btn" disabled>&laquo;</button>
                <button type="button" class="s-res-page-btn s-res-page-btn-active">1</button>
                <button type="button" class="s-res-page-btn">&raquo;</button>
            </div>

        </div>
    </main>

</div>