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

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

// Cek parameter course_id
if (!isset($_GET['id']) || empty($_GET['id'])) {
    $_SESSION['error'] = "Kursus tidak ditemukan!";
    redirect('my_courses.php');
}

$course_id = sanitize($_GET['id']);
$user_id = $_SESSION['user_id'];

// Cek apakah siswa terdaftar di kursus ini
$sql_enrollment = "SELECT e.*, c.judul, c.deskripsi, u.nama_lengkap as nama_guru 
                   FROM lms_enrollments e 
                   JOIN lms_courses c ON e.id_course = c.id 
                   JOIN lms_users u ON c.id_guru = u.id 
                   WHERE e.id_siswa = ? AND e.id_course = ? AND e.status = 'active'";
$stmt_enrollment = $conn->prepare($sql_enrollment);
$stmt_enrollment->bind_param('ii', $user_id, $course_id);
$stmt_enrollment->execute();
$result_enrollment = $stmt_enrollment->get_result();

if ($result_enrollment->num_rows == 0) {
    $_SESSION['error'] = "Anda belum terdaftar di kursus ini!";
    redirect('my_courses.php');
}

$enrollment = $result_enrollment->fetch_assoc();
$current_progress = $enrollment['progress'];

// Ambil data kursus dan lms_lessons
$sql_course = "SELECT c.*, u.nama_lengkap as nama_guru, 
               (SELECT COUNT(*) FROM lms_lessons WHERE id_course = c.id) as total_lms_lessons
               FROM lms_courses c 
               JOIN lms_users u ON c.id_guru = u.id 
               WHERE c.id = ?";
$stmt_course = $conn->prepare($sql_course);
$stmt_course->bind_param('i', $course_id);
$stmt_course->execute();
$course = $stmt_course->get_result()->fetch_assoc();

// Ambil semua lms_lessons
$sql_lms_lessons = "SELECT l.*, 
                (SELECT COUNT(*) FROM lms_quizzes WHERE id_lesson = l.id) as has_quiz,
                (SELECT COUNT(*) FROM lms_quiz_attempts qa 
                 JOIN lms_quizzes q ON qa.id_quiz = q.id 
                 WHERE q.id_lesson = l.id AND qa.id_siswa = ? AND qa.nilai >= 60) as quiz_passed
                FROM lms_lessons l 
                WHERE l.id_course = ? 
                ORDER BY l.urutan ASC";
$stmt_lms_lessons = $conn->prepare($sql_lms_lessons);
$stmt_lms_lessons->bind_param('ii', $user_id, $course_id);
$stmt_lms_lessons->execute();
$result_lms_lessons = $stmt_lms_lessons->get_result();

// Hitung lms_lessons yang sudah diselesaikan
$completed_lms_lessons = 0;
$lms_lessons_data = [];
while ($lesson = $result_lms_lessons->fetch_assoc()) {
    $lms_lessons_data[] = $lesson;
    // Anggap lesson selesai jika urutannya <= progress/10 (asumsi 10 lms_lessons = 100%)
    if ($lesson['urutan'] <= ($current_progress / 10)) {
        $completed_lms_lessons++;
    }
}

// Handle lesson completion
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['complete_lesson'])) {
    $lesson_id = sanitize($_POST['lesson_id']);
    
    // Cari urutan lesson
    $lesson_urutan = 0;
    foreach ($lms_lessons_data as $lesson) {
        if ($lesson['id'] == $lesson_id) {
            $lesson_urutan = $lesson['urutan'];
            break;
        }
    }
    
    if ($lesson_urutan > 0) {
        // Update progress berdasarkan urutan lesson
        $new_progress = min(100, ($lesson_urutan * 10)); // Setiap lesson = 10% progress
        
        $sql_update = "UPDATE lms_enrollments SET progress = ? WHERE id_siswa = ? AND id_course = ?";
        $stmt_update = $conn->prepare($sql_update);
        $stmt_update->bind_param('iii', $new_progress, $user_id, $course_id);
        
        if ($stmt_update->execute()) {
            $_SESSION['success'] = "Lesson berhasil diselesaikan!";
            $current_progress = $new_progress;
            
            // Refresh halaman untuk update progress
            echo "<script>window.location.href = 'course_learn.php?id=$course_id';</script>";
            exit;
        } else {
            $_SESSION['error'] = "Gagal update progress!";
        }
    }
}

