<?php
include '../config.php';

if (!isGuru()) {
    redirect('../login.php');
}

$guru_id = $_SESSION['user_id'];

// Get course ID from URL
$course_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($course_id <= 0) {
    $_SESSION['error'] = "Kursus tidak valid";
    redirect('my_courses.php');
}

// Get course details
$sql_course = "SELECT c.*, cat.nama_kategori, u.nama_lengkap as nama_guru, u.foto_profil as foto_guru
               FROM lms_courses c 
               LEFT JOIN lms_categories cat ON c.id_kategori = cat.id 
               LEFT JOIN lms_users u ON c.id_guru = u.id 
               WHERE c.id = ? AND c.id_guru = ?";
$stmt_course = $conn->prepare($sql_course);
$stmt_course->bind_param('ii', $course_id, $guru_id);
$stmt_course->execute();
$course = $stmt_course->get_result()->fetch_assoc();

if (!$course) {
    $_SESSION['error'] = "Kursus tidak ditemukan atau Anda tidak memiliki akses";
    redirect('my_courses.php');
}

// Get course statistics
$sql_stats = "SELECT 
    COUNT(DISTINCT e.id_siswa) as total_students,
    AVG(e.progress) as avg_progress,
    COUNT(DISTINCT l.id) as total_lms_lessons,
    SUM(l.durasi_menit) as total_minutes,
    COUNT(DISTINCT q.id) as total_lms_quizzes
    FROM lms_courses c
    LEFT JOIN lms_enrollments e ON c.id = e.id_course
    LEFT JOIN lms_lessons l ON c.id = l.id_course
    LEFT JOIN lms_quizzes q ON l.id = q.id_lesson
    WHERE c.id = ?";
$stmt_stats = $conn->prepare($sql_stats);
$stmt_stats->bind_param('i', $course_id);
$stmt_stats->execute();
$stats = $stmt_stats->get_result()->fetch_assoc();

// Get course lms_lessons with their types
$sql_lms_lessons = "SELECT l.*, 
                COUNT(q.id) as quiz_count,
                (SELECT COUNT(*) FROM lms_questions q2 
                 JOIN lms_quizzes qz ON q2.id_quiz = qz.id 
                 WHERE qz.id_lesson = l.id) as total_lms_questions
                FROM lms_lessons l 
                LEFT JOIN lms_quizzes q ON l.id = q.id_lesson
                WHERE l.id_course = ? 
                GROUP BY l.id 
                ORDER BY l.urutan ASC";
$stmt_lms_lessons = $conn->prepare($sql_lms_lessons);
$stmt_lms_lessons->bind_param('i', $course_id);
$stmt_lms_lessons->execute();
$lms_lessons = $stmt_lms_lessons->get_result();

// Get recent reviews (if any)
$sql_reviews = "SELECT e.*, u.nama_lengkap, u.foto_profil 
               FROM lms_enrollments e 
               LEFT JOIN lms_users u ON e.id_siswa = u.id 
               WHERE e.id_course = ? AND e.progress >= 50 
               ORDER BY e.tanggal_enroll DESC 
               LIMIT 3";
$stmt_reviews = $conn->prepare($sql_reviews);
$stmt_reviews->bind_param('i', $course_id);
$stmt_reviews->execute();
$reviews = $stmt_reviews->get_result();

