<?php
/**
 * Admin Resources Management View
 * Lists all educational resources with type, subject filters and CRUD controls.
 */
$typeIconMap = [
    'pdf'          => ['icon' => 'fa-file-pdf', 'color' => 'danger'],
    'video'        => ['icon' => 'fa-play-circle', 'color' => 'primary'],
    'article'      => ['icon' => 'fa-newspaper', 'color' => 'info'],
    'guide'        => ['icon' => 'fa-map', 'color' => 'secondary'],
    'checklist'    => ['icon' => 'fa-list-check', 'color' => 'success'],
    'template'     => ['icon' => 'fa-file-code', 'color' => 'warning'],
    'tip'          => ['icon' => 'fa-lightbulb', 'color' => 'primary'],
];
?>
<div class="container-fluid py-4 px-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="fw-bold font-outfit text-dark m-0"><i class="fa-solid fa-book text-info me-2"></i>Manage Resources</h2>
            <p class="text-muted mb-0"><?= number_format($total ?? 0) ?> resources in the library</p>
        </div>
        <a href="<?= APP_URL ?>/admin/resources/create" class="btn btn-info rounded-pill px-4 font-outfit fw-bold text-white">
            <i class="fa-solid fa-upload me-2"></i>Add Resource
        </a>
    </div>

    <!-- Filters -->
    <div class="card border-0 shadow-sm p-4 mb-4">
        <form action="<?= APP_URL ?>/admin/resources" method="GET" class="row g-3 align-items-end">
            <div class="col-md-4">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Search</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="Title, subject..." value="<?= htmlspecialchars($search ?? '') ?>">
                </div>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Category</label>
                <select name="category" class="form-select">
                    <option value="">All Categories</option>
                    <?php foreach (array_keys($typeIconMap) as $t): ?>
                        <option value="<?= $t ?>" <?= ($selectedCategory ?? '') === $t ? 'selected' : '' ?>><?= ucfirst($t) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">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-info rounded-pill w-100 font-outfit fw-bold text-white"><i class="fa-solid fa-filter me-2"></i>Filter</button>
            </div>
        </form>
    </div>

    <!-- Resources Table -->
    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle text-sm mb-0">
                    <thead class="table-light">
                        <tr>
                            <th class="ps-4 py-3 border-0 font-outfit fw-bold text-muted">Resource</th>
                            <th class="border-0 font-outfit fw-bold text-muted">Category</th>
                            <th class="border-0 font-outfit fw-bold text-muted">Module</th>
                            <th class="border-0 font-outfit fw-bold text-muted">Uploaded By</th>
                            <th class="border-0 font-outfit fw-bold text-muted text-center">Views</th>
                            <th class="border-0 font-outfit fw-bold text-muted">Status</th>
                            <th class="border-0 font-outfit fw-bold text-muted text-center">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($resources ?? [] as $res): 
                            $typeIcon = $typeIconMap[$res['category']] ?? ['icon' => 'fa-file', 'color' => 'secondary'];
                        ?>
                            <tr>
                                <td class="ps-4">
                                    <div class="d-flex align-items-center gap-3">
                                        <?php if (!empty($res['thumbnail'])): ?>
                                            <img src="<?= UPLOAD_URL ?>/thumbnails/<?= htmlspecialchars($res['thumbnail']) ?>" class="rounded-2" style="width:48px;height:48px;object-fit:cover;">
                                        <?php else: ?>
                                            <div class="badge-icon-box bg-<?= $typeIcon['color'] ?>-subtle text-<?= $typeIcon['color'] ?> rounded-2 flex-shrink-0" style="width:48px;height:48px;display:flex;align-items:center;justify-content:center;">
                                                <i class="fa-solid <?= $typeIcon['icon'] ?> fa-lg"></i>
                                            </div>
                                        <?php endif; ?>
                                        <div>
                                            <span class="fw-bold font-outfit text-dark d-block"><?= htmlspecialchars($res['title']) ?></span>
                                            <small class="text-muted"><?= htmlspecialchars(mb_strimwidth($res['description'] ?? '', 0, 50, '...')) ?></small>
                                        </div>
                                    </div>
                                </td>
                                <td><span class="badge bg-<?= $typeIcon['color'] ?>-subtle text-<?= $typeIcon['color'] ?> rounded-pill font-outfit"><?= ucfirst($res['category']) ?></span></td>
                                <td class="text-muted"><?= ucfirst(str_replace('_', ' ', $res['module'] ?? 'general')) ?></td>
                                <td class="text-dark font-outfit"><?= htmlspecialchars($res['first_name'] ?? '') ?> <?= htmlspecialchars($res['last_name'] ?? 'Admin') ?></td>
                                <td class="text-center fw-bold font-outfit"><?= number_format($res['views'] ?? 0) ?></td>
                                <td>
                                    <span class="badge bg-<?= !empty($res['is_published']) ? 'success' : 'secondary' ?>-subtle text-<?= !empty($res['is_published']) ? 'success' : 'secondary' ?> rounded-pill font-outfit">
                                        <?= !empty($res['is_published']) ? 'Active' : 'Hidden' ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <div class="d-flex justify-content-center gap-1">
                                        <a href="<?= APP_URL ?>/resources/<?= $res['id'] ?>" target="_blank" class="btn btn-sm btn-outline-secondary rounded-pill px-2" title="Preview"><i class="fa-solid fa-eye"></i></a>
                                        <a href="<?= APP_URL ?>/admin/resources/<?= $res['id'] ?>/edit" class="btn btn-sm btn-outline-info rounded-pill px-2" title="Edit"><i class="fa-solid fa-pen"></i></a>
                                        <form action="<?= APP_URL ?>/admin/resources/<?= $res['id'] ?>" method="POST" onsubmit="return confirm('Delete this resource?')">
                                            <?= csrf_field() ?><input type="hidden" name="_method" value="DELETE">
                                            <button class="btn btn-sm btn-outline-danger rounded-pill px-2" title="Delete"><i class="fa-solid fa-trash"></i></button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($resources)): ?>
                            <tr><td colspan="8" class="text-center py-5 text-muted">No resources found.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    <?php if (($pagination['total_pages'] ?? 1) > 1): ?>
        <nav class="mt-4 d-flex justify-content-center">
            <ul class="pagination pagination-sm gap-1">
                <?php for ($p = 1; $p <= $pagination['total_pages']; $p++): ?>
                    <li class="page-item <?= $p == ($pagination['current_page'] ?? 1) ? 'active' : '' ?>">
                        <a class="page-link rounded-circle border-0 shadow-sm" href="<?= APP_URL ?>/admin/resources?page=<?= $p ?>&search=<?= urlencode($search ?? '') ?>&type=<?= urlencode($selectedType ?? '') ?>"><?= $p ?></a>
                    </li>
                <?php endfor; ?>
            </ul>
        </nav>
    <?php endif; ?>
</div>
