<?php
/**
 * Resources Library Index View
 * Lists all educational resources with filtering by type, subject, and search.
 */
?>
<div class="container py-4">
    <div class="text-center mb-5">
        <span class="badge bg-info-subtle text-info rounded-pill px-3 py-1 font-outfit fw-bold mb-2 text-uppercase" style="letter-spacing:1px;font-size:11px;"><i class="fa-solid fa-book me-1"></i> Resource Library</span>
        <h2 class="fw-bold font-outfit text-dark">Educational Resources</h2>
        <p class="text-muted col-md-6 mx-auto">Explore study materials, videos, articles, and guides curated to help you excel in your academic journey and university transition.</p>
    </div>

    <!-- Search & Filters -->
    <div class="card border-0 shadow-sm p-4 mb-5">
        <form action="<?= APP_URL ?>/resources" method="GET" class="row g-3 align-items-end">
            <div class="col-md-5">
                <label class="form-label fw-bold font-outfit text-sm text-muted">Search Resources</label>
                <div class="input-group">
                    <span class="input-group-text bg-white border-end-0"><i class="fa-solid fa-search text-muted"></i></span>
                    <input type="text" name="search" class="form-control border-start-0 ps-0" placeholder="Search by title, topic, subject..." value="<?= htmlspecialchars($search ?? '') ?>">
                </div>
            </div>
            <div class="col-md-3">
                <label class="form-label fw-bold font-outfit text-sm text-muted">Resource Category</label>
                <select name="category" class="form-select">
                    <option value="">All Categories</option>
                    <?php foreach (['pdf' => 'PDF', 'video' => 'Video', 'article' => 'Article', 'guide' => 'Guide', 'checklist' => 'Checklist', 'template' => 'Template', 'tip' => 'Tip'] as $val => $label): ?>
                        <option value="<?= $val ?>" <?= ($selectedCategory ?? '') === $val ? 'selected' : '' ?>><?= $label ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label fw-bold font-outfit text-sm text-muted">Module</label>
                <select name="module" class="form-select">
                    <option value="">All Modules</option>
                    <?php foreach (['general', 'transition', 'career', 'scholarship', 'admission', 'study_skills'] as $subj): ?>
                        <option value="<?= $subj ?>" <?= ($selectedModule ?? '') === $subj ? 'selected' : '' ?>><?= ucfirst(str_replace('_', ' ', $subj)) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <button class="btn btn-primary rounded-pill w-100 fw-bold font-outfit"><i class="fa-solid fa-filter me-2"></i>Filter</button>
            </div>
        </form>
    </div>

    <!-- Type Icons Quick Filter -->
    <div class="d-flex flex-wrap gap-2 mb-4">
        <?php
        $typeIconMap = [
            'pdf'          => ['icon' => 'fa-file-pdf', 'color' => 'danger', 'label' => 'PDF'],
            'video'        => ['icon' => 'fa-play-circle', 'color' => 'primary', 'label' => 'Video'],
            'article'      => ['icon' => 'fa-newspaper', 'color' => 'info', 'label' => 'Article'],
            'guide'        => ['icon' => 'fa-map', 'color' => 'secondary', 'label' => 'Guide'],
            'checklist'    => ['icon' => 'fa-list-check', 'color' => 'success', 'label' => 'Checklist'],
            'template'     => ['icon' => 'fa-file-code', 'color' => 'warning', 'label' => 'Template'],
            'tip'          => ['icon' => 'fa-lightbulb', 'color' => 'primary', 'label' => 'Tip'],
        ];
        ?>
        <a href="<?= APP_URL ?>/resources" class="btn btn-<?= empty($selectedCategory) ? 'primary' : 'outline-secondary' ?> rounded-pill btn-sm px-3 font-outfit fw-bold">All</a>
        <?php foreach ($typeIconMap as $type => $meta): ?>
            <a href="<?= APP_URL ?>/resources?category=<?= urlencode($type) ?>&search=<?= urlencode($search ?? '') ?>" 
               class="btn btn-<?= ($selectedCategory ?? '') === $type ? $meta['color'] : 'outline-secondary' ?> rounded-pill btn-sm px-3 font-outfit fw-bold">
                <i class="fa-solid <?= $meta['icon'] ?> me-1"></i><?= $meta['label'] ?>
            </a>
        <?php endforeach; ?>
    </div>

    <!-- Resource Grid -->
    <?php if (empty($resources)): ?>
        <div class="text-center py-5">
            <div class="text-6xl mb-3">📚</div>
            <h5 class="fw-bold font-outfit text-dark">No Resources Found</h5>
            <p class="text-muted">Try adjusting your search filters or check back later for new materials.</p>
        </div>
    <?php else: ?>
        <div class="row g-4">
            <?php foreach ($resources as $resource): 
                $typeIcon = $typeIconMap[$resource['category']] ?? ['icon' => 'fa-file', 'color' => 'secondary', 'label' => ucfirst($resource['category'])];
            ?>
                <div class="col-lg-4 col-md-6">
                    <div class="card border-0 shadow-sm h-100 hover-lift">
                        <div class="card-body p-4">
                            <div class="d-flex align-items-start gap-3 mb-3">
                                <?php if (!empty($resource['thumbnail'])): ?>
                                    <div class="flex-shrink-0">
                                        <img src="<?= UPLOAD_URL ?>/thumbnails/<?= htmlspecialchars($resource['thumbnail']) ?>" class="rounded-3" style="width:48px;height:48px;object-fit:cover;">
                                    </div>
                                <?php else: ?>
                                    <div class="badge-icon-box bg-<?= $typeIcon['color'] ?>-subtle text-<?= $typeIcon['color'] ?> rounded-3 flex-shrink-0" style="width:48px;height:48px;display:flex;align-items:center;justify-content:center;">
                                        <i class="fa-solid <?= $typeIcon['icon'] ?> fs-4"></i>
                                    </div>
                                <?php endif; ?>
                                <div class="flex-grow-1 min-w-0">
                                    <span class="badge bg-<?= $typeIcon['color'] ?>-subtle text-<?= $typeIcon['color'] ?> rounded-pill text-xs font-outfit fw-bold mb-1"><?= htmlspecialchars($typeIcon['label']) ?></span>
                                    <h6 class="fw-bold font-outfit text-dark mb-0 line-clamp-2"><?= htmlspecialchars($resource['title']) ?></h6>
                                </div>
                            </div>
                            <p class="text-muted text-sm mb-3 line-clamp-2"><?= htmlspecialchars($resource['description']) ?></p>
                            <div class="d-flex flex-wrap gap-2 mb-3">
                                <?php if (!empty($resource['module'])): ?>
                                    <span class="badge bg-primary-subtle text-primary rounded-pill text-xs"><?= ucfirst(str_replace('_', ' ', $resource['module'])) ?></span>
                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="card-footer bg-white border-0 px-4 pb-4 pt-0 d-flex gap-2">
                            <a href="<?= APP_URL ?>/resources/<?= $resource['slug'] ?>" class="btn btn-outline-primary btn-sm rounded-pill flex-grow-1 font-outfit fw-bold">
                                <i class="fa-solid fa-eye me-1"></i>View
                            </a>
                            <?php if (!empty($resource['file_path'])): ?>
                                <a href="<?= APP_URL ?>/resources/<?= $resource['slug'] ?>/download" class="btn btn-primary btn-sm rounded-pill flex-shrink-0 font-outfit fw-bold">
                                    <i class="fa-solid fa-download"></i>
                                </a>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <!-- Pagination -->
        <?php if (($totalPages ?? 1) > 1): ?>
            <nav class="mt-5 d-flex justify-content-center">
                <ul class="pagination pagination-sm gap-1">
                    <?php for ($p = 1; $p <= $totalPages; $p++): ?>
                        <li class="page-item <?= $p == ($currentPage ?? 1) ? 'active' : '' ?>">
                            <a class="page-link rounded-circle border-0 shadow-sm" 
                               href="<?= APP_URL ?>/resources?page=<?= $p ?>&search=<?= urlencode($search ?? '') ?>&category=<?= urlencode($selectedCategory ?? '') ?>&module=<?= urlencode($selectedModule ?? '') ?>"><?= $p ?></a>
                        </li>
                    <?php endfor; ?>
                </ul>
            </nav>
        <?php endif; ?>
    <?php endif; ?>
</div>