// Handle course completion
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['complete_course'])) {
    // Update progress menjadi 100% dan tandai sebagai completed
    $sql_complete = "UPDATE lms_enrollments SET progress = 100, status = 'completed' 
                    WHERE id_siswa = ? AND id_course = ?";
    $stmt_complete = $conn->prepare($sql_complete);
    $stmt_complete->bind_param('ii', $user_id, $course_id);
    
    if ($stmt_complete->execute()) {
        $_SESSION['success'] = "Selamat! Anda telah menyelesaikan kursus ini!";
        $current_progress = 100;
        
        // Redirect ke halaman detail kursus dengan pesan sukses
        redirect('course_detail.php?id=' . $course_id);
    } else {
        $_SESSION['error'] = "Gagal menyelesaikan kursus!";
    }
}

// Cek apakah kursus sudah selesai
$is_course_completed = $current_progress >= 100;

// Handle current lesson
$current_lesson_id = isset($_GET['lesson']) ? sanitize($_GET['lesson']) : null;
$current_lesson = null;

if ($current_lesson_id) {
    foreach ($lms_lessons_data as $lesson) {
        if ($lesson['id'] == $current_lesson_id) {
            $current_lesson = $lesson;
            break;
        }
    }
}

// Jika tidak ada lesson yang dipilih, pilih lesson pertama yang belum selesai
if (!$current_lesson) {
    foreach ($lms_lessons_data as $lesson) {
        if ($lesson['urutan'] > ($current_progress / 10)) {
            $current_lesson = $lesson;
            break;
        }
    }
    // Jika semua lesson selesai, pilih lesson terakhir
    if (!$current_lesson && !empty($lms_lessons_data)) {
        $current_lesson = end($lms_lessons_data);
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Belajar: <?php echo $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: #f8f9fa;
        }
        
        body {
            background-color: #f5f7fb;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        .learning-container {
            display: flex;
            min-height: 100vh;
        }
        
        /* Sidebar Styles */
        .sidebar-learning {
            width: 320px;
            background: white;
            border-right: 1px solid #e0e0e0;
            height: 100vh;
            position: fixed;
            left: 0;
            top: 0;
            overflow-y: auto;
            box-shadow: 2px 0 8px rgba(0,0,0,0.1);
        }
        
        .sidebar-header {
            padding: 20px;
            border-bottom: 1px solid #e0e0e0;
            background: white;
            position: sticky;
            top: 0;
            z-index: 10;
        }
        
        .sidebar-content {
            padding: 20px;
        }
        
        /* Main Content Styles */
        .content-learning {
            flex: 1;
            margin-left: 320px;
            background: #f5f7fb;
            min-height: 100vh;
        }
        
        .content-header {
            background: white;
            padding: 15px 30px;
            border-bottom: 1px solid #e0e0e0;
            position: sticky;
            top: 0;
            z-index: 100;
        }
        
        .lesson-content-area {
            max-width: 900px;
            margin: 0 auto;
            padding: 30px;
        }
        
        /* Lesson List Styles */
        .lesson-item {
            padding: 15px;
            border: 1px solid #e9ecef;
            border-radius: 8px;
            margin-bottom: 10px;
            cursor: pointer;
            transition: all 0.3s ease;
            background: white;
        }
        
        .lesson-item:hover {
            border-color: var(--primary-color);
            box-shadow: 0 2px 8px rgba(63, 81, 181, 0.1);
        }
        
        .lesson-item.active {
            border-color: var(--primary-color);
            background: #f0f4ff;
            box-shadow: 0 2px 12px rgba(63, 81, 181, 0.15);
        }
        
        .lesson-item.completed {
            border-left: 4px solid #28a745;
        }
        
        .lesson-number {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            background: #e9ecef;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 0.9rem;
            margin-right: 12px;
            flex-shrink: 0;
        }
        
        .lesson-item.active .lesson-number {
            background: var(--primary-color);
            color: white;
        }
        
        .lesson-item.completed .lesson-number {
            background: #28a745;
            color: white;
        }
        
        /* Progress Styles */
        .progress-container {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 10px;
            margin-bottom: 20px;
        }
        
        .progress-learning {
            height: 8px;
            border-radius: 4px;
            background: #e9ecef;
        }
        
        .progress-learning .progress-bar {
            border-radius: 4px;
            background: linear-gradient(45deg, #667eea, #764ba2);
        }
        
        /* Content Card Styles */
        .content-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            border: 1px solid #e9ecef;
            margin-bottom: 20px;
            overflow: hidden;
        }
        
        .content-card-header {
            padding: 25px 30px 0;
            border-bottom: none;
        }
        
        .content-card-body {
            padding: 25px 30px;
        }
        
        /* Lesson Content Styles */
        .lesson-html-content {
            line-height: 1.7;
            font-size: 1.05rem;
            color: #2d3748;
        }
        
        .lesson-html-content h1 {
            font-size: 2rem;
            font-weight: 700;
            color: #1a202c;
            margin-bottom: 1.5rem;
            border-bottom: 2px solid #e2e8f0;
            padding-bottom: 0.5rem;
        }
        
        .lesson-html-content h2 {
            font-size: 1.5rem;
            font-weight: 600;
            color: #2d3748;
            margin: 2rem 0 1rem 0;
        }
        
        .lesson-html-content h3 {
            font-size: 1.25rem;
            font-weight: 600;
            color: #4a5568;
            margin: 1.5rem 0 0.75rem 0;
        }
        
        .lesson-html-content p {
            margin-bottom: 1.25rem;
            color: #4a5568;
        }
        
        .lesson-html-content ul, 
        .lesson-html-content ol {
            margin-bottom: 1.25rem;
            padding-left: 2rem;
        }
        
        .lesson-html-content li {
            margin-bottom: 0.5rem;
            color: #4a5568;
        }
        
        .lesson-html-content blockquote {
            border-left: 4px solid var(--primary-color);
            padding: 1rem 1.5rem;
            margin: 1.5rem 0;
            background: #f7fafc;
            border-radius: 0 8px 8px 0;
            font-style: italic;
        }
        
        .lesson-html-content table {
            width: 100%;
            border-collapse: collapse;
            margin: 1.5rem 0;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .lesson-html-content table th,
        .lesson-html-content table td {
            border: 1px solid #e2e8f0;
            padding: 12px 15px;
            text-align: left;
        }
        
        .lesson-html-content table th {
            background: #f7fafc;
            font-weight: 600;
            color: #2d3748;
        }
        
        .lesson-html-content img {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
            margin: 1.5rem 0;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        
        /* Quiz Card Styles */
        .quiz-card {
            background: linear-gradient(135deg, #fff9e6, #fff3cd);
            border: 2px solid #ffeaa7;
            border-radius: 12px;
            padding: 25px;
            margin: 25px 0;
        }
        
        /* Navigation Buttons */
        .nav-buttons {
            background: white;
            padding: 20px 30px;
            border-top: 1px solid #e9ecef;
            position: sticky;
            bottom: 0;
            margin-top: 30px;
        }
        
        /* Completion Badge */
        .completion-badge {
            font-size: 0.75rem;
            padding: 4px 8px;
            border-radius: 12px;
        }
        
        /* Course Completion Styles */
        .course-complete-btn {
            background: linear-gradient(45deg, #28a745, #20c997);
            border: none;
            padding: 12px 24px;
            font-weight: 600;
            transition: all 0.3s ease;
        }
        
        .course-complete-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(40, 167, 69, 0.3);
        }
        
        /* Mobile Responsive */
        @media (max-width: 768px) {
            .learning-container {
                flex-direction: column;
            }
            
            .sidebar-learning {
                position: relative;
                width: 100%;
                height: auto;
            }
            
            .content-learning {
                margin-left: 0;
            }
            
            .lesson-content-area {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="learning-container">
        <!-- Sidebar -->
        <div class="sidebar-learning">
            <div class="sidebar-header">
                <div class="d-flex align-items-center">
                    <a href="my_courses.php" class="btn btn-outline-primary btn-sm me-3">
                        <i class="bi bi-arrow-left"></i>
                    </a>
                    <div>
                        <h6 class="mb-1 fw-bold"><?php echo $course['judul']; ?></h6>
                        <small class="text-muted">Oleh: <?php echo $course['nama_guru']; ?></small>
                    </div>
                </div>
            </div>
            
            <div class="sidebar-content">
                <!-- Progress Section -->
                <div class="progress-container">
                    <div class="d-flex justify-content-between align-items-center mb-2">
                        <span class="text-muted small">Progress Kursus</span>
                        <span class="text-muted small"><?php echo $completed_lms_lessons; ?> dari <?php echo count($lms_lessons_data); ?> materi</span>
                    </div>
                    <div class="progress progress-learning">
                        <div class="progress-bar" style="width: <?php echo $current_progress; ?>%"></div>
                    </div>
                    <div class="text-center mt-2">
                        <span class="fw-bold text-primary"><?php echo $current_progress; ?>% Selesai</span>
                    </div>
                    
                    <?php if ($is_course_completed): ?>
                        <div class="alert alert-success mt-3 mb-0 text-center">
                            <i class="bi bi-check-circle-fill me-2"></i>
                            <strong>Kursus Telah Selesai!</strong>
                        </div>
                    <?php endif; ?>
                </div>
                
                <!-- lms_lessons List -->
                <h6 class="fw-bold mb-3">Daftar Materi</h6>
                <div class="lms_lessons-list">
                    <?php foreach ($lms_lessons_data as $lesson): 
                        $is_completed = $lesson['urutan'] <= ($current_progress / 10);
                        $is_active = $current_lesson && $current_lesson['id'] == $lesson['id'];
                        $has_quiz = $lesson['has_quiz'] > 0;
                        $quiz_passed = $lesson['quiz_passed'] > 0;
                    ?>
                        <div class="lesson-item <?php echo $is_active ? 'active' : ''; ?> <?php echo $is_completed ? 'completed' : ''; ?>"
                             onclick="window.location.href='course_learn.php?id=<?php echo $course_id; ?>&lesson=<?php echo $lesson['id']; ?>'">
                            <div class="d-flex align-items-start">
                                <div class="lesson-number"><?php echo $lesson['urutan']; ?></div>
                                <div class="flex-grow-1">
                                    <h6 class="mb-1 fw-semibold"><?php echo $lesson['judul_lesson']; ?></h6>
                                    <div class="d-flex align-items-center text-muted small">
                                        <i class="bi bi-clock me-1"></i>
                                        <span class="me-3"><?php echo $lesson['durasi_menit']; ?> menit</span>
                                        <?php if ($has_quiz): ?>
                                            <i class="bi bi-pencil-square me-1"></i>
                                            <span>Quiz</span>
                                        <?php endif; ?>
                                    </div>
                                </div>
                                <div class="ms-2">
                                    <?php if ($is_completed): ?>
                                        <i class="bi bi-check-circle-fill text-success"></i>
                                    <?php endif; ?>
                                    <?php if ($has_quiz): ?>
                                        <span class="completion-badge badge <?php echo $quiz_passed ? 'bg-success' : 'bg-warning'; ?>">
                                            <i class="bi bi-pencil-square"></i>
                                        </span>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                    
                    <?php if (empty($lms_lessons_data)): ?>
                        <div class="text-center py-4">
                            <i class="bi bi-journal-x text-muted" style="font-size: 2rem;"></i>
                            <p class="text-muted mt-2 small">Belum ada materi pembelajaran</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Main Content -->
        <div class="content-learning">
            <!-- Content Header -->
            <div class="content-header">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h4 class="fw-bold mb-1"><?php echo $current_lesson ? $current_lesson['judul_lesson'] : 'Tidak ada lesson'; ?></h4>
                        <p class="text-muted mb-0">
                            <?php if ($current_lesson): ?>
                                Pertemuan <?php echo $current_lesson['urutan']; ?> • 
                                <?php echo $current_lesson['durasi_menit']; ?> menit
                            <?php endif; ?>
                        </p>
                    </div>
                    <div class="d-flex align-items-center">
                        <div class="me-3 text-end">
                            <small class="text-muted d-block">Progress Anda</small>
                            <span class="fw-bold text-primary"><?php echo $current_progress; ?>%</span>
                        </div>
                        <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="dashboard.php">
                                    <i class="bi bi-speedometer2 me-2"></i>Dashboard
                                </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>
            </div>

            <!-- Lesson Content Area -->
            <div class="lesson-content-area">
                <?php if ($current_lesson): ?>
                    <!-- Completion Button -->
                    <div class="content-card">
                        <div class="content-card-body">
                            <div class="d-flex justify-content-between align-items-center">
                                <div>
                                    <h5 class="mb-1"><?php echo $current_lesson['judul_lesson']; ?></h5>
                                    <p class="text-muted mb-0">
                                        Pastikan Anda telah memahami materi sebelum melanjutkan
                                    </p>
                                </div>
                                <div>
                                    <?php 
                                    $is_lesson_completed = $current_lesson['urutan'] <= ($current_progress / 10);
                                    if (!$is_lesson_completed): 
                                    ?>
                                        <form method="POST" class="d-inline">
                                            <input type="hidden" name="lesson_id" value="<?php echo $current_lesson['id']; ?>">
                                            <button type="submit" name="complete_lesson" class="btn btn-success btn-lg">
                                                <i class="bi bi-check-circle me-2"></i>Tandai Selesai
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <span class="badge bg-success fs-6 p-3">
                                            <i class="bi bi-check-circle me-2"></i>Sudah Selesai
                                        </span>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Lesson Content -->
                    <div class="content-card">
                        <div class="content-card-body">
                            <?php if (!empty($current_lesson['konten'])): ?>
                                <div class="lesson-html-content">
                                    <?php echo $current_lesson['konten']; ?>
                                </div>
                            <?php else: ?>
                                <div class="text-center py-5">
                                    <i class="bi bi-file-text text-muted" style="font-size: 4rem;"></i>
                                    <h5 class="text-muted mt-3">Konten Belum Tersedia</h5>
                                    <p class="text-muted">Materi pembelajaran untuk lesson ini sedang dalam persiapan.</p>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <!-- Quiz Section -->
                    <?php if ($current_lesson['has_quiz'] > 0): ?>
                        <div class="quiz-card">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <div>
                                    <h5 class="mb-1">
                                        <i class="bi bi-pencil-square text-warning me-2"></i>
                                        Quiz - <?php echo $current_lesson['judul_lesson']; ?>
                                    </h5>
                                    <p class="text-muted mb-0">
                                        Uji pemahaman Anda dengan mengerjakan quiz
                                    </p>
                                </div>
                                <?php if ($current_lesson['quiz_passed'] > 0): ?>
                                    <span class="badge bg-success fs-6">
                                        <i class="bi bi-check-circle me-1"></i>Lulus
                                    </span>
                                <?php else: ?>
                                    <span class="badge bg-warning fs-6">
                                        <i class="bi bi-clock me-1"></i>Belum Dikerjakan
                                    </span>
                                <?php endif; ?>
                            </div>
                            <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                                <?php if ($current_lesson['quiz_passed'] > 0): ?>
                                    <a href="quiz_result.php?lesson_id=<?php echo $current_lesson['id']; ?>" 
                                       class="btn btn-outline-success">
                                        <i class="bi bi-eye me-2"></i>Lihat Hasil Quiz
                                    </a>
                                <?php else: ?>
                                    <a href="take_quiz.php?lesson_id=<?php echo $current_lesson['id']; ?>" 
                                       class="btn btn-warning">
                                        <i class="bi bi-pencil me-2"></i>Kerjakan Quiz
                                    </a>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>

                    <!-- Navigation Buttons -->
                    <div class="nav-buttons">
                        <div class="d-flex justify-content-between">
                            <?php 
                            $prev_lesson = null;
                            $next_lesson = null;
                            
                            foreach ($lms_lessons_data as $index => $lesson) {
                                if ($lesson['id'] == $current_lesson['id']) {
                                    $prev_lesson = $index > 0 ? $lms_lessons_data[$index - 1] : null;
                                    $next_lesson = $index < count($lms_lessons_data) - 1 ? $lms_lessons_data[$index + 1] : null;
                                    break;
                                }
                            }
                            ?>
                            
                            <div>
                                <?php if ($prev_lesson): ?>
                                    <a href="course_learn.php?id=<?php echo $course_id; ?>&lesson=<?php echo $prev_lesson['id']; ?>" 
                                    class="btn btn-outline-primary">
                                        <i class="bi bi-chevron-left me-2"></i>Materi Sebelumnya
                                    </a>
                                <?php endif; ?>
                            </div>
                            
                            <div>
                                <?php if ($next_lesson): ?>
                                    <a href="course_learn.php?id=<?php echo $course_id; ?>&lesson=<?php echo $next_lesson['id']; ?>" 
                                    class="btn btn-primary">
                                        Materi Selanjutnya<i class="bi bi-chevron-right ms-2"></i>
                                    </a>
                                <?php else: ?>
                                    <?php if (!$is_course_completed): ?>
                                        <!-- Tombol untuk menyelesaikan kursus -->
                                        <form method="POST" class="d-inline">
                                            <button type="submit" name="complete_course" class="btn btn-success course-complete-btn"
                                                    onclick="return confirm('Apakah Anda yakin telah menyelesaikan seluruh materi dan quiz dalam kursus ini?')">
                                                <i class="bi bi-trophy me-2"></i>Selesaikan Kursus
                                            </button>
                                        </form>
                                    <?php else: ?>
                                        <!-- Jika kursus sudah selesai -->
                                        <a href="course_detail.php?id=<?php echo $course_id; ?>" 
                                        class="btn btn-outline-success">
                                            <i class="bi bi-check-circle me-2"></i>Kursus Telah Selesai
                                        </a>
                                    <?php endif; ?>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>

                <?php else: ?>
                    <div class="content-card">
                        <div class="content-card-body text-center py-5">
                            <i class="bi bi-journal-x text-muted" style="font-size: 4rem;"></i>
                            <h4 class="text-muted mt-3">Tidak ada lesson yang tersedia</h4>
                            <p class="text-muted">Materi pembelajaran untuk kursus ini sedang dalam persiapan.</p>
                            <a href="course_detail.php?id=<?php echo $course_id; ?>" class="btn btn-primary">
                                Kembali ke Detail Kursus
                            </a>
                        </div>
                    </div>
                <?php endif; ?>
            </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() {
            // Fixed layout - no need for height adjustments
            console.log('Learning page loaded successfully');
            
            // Add smooth scrolling for lesson items
            document.querySelectorAll('.lesson-item').forEach(item => {
                item.addEventListener('click', function() {
                    // The page will reload with the new lesson
                });
            });

            // Show confirmation for course completion
            // const completeCourseBtn = document.querySelector('button[name="complete_course"]');
            // if (completeCourseBtn) {
            //     completeCourseBtn.addEventListener('click', function(e) {
            //         if (!confirm('Apakah Anda yakin telah menyelesaikan seluruh materi dan quiz dalam kursus ini?')) {
            //             e.preventDefault();
            //         }
            //     });
            // }
        });
    </script>
</body>
</html>