<?php ob_start(); ?>

<section class="downloads-section">
    <div class="container">

        <div class="downloads-header">
            <h1 class="section-title"><?= e($page_title ?? 'Downloads') ?></h1>

            <!-- Search form -->
            <form method="GET" action="/download" class="downloads-search-form">
                <div class="search-wrap">
                    <input type="text" name="search" value="<?= e($search ?? '') ?>"
                           placeholder="Search downloads…"
                           class="search-input"
                           autocomplete="off">
                    <?php if (($current_category ?? '') !== ''): ?>
                    <input type="hidden" name="category" value="<?= e($current_category) ?>">
                    <?php endif; ?>
                    <button type="submit" class="search-btn">Search</button>
                    <?php if (($search ?? '') !== ''): ?>
                    <a href="/download<?= ($current_category ?? '') !== '' ? '?category=' . e($current_category) : '' ?>"
                       class="search-clear">✕</a>
                    <?php endif; ?>
                </div>
            </form>

            <!-- Category filter -->
            <?php if (!empty($categories)): ?>
            <nav class="downloads-category-filter" aria-label="Download categories">
                <a href="/download" class="<?= ($current_category ?? '') === '' ? 'active' : '' ?>">All</a>
                <?php foreach ($categories as $cat): ?>
                <a href="/download/category/<?= e($cat['slug']) ?>"
                   class="<?= ($current_category ?? '') === $cat['slug'] ? 'active' : '' ?>">
                    <?= e($cat['name']) ?>
                </a>
                <?php endforeach; ?>
            </nav>
            <?php endif; ?>
        </div>

        <?php if (empty($downloads)): ?>
        <p class="text-muted">
            No downloads found<?= ($current_category ?? '') !== '' ? ' in this category' : '' ?><?= ($search ?? '') !== '' ? ' for "' . e($search) . '"' : '' ?>.
        </p>
        <?php else: ?>

        <div class="downloads-grid">
            <?php foreach ($downloads as $dl): ?>
            <?php
                $hasFile    = !empty($dl['file_path']);
                $fileExt    = $hasFile ? strtoupper(pathinfo($dl['file_path'], PATHINFO_EXTENSION)) : '';
                $fileSizeHR = '';
                if ($hasFile && !empty($dl['file_size'])) {
                    $bytes      = (int) $dl['file_size'];
                    $fileSizeHR = $bytes >= 1048576
                        ? round($bytes / 1048576, 1) . ' MB'
                        : ($bytes >= 1024 ? round($bytes / 1024, 1) . ' KB' : $bytes . ' B');
                }
            ?>
            <article class="download-card">
                <?php if (!empty($dl['media_path'])): ?>
                <div class="download-card-image">
                    <?= picture_tag($dl['media_path'], $dl['media_alt'] ?? $dl['title'], 'download-thumb', true) ?>
                </div>
                <?php else: ?>
                <div class="download-card-placeholder">
                    <span class="download-placeholder-ext"><?= $fileExt !== '' ? e($fileExt) : '↓' ?></span>
                </div>
                <?php endif; ?>
                <div class="download-card-body">
                    <?php if (!empty($dl['category_name'])): ?>
                    <span class="download-category">
                        <a href="/download/category/<?= e($dl['category_slug']) ?>"><?= e($dl['category_name']) ?></a>
                    </span>
                    <?php endif; ?>

                    <h3><a href="/download/<?= e($dl['slug']) ?>"><?= e($dl['title']) ?></a></h3>

                    <?php if (!empty($dl['version'])): ?>
                    <span class="download-version">v<?= e($dl['version']) ?></span>
                    <?php endif; ?>

                    <?php if (!empty($dl['excerpt'])): ?>
                    <p class="download-excerpt"><?= e(str_truncate($dl['excerpt'], 120)) ?></p>
                    <?php endif; ?>

                    <div class="download-card-footer">
                        <?php if ($hasFile): ?>
                        <div class="download-meta">
                            <span class="download-ext-badge"><?= e($fileExt) ?></span>
                            <?php if ($fileSizeHR !== ''): ?>
                            <span class="download-size"><?= e($fileSizeHR) ?></span>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>
                        <a href="/download/<?= e($dl['slug']) ?>" class="btn-download-detail">Details →</a>
                    </div>
                </div>
            </article>
            <?php endforeach; ?>
        </div>

        <?php if (($pages ?? 1) > 1): ?>
        <div class="pagination">
            <?php for ($p = 1; $p <= $pages; $p++): ?>
            <?php
                $catPart    = ($current_category ?? '') !== '' ? '/category/' . $current_category : '';
                $searchPart = ($search ?? '') !== '' ? '&search=' . urlencode($search) : '';
                $pUrl       = '/download' . $catPart . ($p > 1 ? '?page=' . $p . $searchPart : ($searchPart !== '' ? '?' . ltrim($searchPart, '&') : ''));
            ?>
            <a href="<?= e($pUrl) ?>" class="pagination-btn <?= $p === ($page ?? 1) ? 'active' : '' ?>"><?= $p ?></a>
            <?php endfor; ?>
        </div>
        <?php endif; ?>

        <?php endif; ?>
    </div>
</section>

<?php $content = ob_get_clean(); include __DIR__ . '/layout.php'; ?>
