<?php 
include 'includes/header.php'; 
include 'includes/sidebar.php'; 

$teacher_id = $_SESSION['teacher_id'];

// Fetch students enrolled in teacher's courses
try {
    $stmt = $pdo->prepare("SELECT s.*, c.title as course_name FROM students s 
                           JOIN courses c ON s.course_id = c.id 
                           WHERE c.teacher_id = ? ORDER BY s.name ASC");
    $stmt->execute([$teacher_id]);
    $students = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { $students = []; }
?>

<style>
    .dashboard-header { background: #fff; padding: 20px 30px; border-radius: 15px; box-shadow: 0 5px 20px rgba(0,0,0,0.03); margin-bottom: 25px; border-left: 6px solid var(--theme-green); }
    .custom-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 18px; box-shadow: 0 10px 40px rgba(0,0,0,0.03); overflow: hidden; border-top: 5px solid var(--theme-green); }
    
    .table thead th { border: none; padding: 18px 20px; color: #64748b; font-weight: 800; text-transform: uppercase; font-size: 11px; letter-spacing: 0.5px; background-color: var(--bg-soft); border-bottom: 1px solid #e2e8f0; }
    .table tbody tr:hover { background-color: rgba(6, 78, 59, 0.02); }
    
    .std-avatar { width: 45px; height: 45px; border-radius: 12px; background: rgba(6, 78, 59, 0.08); color: var(--theme-green); display: flex; align-items: center; justify-content: center; font-weight: 800; border: 1px solid rgba(6, 78, 59, 0.15); overflow: hidden; }
    
    #sidebarToggle { width: 42px !important; height: 42px !important; padding: 0; display: inline-flex !important; align-items: center; justify-content: center; flex-shrink: 0; border-radius: 8px; border: 1px solid #e2e8f0; background: #fff; }

    @media (max-width: 991px) {
        .dashboard-header { flex-direction: column; align-items: flex-start !important; gap: 15px; padding: 15px 20px; }
        .dashboard-header .d-flex { width: 100%; align-items: center; }
    }
</style>

<div class="main-content">
    <div class="dashboard-header d-flex justify-content-between align-items-center fade-up">
        <div class="d-flex align-items-center">
            <button class="btn btn-light border d-lg-none me-3 shadow-sm" id="sidebarToggle"><i class="fas fa-bars text-dark"></i></button>
            <div>
                <h4 class="mb-0 fw-bold text-dark"><i class="fas fa-user-graduate me-2" style="color: var(--theme-green);"></i> My Students</h4>
                <p class="text-muted small mb-0 fw-bold d-none d-md-block">List of all students enrolled in your assigned courses.</p>
            </div>
        </div>
    </div>

    <div class="custom-card fade-up" style="animation-delay: 0.1s;">
        <div class="table-responsive">
            <table class="table align-middle border-0 mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">Student Info</th>
                        <th>Enrolled Course</th>
                        <th>Contact No.</th>
                        <th>Join Date</th>
                        <th class="text-end pe-4">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($students)): foreach($students as $std): ?>
                    <tr>
                        <td class="ps-4 py-3">
                            <div class="d-flex align-items-center">
                                <div class="std-avatar me-3">
                                    <?php if(!empty($std['profile_photo'])): ?>
                                        <img src="../<?= $std['profile_photo'] ?>" style="width:100%; height:100%; object-fit:cover;">
                                    <?php else: ?>
                                        <?= strtoupper(substr($std['name'], 0, 1)) ?>
                                    <?php endif; ?>
                                </div>
                                <div>
                                    <div class="fw-bold text-dark"><?= htmlspecialchars($std['name']) ?></div>
                                    <small class="text-muted fw-bold"><?= htmlspecialchars($std['email']) ?></small>
                                </div>
                            </div>
                        </td>
                        <td>
                            <span class="badge bg-light text-muted border px-3 py-2 rounded-pill fw-bold" style="font-size: 11px;">
                                <i class="fas fa-book me-1" style="color: var(--theme-gold);"></i> <?= htmlspecialchars($std['course_name']) ?>
                            </span>
                        </td>
                        <td><div class="fw-bold small" style="color: var(--theme-green);"><i class="fab fa-whatsapp me-1"></i> <?= htmlspecialchars($std['phone']) ?></div></td>
                        <td><div class="small text-muted fw-bold"><?= date('d M, Y', strtotime($std['created_at'])) ?></div></td>
                        <td class="text-end pe-4">
                            <a href="view_student.php?id=<?= $std['id'] ?>" class="btn btn-sm btn-light border shadow-sm text-dark fw-bold px-3 rounded-pill" style="font-size: 11px;">Profile</a>
                        </td>
                    </tr>
                    <?php endforeach; else: ?>
                    <tr><td colspan="5" class="text-center py-5 fw-bold text-muted">No students enrolled yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'includes/footer.php'; ?>