<?php
/**
 * Admin Mentors Management View
 * Lists all mentors with approval status, specialty, rating, and controls.
 */
?>
<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-user-tie text-success me-2"></i>Manage Mentors</h2>
            <p class="text-muted mb-0"><?= number_format($pagination['total'] ?? 0) ?> mentors registered</p>
        </div>
    </div>

    <!-- Filters -->
    <div class="card border-0 shadow-sm p-4 mb-4">
        <form action="<?= APP_URL ?>/admin/mentors" 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="Name, expertise, university..." value="<?= htmlspecialchars($search ?? '') ?>">
                </div>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Approval</label>
                <select name="approval" class="form-select">
                    <option value="">All</option>
                    <option value="approved" <?= ($selectedApproval ?? '') === 'approved' ? 'selected' : '' ?>>Approved</option>
                    <option value="pending" <?= ($selectedApproval ?? '') === 'pending' ? 'selected' : '' ?>>Pending</option>
                    <option value="rejected" <?= ($selectedApproval ?? '') === 'rejected' ? 'selected' : '' ?>>Rejected</option>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label text-muted text-xs font-outfit fw-bold">Availability</label>
                <select name="available" class="form-select">
                    <option value="">All</option>
                    <option value="1" <?= ($selectedAvailable ?? '') === '1' ? 'selected' : '' ?>>Available</option>
                    <option value="0" <?= ($selectedAvailable ?? '') === '0' ? 'selected' : '' ?>>Not Available</option>
                </select>
            </div>
            <div class="col-md-2">
                <button class="btn btn-success rounded-pill w-100 font-outfit fw-bold"><i class="fa-solid fa-filter me-2"></i>Filter</button>
            </div>
        </form>
    </div>

    <!-- Mentor Cards Grid -->
    <?php if (empty($mentors)): ?>
        <div class="text-center py-5">
            <div class="text-6xl mb-3">👔</div>
            <p class="text-muted">No mentors found matching your criteria.</p>
        </div>
    <?php else: ?>
        <div class="row g-4">
            <?php foreach ($mentors as $mentor): 
                $approvalStatus = !empty($mentor['verified']) ? 'approved' : 'pending';
                $approvalColors = ['approved' => 'success', 'pending' => 'warning', 'rejected' => 'danger'];
                $approvalColor = $approvalColors[$approvalStatus] ?? 'secondary';
            ?>
                <div class="col-md-4 col-sm-6">
                    <div class="card border-0 shadow-sm hover-lift h-100">
                        <div class="card-body p-4">
                            <div class="d-flex align-items-start gap-3 mb-3">
                                <div class="position-relative">
                                    <img src="<?= !empty($mentor['avatar']) ? htmlspecialchars($mentor['avatar']) : 'https://api.dicebear.com/7.x/initials/svg?seed=' . urlencode($mentor['first_name'] . ' ' . $mentor['last_name']) . '&backgroundColor=1a2940' ?>" 
                                         alt="<?= htmlspecialchars($mentor['first_name'] . ' ' . $mentor['last_name']) ?>" 
                                         class="rounded-circle" width="52" height="52" style="object-fit:cover;">
                                    <span class="position-absolute bottom-0 end-0 badge bg-<?= !empty($mentor['is_available']) ? 'success' : 'secondary' ?> rounded-circle p-1" style="width:14px;height:14px;"></span>
                                </div>
                                <div class="flex-grow-1 min-w-0">
                                    <h6 class="fw-bold font-outfit text-dark mb-0 text-truncate"><?= htmlspecialchars($mentor['first_name'] . ' ' . $mentor['last_name']) ?></h6>
                                    <small class="text-muted text-truncate d-block"><?= htmlspecialchars($mentor['qualification'] ?? 'Mentor') ?></small>
                                    <div class="d-flex align-items-center gap-1 mt-1">
                                        <span class="badge bg-<?= $approvalColor ?>-subtle text-<?= $approvalColor ?> rounded-pill text-xs font-outfit"><?= ucfirst($approvalStatus) ?></span>
                                    </div>
                                </div>
                            </div>
                            <div class="d-flex flex-wrap gap-2 mb-3">
                                <?php if (!empty($mentor['university_name'])): ?>
                                    <span class="badge bg-info-subtle text-info rounded-pill text-xs"><i class="fa-solid fa-building-columns me-1"></i><?= htmlspecialchars($mentor['university_name']) ?></span>
                                <?php endif; ?>
                                <span class="badge bg-primary-subtle text-primary rounded-pill text-xs"><i class="fa-solid fa-users me-1"></i><?= $mentor['sessions_completed'] ?? 0 ?> sessions</span>
                                <?php if (!empty($mentor['avg_rating'])): ?>
                                    <span class="badge bg-warning-subtle text-warning rounded-pill text-xs"><i class="fa-solid fa-star me-1"></i><?= number_format($mentor['avg_rating'], 1) ?></span>
                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="card-footer bg-white border-0 px-4 pb-4 pt-0 d-flex gap-2 flex-wrap">
                            <a href="<?= APP_URL ?>/admin/users/<?= $mentor['user_id'] ?>" class="btn btn-outline-primary btn-sm rounded-pill flex-grow-1 font-outfit fw-bold">View Profile</a>
                            <?php if (empty($mentor['verified'])): ?>
                                <form action="<?= APP_URL ?>/admin/users/<?= $mentor['user_id'] ?>/approve" method="POST">
                                    <?= csrf_field() ?>
                                    <button class="btn btn-success btn-sm rounded-pill px-3 font-outfit fw-bold"><i class="fa-solid fa-check me-1"></i>Approve</button>
                                </form>
                            <?php endif; ?>
                            <form action="<?= APP_URL ?>/admin/users/<?= $mentor['user_id'] ?>/toggle" method="POST">
                                <?= csrf_field() ?>
                                <button class="btn btn-outline-<?= ($mentor['user_status'] === 'active') ? 'warning' : 'success' ?> btn-sm rounded-pill px-2" title="<?= ($mentor['user_status'] === 'active') ? 'Deactivate' : 'Activate' ?>">
                                    <i class="fa-solid fa-<?= ($mentor['user_status'] === 'active') ? 'ban' : 'circle-check' ?>"></i>
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>

    <!-- 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/mentors?page=<?= $p ?>&search=<?= urlencode($search ?? '') ?>&approval=<?= urlencode($selectedApproval ?? '') ?>"><?= $p ?></a>
                    </li>
                <?php endfor; ?>
            </ul>
        </nav>
    <?php endif; ?>
</div>