// Calculate total course duration in hours
$total_hours = ceil(($stats['total_minutes'] ?? 0) / 60);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($course['judul']); ?> - SMANSAB LMS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        :root {
            --primary-color: #3f51b5;
            --secondary-color: #f5f5f5;
        }
        
        .sidebar {
            background: var(--primary-color);
            color: white;
            min-height: 100vh;
            padding: 0;
        }
        
        .sidebar-sticky {
            position: sticky;
            top: 0;
            height: 100vh;
            padding-top: 20px;
            overflow-y: auto;
        }
        
        .sidebar .nav-link {
            color: rgba(255, 255, 255, 0.8);
            padding: 12px 20px;
            margin: 2px 0;
            border-radius: 0;
            transition: all 0.3s;
        }
        
        .sidebar .nav-link:hover {
            color: white;
            background: rgba(255, 255, 255, 0.1);
        }
        
        .sidebar .nav-link.active {
            color: white;
            background: rgba(255, 255, 255, 0.2);
            border-left: 4px solid #fff;
        }
        
        .sidebar .nav-link i {
            width: 20px;
            margin-right: 10px;
        }
        
        .main-content {
            background: #f8f9fa;
            min-height: 100vh;
        }
        
        .navbar-custom {
            background: white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .course-hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 60px 0;
            margin-bottom: 40px;
        }
        
        .course-content {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }
        
        .stats-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            border: none;
        }
        
        .lesson-item {
            border: 1px solid #e9ecef;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 15px;
            transition: all 0.3s;
        }
        
        .lesson-item:hover {
            border-color: var(--primary-color);
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        
        .lesson-icon {
            width: 50px;
            height: 50px;
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.5rem;
            margin-right: 15px;
        }
        
        .icon-video { background: #e3f2fd; color: #1976d2; }
        .icon-text { background: #e8f5e8; color: #388e3c; }
        .icon-quiz { background: #fff3e0; color: #f57c00; }
        .icon-assignment { background: #fce4ec; color: #c2185b; }
        
        .progress-course {
            height: 8px;
            border-radius: 4px;
        }
        
        .level-badge {
            font-size: 0.7rem;
            padding: 4px 8px;
        }
        
        .bg-beginner { background: #28a745 !important; }
        .bg-intermediate { background: #ffc107 !important; color: #000 !important; }
        .bg-advanced { background: #dc3545 !important; }
        
        .review-card {
            border: 1px solid #e9ecef;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 15px;
        }
        
        .instructor-avatar {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            object-fit: cover;
            border: 3px solid white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .price-tag {
            font-size: 2rem;
            font-weight: bold;
            color: var(--primary-color);
        }
        
        .course-meta {
            background: rgba(255,255,255,0.1);
            border-radius: 10px;
            padding: 20px;
            backdrop-filter: blur(10px);
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-3 col-lg-2 sidebar">
                <div class="sidebar-sticky">
                    <div class="text-center mb-4">
                        <h4 class="fw-bold">
                            <i class="bi bi-book-half"></i><br>
                            SMANSAB LMS
                        </h4>
                        <small class="text-white-50">Guru Dashboard</small>
                    </div>
                    
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="dashboard.php">
                                <i class="bi bi-speedometer2"></i> Dashboard
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="my_courses.php">
                                <i class="bi bi-journal-bookmark"></i> Kursus Saya
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="manage_lessons.php?course_id=<?php echo $course_id; ?>">
                                <i class="bi bi-collection-play"></i> Kelola Materi
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="manage_quizzes.php?course_id=<?php echo $course_id; ?>">
                                <i class="bi bi-pencil-square"></i> Kelola Kuis
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="students_progress.php?course_id=<?php echo $course_id; ?>">
                                <i class="bi bi-graph-up"></i> Progress Siswa
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="course_management.php?id=<?php echo $course_id; ?>">
                                <i class="bi bi-gear"></i> Kelola Kursus
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="course_detail.php?id=<?php echo $course_id; ?>">
                                <i class="bi bi-eye"></i> Preview Kursus
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="profile.php">
                                <i class="bi bi-person"></i> Profil Saya
                            </a>
                        </li>
                        <li class="nav-item mt-4">
                            <a class="nav-link" href="../logout.php">
                                <i class="bi bi-box-arrow-right"></i> Logout
                            </a>
                        </li>
                    </ul>
                    
                    <div class="mt-5 px-3">
                        <small class="text-white-50">
                            <i class="bi bi-info-circle"></i> Butuh bantuan?
                            <a href="#" class="text-white">Hubungi Admin</a>
                        </small>
                    </div>
                </div>
            </div>

            <!-- Main Content -->
            <div class="col-md-9 col-lg-10 main-content">
                <!-- Top Navbar -->
                <nav class="navbar navbar-custom">
                    <div class="container-fluid">
                        <div class="d-flex align-items-center">
                            <button class="btn btn-outline-primary me-3 d-md-none" type="button">
                                <i class="bi bi-list"></i>
                            </button>
                            <h5 class="mb-0">Preview Kursus</h5>
                        </div>
                        <div class="d-flex align-items-center">
                            <div class="dropdown">
                                <button class="btn btn-outline-primary dropdown-toggle" type="button" 
                                        data-bs-toggle="dropdown">
                                    <i class="bi bi-person-circle me-2"></i>
                                    <?php echo $_SESSION['nama_lengkap']; ?>
                                </button>
                                <ul class="dropdown-menu">
                                    <li><a class="dropdown-item" href="profile.php">
                                        <i class="bi bi-person me-2"></i>Profil
                                    </a></li>
                                    <li><a class="dropdown-item" href="my_courses.php">
                                        <i class="bi bi-journal-bookmark me-2"></i>Kursus Saya
                                    </a></li>
                                    <li><hr class="dropdown-divider"></li>
                                    <li><a class="dropdown-item" href="../logout.php">
                                        <i class="bi bi-box-arrow-right me-2"></i>Logout
                                    </a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </nav>

                <div class="container-fluid">
                    <!-- Course Hero Section -->
                    <div class="course-hero">
                        <div class="container">
                            <nav aria-label="breadcrumb">
                                <ol class="breadcrumb breadcrumb-dark">
                                    <li class="breadcrumb-item"><a href="my_courses.php" class="text-white-50">Kursus Saya</a></li>
                                    <li class="breadcrumb-item active text-white">Preview</li>
                                </ol>
                            </nav>
                            
                            <div class="row align-items-center">
                                <div class="col-lg-8">
                                    <div class="d-flex flex-wrap gap-2 mb-3">
                                        <span class="badge bg-light text-dark">
                                            <i class="bi bi-tag me-1"></i><?php echo $course['nama_kategori']; ?>
                                        </span>
                                        <span class="badge <?php echo 'bg-' . ($course['level'] === 'pemula' ? 'beginner' : ($course['level'] === 'menengah' ? 'intermediate' : 'advanced')); ?> level-badge">
                                            <i class="bi bi-bar-chart me-1"></i><?php echo ucfirst($course['level']); ?>
                                        </span>
                                        <span class="badge bg-light text-dark">
                                            <i class="bi bi-star-fill me-1 text-warning"></i><?php echo $course['rating']; ?>
                                        </span>
                                    </div>
                                    
                                    <h1 class="display-5 fw-bold mb-3"><?php echo htmlspecialchars($course['judul']); ?></h1>
                                    <p class="lead mb-4 opacity-75"><?php echo htmlspecialchars($course['deskripsi']); ?></p>
                                    
                                    <div class="course-meta">
                                        <div class="row text-center">
                                            <div class="col-md-3 col-6 mb-3">
                                                <div class="text-white">
                                                    <i class="bi bi-people display-6 mb-2"></i>
                                                    <h4 class="fw-bold"><?php echo $stats['total_students'] ?? 0; ?></h4>
                                                    <small>Siswa</small>
                                                </div>
                                            </div>
                                            <div class="col-md-3 col-6 mb-3">
                                                <div class="text-white">
                                                    <i class="bi bi-collection-play display-6 mb-2"></i>
                                                    <h4 class="fw-bold"><?php echo $stats['total_lms_lessons'] ?? 0; ?></h4>
                                                    <small>Materi</small>
                                                </div>
                                            </div>
                                            <div class="col-md-3 col-6 mb-3">
                                                <div class="text-white">
                                                    <i class="bi bi-clock display-6 mb-2"></i>
                                                    <h4 class="fw-bold"><?php echo $total_hours; ?>h</h4>
                                                    <small>Durasi</small>
                                                </div>
                                            </div>
                                            <div class="col-md-3 col-6 mb-3">
                                                <div class="text-white">
                                                    <i class="bi bi-graph-up display-6 mb-2"></i>
                                                    <h4 class="fw-bold"><?php echo number_format($stats['avg_progress'] ?? 0, 1); ?>%</h4>
                                                    <small>Progress</small>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-4 text-center">
                                    <div class="bg-white rounded-3 p-4 shadow">
                                        <?php if ($course['harga'] > 0): ?>
                                            <div class="price-tag mb-3">
                                                Rp <?php echo number_format($course['harga'], 0, ',', '.'); ?>
                                            </div>
                                        <?php else: ?>
                                            <div class="price-tag text-success mb-3">
                                                Gratis
                                            </div>
                                        <?php endif; ?>
                                        
                                        <div class="d-grid gap-2">
                                            <a href="course_management.php?id=<?php echo $course_id; ?>" class="btn btn-primary btn-lg">
                                                <i class="bi bi-gear me-2"></i>Kelola Kursus
                                            </a>
                                            <a href="manage_lessons.php?course_id=<?php echo $course_id; ?>" class="btn btn-outline-primary">
                                                <i class="bi bi-plus-circle me-2"></i>Tambah Materi
                                            </a>
                                            <a href="my_courses.php" class="btn btn-outline-secondary">
                                                <i class="bi bi-arrow-left me-2"></i>Kembali
                                            </a>
                                        </div>
                                        
                                        <div class="mt-3 small text-muted">
                                            <i class="bi bi-shield-check me-1"></i>
                                            Preview mode - Guru
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="container">
                        <div class="row">
                            <!-- Main Content -->
                            <div class="col-lg-8">
                                <!-- Course Content Section -->
                                <div class="course-content">
                                    <h3 class="fw-bold mb-4">
                                        <i class="bi bi-list-check me-2"></i>Materi Pembelajaran
                                    </h3>
                                    
                                    <?php if ($lms_lessons->num_rows > 0): ?>
                                        <div class="lms_lessons-list">
                                            <?php 
                                            $lesson_number = 1;
                                            while($lesson = $lms_lessons->fetch_assoc()): 
                                                $icon_class = [
                                                    'video' => 'icon-video bi-camera-video',
                                                    'teks' => 'icon-text bi-file-text',
                                                    'quiz' => 'icon-quiz bi-pencil-square',
                                                    'tugas' => 'icon-assignment bi-file-earmark-text'
                                                ];
                                            ?>
                                                <div class="lesson-item">
                                                    <div class="d-flex align-items-start">
                                                        <div class="lesson-icon <?php echo str_replace('icon-', '', $icon_class[$lesson['tipe_lesson']]); ?>">
                                                            <i class="<?php echo $icon_class[$lesson['tipe_lesson']]; ?>"></i>
                                                        </div>
                                                        <div class="flex-grow-1">
                                                            <div class="d-flex justify-content-between align-items-start mb-2">
                                                                <h5 class="fw-bold mb-1">
                                                                    <?php echo $lesson_number; ?>. <?php echo htmlspecialchars($lesson['judul_lesson']); ?>
                                                                </h5>
                                                                <small class="text-muted">
                                                                    <?php echo $lesson['durasi_menit']; ?> menit
                                                                </small>
                                                            </div>
                                                            <p class="text-muted mb-2 small">
                                                                <?php 
                                                                if (strlen($lesson['konten']) > 150) {
                                                                    echo substr(strip_tags($lesson['konten']), 0, 150) . '...';
                                                                } else {
                                                                    echo strip_tags($lesson['konten']);
                                                                }
                                                                ?>
                                                            </p>
                                                            
                                                            <?php if ($lesson['tipe_lesson'] === 'quiz' && $lesson['quiz_count'] > 0): ?>
                                                                <div class="d-flex align-items-center gap-3">
                                                                    <span class="badge bg-warning text-dark">
                                                                        <i class="bi bi-question-circle me-1"></i>
                                                                        <?php echo $lesson['total_lms_questions']; ?> Soal
                                                                    </span>
                                                                    <small class="text-muted">
                                                                        Kuis
                                                                    </small>
                                                                </div>
                                                            <?php else: ?>
                                                                <small class="text-muted text-uppercase">
                                                                    <?php echo $lesson['tipe_lesson']; ?>
                                                                </small>
                                                            <?php endif; ?>
                                                        </div>
                                                    </div>
                                                </div>
                                                <?php $lesson_number++; ?>
                                            <?php endwhile; ?>
                                        </div>
                                    <?php else: ?>
                                        <div class="text-center py-5">
                                            <i class="bi bi-collection-play display-1 text-muted"></i>
                                            <h4 class="text-muted mt-3">Belum ada materi</h4>
                                            <p class="text-muted mb-4">Mulai dengan menambahkan materi pertama untuk kursus ini</p>
                                            <a href="manage_lessons.php?course_id=<?php echo $course_id; ?>" class="btn btn-primary">
                                                <i class="bi bi-plus-circle me-2"></i>Tambah Materi Pertama
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                </div>

                                <!-- Course Description -->
                                <div class="course-content">
                                    <h3 class="fw-bold mb-4">
                                        <i class="bi bi-journal-text me-2"></i>Tentang Kursus Ini
                                    </h3>
                                    <div class="course-description">
                                        <?php echo nl2br(htmlspecialchars($course['deskripsi'])); ?>
                                    </div>
                                    
                                    <div class="mt-4">
                                        <h5 class="fw-bold mb-3">Yang akan Anda pelajari:</h5>
                                        <div class="row">
                                            <div class="col-md-6">
                                                <ul class="list-unstyled">
                                                    <li class="mb-2"><i class="bi bi-check-circle-fill text-success me-2"></i>Dasar-dasar pemrograman</li>
                                                    <li class="mb-2"><i class="bi bi-check-circle-fill text-success me-2"></i>Konsep OOP yang solid</li>
                                                    <li class="mb-2"><i class="bi bi-check-circle-fill text-success me-2"></i>Project-based learning</li>
                                                </ul>
                                            </div>
                                            <div class="col-md-6">
                                                <ul class="list-unstyled">
                                                    <li class="mb-2"><i class="bi bi-check-circle-fill text-success me-2"></i>Best practices coding</li>
                                                    <li class="mb-2"><i class="bi bi-check-circle-fill text-success me-2"></i>Debugging techniques</li>
                                                    <li class="mb-2"><i class="bi bi-check-circle-fill text-success me-2"></i>Portfolio building</li>
                                                </ul>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- Sidebar -->
                            <div class="col-lg-4">
                                <!-- Instructor Card -->
                                <div class="stats-card">
                                    <h5 class="fw-bold mb-4">Tentang Pengajar</h5>
                                    <div class="text-center">
                                        <?php if (!empty($course['foto_guru'])): ?>
                                            <img src="../uploads/<?php echo $course['foto_guru']; ?>" 
                                                 alt="<?php echo htmlspecialchars($course['nama_guru']); ?>" 
                                                 class="instructor-avatar mb-3">
                                        <?php else: ?>
                                            <div class="instructor-avatar bg-primary text-white d-inline-flex align-items-center justify-content-center mb-3">
                                                <i class="bi bi-person-fill display-6"></i>
                                            </div>
                                        <?php endif; ?>
                                        <h6 class="fw-bold"><?php echo $course['nama_guru']; ?></h6>
                                        <p class="text-muted small mb-3">Pengajar Profesional</p>
                                        <div class="d-flex justify-content-center gap-2 mb-3">
                                            <span class="badge bg-primary">
                                                <i class="bi bi-star me-1"></i>4.8 Rating
                                            </span>
                                            <span class="badge bg-success">
                                                <i class="bi bi-people me-1"></i><?php echo $stats['total_students'] ?? 0; ?> Siswa
                                            </span>
                                        </div>
                                        <p class="small text-muted">
                                            Pengajar berpengalaman dengan passion dalam membagikan pengetahuan dan membantu siswa mencapai potensi terbaik mereka.
                                        </p>
                                    </div>
                                </div>

                                <!-- Course Stats -->
                                <div class="stats-card">
                                    <h5 class="fw-bold mb-4">Statistik Kursus</h5>
                                    <div class="row text-center">
                                        <div class="col-6 mb-3">
                                            <div class="text-primary">
                                                <i class="bi bi-people display-6 mb-2"></i>
                                                <h5 class="fw-bold"><?php echo $stats['total_students'] ?? 0; ?></h5>
                                                <small>Siswa</small>
                                            </div>
                                        </div>
                                        <div class="col-6 mb-3">
                                            <div class="text-success">
                                                <i class="bi bi-collection-play display-6 mb-2"></i>
                                                <h5 class="fw-bold"><?php echo $stats['total_lms_lessons'] ?? 0; ?></h5>
                                                <small>Materi</small>
                                            </div>
                                        </div>
                                        <div class="col-6 mb-3">
                                            <div class="text-warning">
                                                <i class="bi bi-pencil-square display-6 mb-2"></i>
                                                <h5 class="fw-bold"><?php echo $stats['total_lms_quizzes'] ?? 0; ?></h5>
                                                <small>Kuis</small>
                                            </div>
                                        </div>
                                        <div class="col-6 mb-3">
                                            <div class="text-info">
                                                <i class="bi bi-clock display-6 mb-2"></i>
                                                <h5 class="fw-bold"><?php echo $total_hours; ?>h</h5>
                                                <small>Durasi</small>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="mt-3">
                                        <div class="d-flex justify-content-between mb-1">
                                            <small class="text-muted">Progress Rata-rata</small>
                                            <small class="text-muted"><?php echo number_format($stats['avg_progress'] ?? 0, 1); ?>%</small>
                                        </div>
                                        <div class="progress progress-course">
                                            <div class="progress-bar 
                                                <?php echo ($stats['avg_progress'] ?? 0) >= 80 ? 'bg-success' : 
                                                      (($stats['avg_progress'] ?? 0) >= 50 ? 'bg-warning' : 'bg-primary'); ?>" 
                                                 style="width: <?php echo $stats['avg_progress'] ?? 0; ?>%">
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- Recent Student Progress -->
                                <?php if ($reviews->num_rows > 0): ?>
                                <div class="stats-card">
                                    <h5 class="fw-bold mb-4">Progress Siswa</h5>
                                    <div class="reviews-list">
                                        <?php while($review = $reviews->fetch_assoc()): ?>
                                            <div class="review-card">
                                                <div class="d-flex align-items-center mb-2">
                                                    <?php if (!empty($review['foto_profil'])): ?>
                                                        <img src="../uploads/<?php echo $review['foto_profil']; ?>" 
                                                             alt="<?php echo htmlspecialchars($review['nama_lengkap']); ?>" 
                                                             class="rounded-circle me-3" width="40" height="40">
                                                    <?php else: ?>
                                                        <div class="rounded-circle bg-primary text-white d-flex align-items-center justify-content-center me-3" 
                                                             style="width: 40px; height: 40px;">
                                                            <i class="bi bi-person"></i>
                                                        </div>
                                                    <?php endif; ?>
                                                    <div>
                                                        <h6 class="fw-bold mb-0"><?php echo htmlspecialchars($review['nama_lengkap']); ?></h6>
                                                        <small class="text-muted">
                                                            Progress: <?php echo $review['progress']; ?>%
                                                        </small>
                                                    </div>
                                                </div>
                                                <div class="progress progress-course mt-2">
                                                    <div class="progress-bar 
                                                        <?php echo $review['progress'] >= 80 ? 'bg-success' : 
                                                              ($review['progress'] >= 50 ? 'bg-warning' : 'bg-primary'); ?>" 
                                                         style="width: <?php echo $review['progress']; ?>%">
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endwhile; ?>
                                    </div>
                                    <div class="text-center mt-3">
                                        <a href="students_progress.php?course_id=<?php echo $course_id; ?>" class="btn btn-outline-primary btn-sm">
                                            Lihat Semua Progress
                                        </a>
                                    </div>
                                </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Smooth scroll untuk anchor links
            document.querySelectorAll('a[href^="#"]').forEach(anchor => {
                anchor.addEventListener('click', function (e) {
                    e.preventDefault();
                    document.querySelector(this.getAttribute('href')).scrollIntoView({
                        behavior: 'smooth'
                    });
                });
            });

            // Animation untuk lesson items
            const lessonItems = document.querySelectorAll('.lesson-item');
            lessonItems.forEach((item, index) => {
                item.style.animationDelay = `${index * 0.1}s`;
                item.classList.add('animate__animated', 'animate__fadeInUp');
            });
        });
    </script>
</body>
</html>
<?php
// Tutup koneksi database
if (isset($stmt_course)) $stmt_course->close();
if (isset($stmt_stats)) $stmt_stats->close();
if (isset($stmt_lms_lessons)) $stmt_lms_lessons->close();
if (isset($stmt_reviews)) $stmt_reviews->close();
$conn->close();
?>