<?php
session_start();
require_once('../config.php');

if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'lecturer') {
    header("Location: dashboard.php");
    exit();
}

include('../header/header.php');

$user_id = $_SESSION['user_id'];

$stmt = $pdo->prepare("
    SELECT DISTINCT u.id, u.username, u.email 
    FROM users u
    JOIN enrollments e ON u.id = e.student_id
    JOIN courses c ON e.course_id = c.id
    WHERE c.lecturer_id = ? AND e.status = 'enrolled'
    ORDER BY u.username
");
$stmt->execute([$user_id]);
$students = $stmt->fetchAll();

$student_courses = [];
foreach ($students as $student) {
    $stmt = $pdo->prepare("
        SELECT c.*, e.progress, e.enrolled_at
        FROM courses c
        JOIN enrollments e ON c.id = e.course_id
        WHERE c.lecturer_id = ? AND e.student_id = ? AND e.status = 'enrolled'
        ORDER BY c.course_name
    ");
    $stmt->execute([$user_id, $student['id']]);
    $student_courses[$student['id']] = $stmt->fetchAll();
}
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold">My Students</h2>
        <a href="dashboard.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left me-2"></i>Back</a>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body">
            <?php if (empty($students)): ?>
                <div class="text-center py-5">
                    <i class="fas fa-user-graduate fa-3x text-muted mb-3"></i>
                    <h4 class="text-muted">No students yet</h4>
                    <p class="text-muted">Your students will appear here once they enroll in your courses.</p>
                </div>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="bg-light">
                            <tr>
                                <th class="ps-4">Student</th>
                                <th>Email</th>
                                <th>Courses Enrolled</th>
                                <th class="text-end pe-4">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($students as $student): ?>
                                <tr>
                                    <td class="ps-4">
                                        <div class="d-flex align-items-center">
                                            <div class="rounded-circle bg-yellowish d-flex align-items-center justify-content-center me-3 shadow-sm" style="width:40px;height:40px;">
                                                <i class="fas fa-user text-primary"></i>
                                            </div>
                                            <div class="fw-bold"><?= htmlspecialchars($student['username']); ?></div>
                                        </div>
                                    </td>
                                    <td>
                                        <?= htmlspecialchars($student['email']); ?>
                                    </td>
                                    <td>
                                        <span class="badge bg-primary"><?= count($student_courses[$student['id']]); ?> Course<?= count($student_courses[$student['id']]) != 1 ? 's' : '' ?></span>
                                    </td>
                                    <td class="text-end pe-4">
                                        <button class="btn btn-sm btn-outline-primary" type="button" data-bs-toggle="collapse" data-bs-target="#student-<?= $student['id']; ?>">
                                            <i class="fas fa-eye me-1"></i> View Courses
                                        </button>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="4" class="p-0 border-top-0">
                                        <div class="collapse" id="student-<?= $student['id']; ?>">
                                            <div class="p-4 bg-light">
                                                <h6 class="fw-bold mb-3">Enrolled Courses:</h6>
                                                <div class="row g-3">
                                                    <?php foreach ($student_courses[$student['id']] as $course): ?>
                                                        <div class="col-md-6">
                                                            <div class="card h-100 border-0 shadow-sm">
                                                                <div class="card-body">
                                                                    <h6 class="fw-bold mb-2"><?= htmlspecialchars($course['course_name']); ?></h6>
                                                                    <div class="mb-2">
                                                                        <small class="text-muted">Progress:</small>
                                                                        <div class="progress mt-1" style="height: 6px;">
                                                                            <div class="progress-bar" role="progressbar" style="width: <?= $course['progress']; ?>%" aria-valuenow="<?= $course['progress']; ?>" aria-valuemin="0" aria-valuemax="100"></div>
                                                                        </div>
                                                                        <small class="text-muted"><?= $course['progress']; ?>%</small>
                                                                    </div>
                                                                    <small class="text-muted">Enrolled: <?= date('M d, Y', strtotime($course['enrolled_at'])); ?></small>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    <?php endforeach; ?>
                                                </div>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php include('../footer/footer.php'); ?>
