<div class="page-banner">
    <div class="container">
        <h2>Genomics Library</h2>
        <p>Stay updated with the latest research, articles, and insights</p>
        <div class="breadcrumb">
            <a href="/">Home</a>
            <span class="separator"><i class="fas fa-chevron-right"></i></span>
            <span>Genomics Library</span>
        </div>
    </div>
</div>

<section class="sections-area">
    <div class="container">
        <div class="articles-filters fade-in-up">
            <form class="articles-search" method="get" action="/articles">
                <?php if (!empty($currentCategory)): ?>
                <input type="hidden" name="category" value="<?= htmlspecialchars($currentCategory) ?>">
                <?php endif; ?>
                <i class="fas fa-search"></i>
                <input type="text" name="q" placeholder="Search articles..." value="<?= htmlspecialchars($searchQuery ?? '') ?>">
            </form>
            <div class="category-filters">
                <a href="/articles" class="category-btn <?= empty($currentCategory) ? 'active' : '' ?>">All</a>
                <?php if (!empty($categories)): ?>
                    <?php foreach ($categories as $cat): ?>
                    <a href="/articles?category=<?= urlencode($cat['id']) ?>" class="category-btn <?= ($currentCategory ?? '') == $cat['id'] ? 'active' : '' ?>"><?= htmlspecialchars($cat['name']) ?></a>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>

        <div class="sections-grid">
            <?php if (!empty($articles)): ?>
                <?php foreach ($articles as $index => $art): ?>
                <div class="article-card fade-in-up" style="transition-delay: <?= ($index % 6) * 0.1 ?>s">
                    <?php if (!empty($art['image_url'])): ?>
                    <div class="article-card-image">
                        <img src="<?= htmlspecialchars($art['image_url']) ?>" alt="<?= htmlspecialchars($art['title']) ?>">
                        <?php if (!empty($art['category_name'])): ?>
                        <span class="article-category-badge"><?= htmlspecialchars($art['category_name']) ?></span>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                    <div class="article-card-body">
                        <h3><a href="/article/<?= htmlspecialchars($art['slug'] ?? $art['id']) ?>"><?= htmlspecialchars($art['title']) ?></a></h3>
                        <p><?= htmlspecialchars($art['excerpt'] ?? substr($art['content'] ?? '', 0, 150)) ?>...</p>
                        <div class="article-meta">
                            <span><i class="fas fa-user"></i> <?= htmlspecialchars($art['author'] ?? 'Admin') ?></span>
                            <span><i class="fas fa-calendar"></i> <?= date('M d, Y', strtotime($art['created_at'] ?? 'now')) ?></span>
                            <?php if (isset($art['views'])): ?>
                            <span><i class="fas fa-eye"></i> <?= htmlspecialchars($art['views']) ?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="no-results">
                    <i class="fas fa-search"></i>
                    <p>No articles found.</p>
                </div>
            <?php endif; ?>
        </div>

        <?php
        $totalPages = isset($totalArticles, $perPage) && $perPage > 0 ? ceil($totalArticles / $perPage) : 1;
        if ($totalPages > 1):
        ?>
        <div class="pagination fade-in-up">
            <?php if (($currentPage ?? 1) > 1): ?>
            <a href="?page=<?= ($currentPage ?? 1) - 1 ?><?= !empty($currentCategory) ? '&category=' . urlencode($currentCategory) : '' ?><?= !empty($searchQuery) ? '&q=' . urlencode($searchQuery) : '' ?>"><i class="fas fa-chevron-left"></i></a>
            <?php endif; ?>
            <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                <?php if ($i == ($currentPage ?? 1)): ?>
                <span class="active"><?= $i ?></span>
                <?php else: ?>
                <a href="?page=<?= $i ?><?= !empty($currentCategory) ? '&category=' . urlencode($currentCategory) : '' ?><?= !empty($searchQuery) ? '&q=' . urlencode($searchQuery) : '' ?>"><?= $i ?></a>
                <?php endif; ?>
            <?php endfor; ?>
            <?php if (($currentPage ?? 1) < $totalPages): ?>
            <a href="?page=<?= ($currentPage ?? 1) + 1 ?><?= !empty($currentCategory) ? '&category=' . urlencode($currentCategory) : '' ?><?= !empty($searchQuery) ? '&q=' . urlencode($searchQuery) : '' ?>"><i class="fas fa-chevron-right"></i></a>
            <?php endif; ?>
        </div>
        <?php endif; ?>
    </div>
</section>
