<?php
/**
 * Admin Universities Management View
 * Lists all universities with search, status management, and CRUD shortcuts.
 */
?>
<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-building-columns text-primary me-2"></i>Manage Universities</h2>
            <p class="text-muted mb-0"><?= number_format($total ?? 0) ?> universities in the system</p>
        </div>
        <a href="<?= APP_URL ?>/admin/universities/create" class="btn btn-primary rounded-pill px-4 font-outfit fw-bold">
            <i class="fa-solid fa-plus me-2"></i>Add University
        </a>
    </div>

    <!-- Filters -->
    <div class="card border-0 shadow-sm p-4 mb-4">
        <form action="<?= APP_URL ?>/admin/universities" method="GET" class="row g-3 align-items-end">
            <div class="col-md-5">
                <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="University name, location..." value="<?= htmlspecialchars($search ?? '') ?>">
                </div>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Type</label>
                <select name="type" class="form-select">
                    <option value="">All Types</option>
                    <option value="Public" <?= ($selectedType ?? '') === 'Public' ? 'selected' : '' ?>>Public</option>
                    <option value="Private" <?= ($selectedType ?? '') === 'Private' ? 'selected' : '' ?>>Private</option>
                    <option value="Technical" <?= ($selectedType ?? '') === 'Technical' ? 'selected' : '' ?>>Technical</option>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Status</label>
                <select name="status" class="form-select">
                    <option value="">All</option>
                    <option value="1" <?= isset($selectedStatus) && $selectedStatus === '1' ? 'selected' : '' ?>>Active</option>
                    <option value="0" <?= isset($selectedStatus) && $selectedStatus === '0' ? 'selected' : '' ?>>Inactive</option>
                </select>
            </div>
            <div class="col-md-3">
                <button class="btn btn-primary rounded-pill w-100 font-outfit fw-bold"><i class="fa-solid fa-filter me-2"></i>Apply Filters</button>
            </div>
        </form>
    </div>

    <!-- University Cards Grid -->
    <?php if (empty($universities)): ?>
        <div class="text-center py-5">
            <div class="text-6xl mb-3">🏫</div>
            <p class="text-muted">No universities found. <a href="<?= APP_URL ?>/admin/universities/create">Add the first one.</a></p>
        </div>
    <?php else: ?>
        <div class="row g-4">
            <?php foreach ($universities as $uni): ?>
                <div class="col-lg-4 col-md-6">
                    <div class="card border-0 shadow-sm h-100 hover-lift">
                        <?php if (!empty($uni['banner'])): ?>
                            <div class="rounded-top overflow-hidden" style="height:120px;">
                                <img src="<?= UPLOAD_URL ?>/banners/<?= htmlspecialchars($uni['banner']) ?>" alt="<?= htmlspecialchars($uni['name']) ?>" class="w-100 h-100" style="object-fit:cover;">
                            </div>
                        <?php else: ?>
                            <div class="rounded-top" style="height:80px;background:linear-gradient(135deg,#0d6efd20,#6610f230);"></div>
                        <?php endif; ?>
                        <div class="card-body p-4">
                            <div class="d-flex align-items-start gap-3 mb-3">
                                <?php if (!empty($uni['logo'])): ?>
                                    <img src="<?= logoUrl($uni['logo']) ?>" alt="Logo" class="rounded-2 border" width="44" height="44" style="object-fit:contain;background:#fff;">
                                <?php else: ?>
                                    <div class="badge-icon-box bg-primary-subtle text-primary rounded-2 flex-shrink-0" style="width:44px;height:44px;display:flex;align-items:center;justify-content:center;">
                                        <i class="fa-solid fa-building-columns"></i>
                                    </div>
                                <?php endif; ?>
                                <div class="flex-grow-1 min-w-0">
                                    <h6 class="fw-bold font-outfit text-dark mb-0 text-truncate"><?= htmlspecialchars($uni['name']) ?></h6>
                                    <small class="text-muted"><i class="fa-solid fa-location-dot me-1"></i><?= htmlspecialchars($uni['city'] ?? '') ?></small>
                                </div>
                                <span class="badge bg-<?= $uni['status'] === 'active' ? 'primary' : 'info' ?>-subtle text-<?= $uni['status'] === 'active' ? 'primary' : 'info' ?> rounded-pill text-xs flex-shrink-0">
                                    <?= $uni['status'] === 'active' ? 'Active' : 'Hidden' ?>
                                </span>
                            </div>
                            <div class="d-flex flex-wrap gap-2 mb-3">
                                <span class="badge bg-primary-subtle text-primary rounded-pill text-xs font-outfit"><?= htmlspecialchars($uni['university_type'] ?? 'University') ?></span>
                                <span class="badge bg-info-subtle text-info rounded-pill text-xs font-outfit"><i class="fa-solid fa-list me-1"></i><?= $uni['program_count'] ?? 0 ?> programmes</span>
                                <span class="badge bg-warning-subtle text-warning rounded-pill text-xs font-outfit"><i class="fa-solid fa-graduation-cap me-1"></i><?= $uni['scholarship_count'] ?? 0 ?> scholarships</span>
                            </div>
                        </div>
                        <div class="card-footer bg-white border-0 px-4 pb-4 pt-0 d-flex gap-2">
                            <a href="<?= APP_URL ?>/admin/universities/<?= $uni['id'] ?>/edit" class="btn btn-outline-primary btn-sm rounded-pill flex-grow-1 font-outfit fw-bold">
                                <i class="fa-solid fa-pen me-1"></i>Edit
                            </a>
                            <a href="<?= APP_URL ?>/admin/universities/<?= $uni['id'] ?>/programmes" class="btn btn-outline-info btn-sm rounded-pill px-2" title="Manage Programmes">
                                <i class="fa-solid fa-list-check"></i>
                            </a>
                            <a href="<?= APP_URL ?>/universities/<?= $uni['slug'] ?>" target="_blank" class="btn btn-outline-secondary btn-sm rounded-pill px-2" title="Preview">
                                <i class="fa-solid fa-eye"></i>
                            </a>
                            <form action="<?= APP_URL ?>/admin/universities/<?= $uni['id'] ?>/delete" method="POST" onsubmit="return confirm('Delete this university?')">
                                <?= csrf_field() ?><input type="hidden" name="_method" value="DELETE">
                                <button class="btn btn-outline-danger btn-sm rounded-pill px-2" title="Delete"><i class="fa-solid fa-trash"></i></button>
                            </form>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <!-- Pagination -->
        <?php if (($totalPages ?? 1) > 1): ?>
            <nav class="mt-4 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 ?>/admin/universities?page=<?= $p ?>&search=<?= urlencode($search ?? '') ?>&type=<?= urlencode($selectedType ?? '') ?>"><?= $p ?></a>
                        </li>
                    <?php endfor; ?>
                </ul>
            </nav>
        <?php endif; ?>
    <?php endif; ?>
</div>
