<?php
include 'config.php';

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

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

// Ambil data kursus
$sql_course = "SELECT c.*, u.nama_lengkap as nama_guru, u.email as email_guru, 
                      cat.nama_kategori, cat.deskripsi as deskripsi_kategori
               FROM lms_courses c 
               JOIN lms_users u ON c.id_guru = u.id 
               JOIN lms_categories cat ON c.id_kategori = cat.id 
               WHERE c.id = ?";
$stmt_course = $conn->prepare($sql_course);
$stmt_course->bind_param('i', $course_id);
$stmt_course->execute();
$result_course = $stmt_course->get_result();

if ($result_course->num_rows == 0) {
    $_SESSION['error'] = "Kursus tidak ditemukan!";
    redirect('courses.php');
}

$course = $result_course->fetch_assoc();

// Ambil lms_lessons/pertemuan
$sql_lms_lessons = "SELECT * FROM lms_lessons 
                WHERE id_course = ? 
                ORDER BY urutan ASC";
$stmt_lms_lessons = $conn->prepare($sql_lms_lessons);
$stmt_lms_lessons->bind_param('i', $course_id);
$stmt_lms_lessons->execute();
$result_lms_lessons = $stmt_lms_lessons->get_result();

// Cek apakah user sudah terdaftar di kursus ini
$is_enrolled = false;
$enrollment_progress = 0;
$is_course_completed = false; // Tambahkan variabel untuk menandai kursus selesai
if (isLoggedIn() && isSiswa()) {
    $user_id = $_SESSION['user_id'];
    $sql_enrollment = "SELECT * FROM lms_enrollments 
                      WHERE id_siswa = ? AND id_course = ?";
    $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) {
        $is_enrolled = true;
        $enrollment = $result_enrollment->fetch_assoc();
        $enrollment_progress = $enrollment['progress'];
        $is_course_completed = ($enrollment_progress >= 100); // Cek jika progress 100%
    }
}

// Ambil data guru lainnya
$sql_other_teachers = "SELECT u.*, COUNT(c.id) as total_lms_courses 
                       FROM lms_users u 
                       LEFT JOIN lms_courses c ON u.id = c.id_guru 
                       WHERE u.role = 'guru' AND u.id != ? 
                       GROUP BY u.id 
                       ORDER BY total_lms_courses DESC 
                       LIMIT 3";
$stmt_teachers = $conn->prepare($sql_other_teachers);
$stmt_teachers->bind_param('i', $course['id_guru']);
$stmt_teachers->execute();
$result_teachers = $stmt_teachers->get_result();

// Ambil kursus terkait
$sql_related_lms_courses = "SELECT c.*, u.nama_lengkap as nama_guru 
                        FROM lms_courses c 
                        JOIN lms_users u ON c.id_guru = u.id 
                        WHERE c.id_kategori = ? AND c.id != ? 
                        ORDER BY c.rating DESC 
                        LIMIT 4";
$stmt_related = $conn->prepare($sql_related_lms_courses);
$stmt_related->bind_param('ii', $course['id_kategori'], $course_id);
$stmt_related->execute();
$result_related = $stmt_related->get_result();

// Handle enroll course
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['enroll_course'])) {
    if (!isLoggedIn()) {
        $_SESSION['error'] = "Silakan login terlebih dahulu!";
        redirect('login.php');
    }
    
    if (!isSiswa()) {
        $_SESSION['error'] = "Hanya siswa yang dapat mendaftar kursus!";
        redirect('courses.php');
    }
    
    $user_id = $_SESSION['user_id'];
    
    // Cek apakah sudah terdaftar
    if (!$is_enrolled) {
        $sql_enroll = "INSERT INTO lms_enrollments (id_siswa, id_course) VALUES (?, ?)";
        $stmt_enroll = $conn->prepare($sql_enroll);
        $stmt_enroll->bind_param('ii', $user_id, $course_id);
        
        if ($stmt_enroll->execute()) {
            // Update total siswa di course
            $sql_update = "UPDATE lms_courses SET total_siswa = total_siswa + 1 WHERE id = ?";
            $stmt_update = $conn->prepare($sql_update);
            $stmt_update->bind_param('i', $course_id);
            $stmt_update->execute();
            
            $_SESSION['success'] = "Berhasil mendaftar ke kursus!";
            $is_enrolled = true;
        } else {
            $_SESSION['error'] = "Gagal mendaftar ke kursus!";
        }
    } else {
        $_SESSION['info'] = "Anda sudah terdaftar di kursus ini!";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?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: #f5f5f5;
        }
        
        .course-hero {
            background: linear-gradient(135deg, var(--primary-color), #7986cb);
            color: white;
            padding: 60px 0;
        }
        
        .course-image {
            height: 300px;
            background: linear-gradient(45deg, #667eea, #764ba2);
            border-radius: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 4rem;
            margin-bottom: 20px;
        }
        
        .enrollment-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            padding: 30px;
            position: sticky;
            top: 20px;
        }
        
        .price {
            font-size: 2rem;
            font-weight: bold;
            color: var(--primary-color);
        }
        
        .lesson-item {
            padding: 15px;
            border: 1px solid #e9ecef;
            border-radius: 10px;
            margin-bottom: 10px;
            transition: all 0.3s;
        }
        
        .lesson-item:hover {
            border-color: var(--primary-color);
            background-color: #f8f9ff;
        }
        
        .lesson-completed {
            background-color: #d4edda;
            border-color: #c3e6cb;
        }
        
        .progress-bar {
            background-color: var(--primary-color);
        }
        
        .teacher-card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            transition: transform 0.3s;
        }
        
        .teacher-card:hover {
            transform: translateY(-5px);
        }
        
        .rating {
            color: #ffc107;
        }
        
        .feature-list li {
            margin-bottom: 10px;
            padding-left: 30px;
            position: relative;
        }
        
        .feature-list li:before {
            content: "✓";
            color: #28a745;
            font-weight: bold;
            position: absolute;
            left: 0;
        }
        
        .nav-tabs .nav-link.active {
            color: var(--primary-color);
            font-weight: bold;
            border-bottom: 3px solid var(--primary-color);
        }
        
        /* Style untuk tombol kursus selesai */
        .btn-completed {
            background: linear-gradient(45deg, #28a745, #20c997);
            border: none;
            color: white;
        }
        
        .btn-completed:hover {
            background: linear-gradient(45deg, #218838, #1e7e34);
            color: white;
        }
    </style>
</head>
<body>
    <!-- Navbar -->
    <nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
        <div class="container">
            <a class="navbar-brand fw-bold text-primary" href="index.php">
                <i class="bi bi-book-half"></i> SMANSAB LMS
            </a>
            <div class="navbar-nav ms-auto">
                <?php if (isLoggedIn()): ?>
                    <span class="navbar-text me-3">
                        <i class="bi bi-person-circle"></i> <?php echo $_SESSION['nama_lengkap']; ?>
                    </span>
                    <?php if (isAdmin()): ?>
                        <a href="admin/dashboard.php" class="btn btn-outline-primary me-2">Dashboard Admin</a>
                    <?php elseif (isGuru()): ?>
                        <a href="guru/dashboard.php" class="btn btn-outline-primary me-2">Dashboard Guru</a>
                    <?php elseif (isSiswa()): ?>
                        <a href="dashboard.php" class="btn btn-outline-primary me-2">Dashboard Siswa</a>
                    <?php endif; ?>
                    <a href="logout.php" class="btn btn-outline-secondary">Logout</a>
                <?php else: ?>
                    <a href="login.php" class="btn btn-outline-primary me-2">Login</a>
                    <a href="register.php" class="btn btn-primary">Daftar</a>
                <?php endif; ?>
            </div>
        </div>
    </nav>

    <!-- Course Hero Section -->
    <section class="course-hero">
        <div class="container">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="index.php" class="text-white">Beranda</a></li>
                            <li class="breadcrumb-item"><a href="my_courses.php" class="text-white">Kursus</a></li>
                            <li class="breadcrumb-item"><a href="my_courses.php?category=<?php echo $course['id_kategori']; ?>" class="text-white"><?php echo $course['nama_kategori']; ?></a></li>
                            <li class="breadcrumb-item active text-white" aria-current="page"><?php echo $course['judul']; ?></li>
                        </ol>
                    </nav>
                    
                    <h1 class="display-5 fw-bold mb-3"><?php echo $course['judul']; ?></h1>
                    <p class="lead mb-4"><?php echo $course['deskripsi']; ?></p>
                    
                    <div class="d-flex flex-wrap gap-3 mb-3">
                        <div class="rating">
                            <i class="bi bi-star-fill"></i>
                            <span><?php echo $course['rating']; ?></span>
                            <span class="text-white-50">(<?php echo $course['total_siswa']; ?> siswa)</span>
                        </div>
                        <div class="text-white-50">
                            <i class="bi bi-clock"></i> <?php echo $course['durasi_jam']; ?> jam
                        </div>
                        <div class="text-white-50">
                            <i class="bi bi-bar-chart"></i> 
                            <span class="text-capitalize"><?php echo $course['level']; ?></span>
                        </div>
                        <div class="text-white-50">
                            <i class="bi bi-person"></i> Oleh: <?php echo $course['nama_guru']; ?>
                        </div>
                    </div>
                    
                    <?php if ($is_enrolled): ?>
                        <div class="alert alert-success d-inline-block">
                            <i class="bi bi-check-circle"></i> 
                            <?php if ($is_course_completed): ?>
                                Selamat! Anda telah menyelesaikan kursus ini
                            <?php else: ?>
                                Anda sudah terdaftar di kursus ini - Progress: <?php echo $enrollment_progress; ?>%
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </section>

    <div class="container my-5">
        <div class="row">
            <!-- Main Content -->
            <div class="col-lg-8">
                <!-- Course Content -->
                <div class="card border-0 shadow-sm mb-4">
                    <div class="card-body">
                        <h4 class="card-title mb-4">Materi Kursus</h4>
                        
                        <?php if ($is_enrolled): ?>
                            <div class="mb-4">
                                <div class="d-flex justify-content-between mb-2">
                                    <span>Progress Belajar</span>
                                    <span><?php echo $enrollment_progress; ?>%</span>
                                </div>
                                <div class="progress" style="height: 8px;">
                                    <div class="progress-bar <?php echo $is_course_completed ? 'bg-success' : ''; ?>" 
                                         style="width: <?php echo $enrollment_progress; ?>%">
                                    </div>
                                </div>
                                <?php if ($is_course_completed): ?>
                                    <div class="text-center mt-2">
                                        <span class="badge bg-success">
                                            <i class="bi bi-check-circle me-1"></i>Kursus Telah Selesai
                                        </span>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                        
                        <div class="lms_lessons-container">
                            <?php if ($result_lms_lessons->num_rows > 0): ?>
                                <?php $lesson_count = 1; ?>
                                <?php while($lesson = $result_lms_lessons->fetch_assoc()): ?>
                                    <div class="lesson-item <?php echo ($is_enrolled && $lesson_count <= $enrollment_progress / 10) ? 'lesson-completed' : ''; ?>">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <div class="d-flex align-items-center">
                                                <div class="me-3">
                                                    <?php if ($is_enrolled && $lesson_count <= $enrollment_progress / 10): ?>
                                                        <i class="bi bi-check-circle-fill text-success" style="font-size: 1.2rem;"></i>
                                                    <?php else: ?>
                                                        <i class="bi bi-play-circle text-primary" style="font-size: 1.2rem;"></i>
                                                    <?php endif; ?>
                                                </div>
                                                <div>
                                                    <h6 class="mb-1">Pertemuan <?php echo $lesson_count; ?>: <?php echo $lesson['judul_lesson']; ?></h6>
                                                    <small class="text-muted">
                                                        <i class="bi bi-clock"></i> <?php echo $lesson['durasi_menit']; ?> menit • 
                                                        <?php echo ucfirst($lesson['tipe_lesson']); ?>
                                                    </small>
                                                </div>
                                            </div>
                                            <div>
                                                <?php if ($is_enrolled): ?>
                                                    <?php if ($is_course_completed): ?>
                                                        <a href="course_learn.php?id=<?php echo $course_id; ?>&lesson=<?php echo $lesson['id']; ?>" 
                                                           class="btn btn-outline-success btn-sm">
                                                            <i class="bi bi-eye"></i> Lihat Ulang
                                                        </a>
                                                    <?php else: ?>
                                                        <a href="course_learn.php?id=<?php echo $course_id; ?>&lesson=<?php echo $lesson['id']; ?>" 
                                                           class="btn btn-primary btn-sm">
                                                            <?php echo ($lesson_count <= $enrollment_progress / 10) ? 'Lihat Ulang' : 'Mulai Belajar'; ?>
                                                        </a>
                                                    <?php endif; ?>
                                                <?php else: ?>
                                                    <span class="badge bg-secondary">Login untuk mengakses</span>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </div>
                                    <?php $lesson_count++; ?>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <div class="text-center py-4">
                                    <i class="bi bi-journal-x" style="font-size: 3rem; color: #6c757d;"></i>
                                    <p class="mt-3 text-muted">Materi kursus sedang dalam persiapan</p>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <!-- Course Description -->
                <div class="card border-0 shadow-sm mb-4">
                    <div class="card-body">
                        <ul class="nav nav-tabs" id="courseTab" role="tablist">
                            <li class="nav-item" role="presentation">
                                <button class="nav-link active" id="description-tab" data-bs-toggle="tab" 
                                        data-bs-target="#description" type="button" role="tab">
                                    Deskripsi
                                </button>
                            </li>
                            <li class="nav-item" role="presentation">
                                <button class="nav-link" id="curriculum-tab" data-bs-toggle="tab" 
                                        data-bs-target="#curriculum" type="button" role="tab">
                                    Kurikulum
                                </button>
                            </li>
                            <li class="nav-item" role="presentation">
                                <button class="nav-link" id="instructor-tab" data-bs-toggle="tab" 
                                        data-bs-target="#instructor" type="button" role="tab">
                                    Tentang Guru
                                </button>
                            </li>
                        </ul>
                        
                        <div class="tab-content mt-4" id="courseTabContent">
                            <div class="tab-pane fade show active" id="description" role="tabpanel">
                                <h5>Apa yang akan Anda pelajari?</h5>
                                <p><?php echo $course['deskripsi']; ?></p>
                                
                                <h5 class="mt-4">Fitur Utama Kursus</h5>
                                <ul class="feature-list">
                                    <li>Materi lengkap dan terstruktur</li>
                                    <li>Video pembelajaran berkualitas tinggi</li>
                                    <li>Quiz dan latihan soal</li>
                                    <li>Sertifikat penyelesaian</li>
                                    <li>Akses seumur hidup</li>
                                    <li>Dukungan dari instruktur</li>
                                </ul>
                            </div>
                            
                            <div class="tab-pane fade" id="curriculum" role="tabpanel">
                                <h5>Struktur Kursus</h5>
                                <div class="table-responsive">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Pertemuan</th>
                                                <th>Materi</th>
                                                <th>Durasi</th>
                                                <th>Tipe</th>
                                                <?php if ($is_enrolled): ?>
                                                <th>Status</th>
                                                <?php endif; ?>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php 
                                            $result_lms_lessons->data_seek(0); // Reset pointer
                                            $lesson_count = 1; 
                                            ?>
                                            <?php while($lesson = $result_lms_lessons->fetch_assoc()): ?>
                                                <tr>
                                                    <td><?php echo $lesson_count; ?></td>
                                                    <td><?php echo $lesson['judul_lesson']; ?></td>
                                                    <td><?php echo $lesson['durasi_menit']; ?> menit</td>
                                                    <td>
                                                        <span class="badge bg-info text-capitalize">
                                                            <?php echo $lesson['tipe_lesson']; ?>
                                                        </span>
                                                    </td>
                                                    <?php if ($is_enrolled): ?>
                                                    <td>
                                                        <?php if ($lesson_count <= $enrollment_progress / 10): ?>
                                                            <span class="badge bg-success">
                                                                <i class="bi bi-check-circle"></i> Selesai
                                                            </span>
                                                        <?php else: ?>
                                                            <span class="badge bg-secondary">
                                                                <i class="bi bi-clock"></i> Belum
                                                            </span>
                                                        <?php endif; ?>
                                                    </td>
                                                    <?php endif; ?>
                                                </tr>
                                                <?php $lesson_count++; ?>
                                            <?php endwhile; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            
                            <div class="tab-pane fade" id="instructor" role="tabpanel">
                                <div class="row">
                                    <div class="col-md-3 text-center">
                                        <div class="mb-3">
                                            <i class="bi bi-person-circle" style="font-size: 4rem; color: #6c757d;"></i>
                                        </div>
                                        <h5><?php echo $course['nama_guru']; ?></h5>
                                        <p class="text-muted">Instruktur</p>
                                    </div>
                                    <div class="col-md-9">
                                        <h5>Tentang Instruktur</h5>
                                        <p><?php echo $course['nama_guru']; ?> adalah instruktur berpengalaman di bidang <?php echo $course['nama_kategori']; ?> dengan pengalaman mengajar lebih dari 5 tahun.</p>
                                        
                                        <div class="row mt-4">
                                            <div class="col-md-4">
                                                <div class="text-center">
                                                    <h4 class="text-primary"><?php 
                                                        $sql_course_count = "SELECT COUNT(*) as total FROM lms_courses WHERE id_guru = ?";
                                                        $stmt_count = $conn->prepare($sql_course_count);
                                                        $stmt_count->bind_param('i', $course['id_guru']);
                                                        $stmt_count->execute();
                                                        $course_count = $stmt_count->get_result()->fetch_assoc()['total'];
                                                        echo $course_count;
                                                    ?></h4>
                                                    <small>Total Kursus</small>
                                                </div>
                                            </div>
                                            <div class="col-md-4">
                                                <div class="text-center">
                                                    <h4 class="text-primary"><?php echo $course['total_siswa']; ?></h4>
                                                    <small>Total Siswa</small>
                                                </div>
                                            </div>
                                            <div class="col-md-4">
                                                <div class="text-center">
                                                    <h4 class="text-primary"><?php echo $course['rating']; ?></h4>
                                                    <small>Rating</small>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Sidebar -->
            <div class="col-lg-4">
                <!-- Enrollment Card -->
                <div class="enrollment-card">
                    <div class="course-image">
                        <i class="bi bi-laptop"></i>
                        <?php if ($is_course_completed): ?>
                            <div class="position-absolute top-0 end-0 m-3">
                                <span class="badge bg-success fs-6">
                                    <i class="bi bi-trophy"></i> Selesai
                                </span>
                            </div>
                        <?php endif; ?>
                    </div>
                    
                    <div class="text-center mb-4">
                        <?php if ($course['harga'] == 0): ?>
                            <div class="price text-success">Gratis</div>
                        <?php else: ?>
                            <div class="price">Rp <?php echo number_format($course['harga'], 0, ',', '.'); ?></div>
                        <?php endif; ?>
                    </div>
                    
                    <?php if ($is_enrolled): ?>
                        <?php if ($is_course_completed): ?>
                            <a href="certificates.php?course_id=<?php echo $course_id; ?>" 
                               class="btn btn-completed w-100 btn-lg mb-3">
                                <i class="bi bi-award"></i> Lihat Sertifikat
                            </a>
                            <a href="course_learn.php?id=<?php echo $course_id; ?>" 
                               class="btn btn-outline-success w-100 btn-lg mb-3">
                                <i class="bi bi-eye"></i> Lihat Ulang Materi
                            </a>
                        <?php else: ?>
                            <a href="course_learn.php?id=<?php echo $course_id; ?>" 
                               class="btn btn-primary w-100 btn-lg mb-3">
                                <i class="bi bi-play-circle"></i> Lanjutkan Belajar
                            </a>
                        <?php endif; ?>
                    <?php else: ?>
                        <form method="POST">
                            <button type="submit" name="enroll_course" 
                                    class="btn btn-primary w-100 btn-lg mb-3">
                                <i class="bi bi-cart-plus"></i> Daftar Kursus
                            </button>
                        </form>
                    <?php endif; ?>
                    
                    <div class="text-center mb-4">
                        <small class="text-muted">
                            <?php echo $course['total_siswa']; ?> siswa sudah mendaftar
                        </small>
                    </div>
                    
                    <hr>
                    
                    <h6>Fitur Kursus Ini:</h6>
                    <ul class="list-unstyled">
                        <li class="mb-2">
                            <i class="bi bi-check-circle text-success me-2"></i>
                            <?php echo $result_lms_lessons->num_rows; ?> Materi Pembelajaran
                        </li>
                        <li class="mb-2">
                            <i class="bi bi-check-circle text-success me-2"></i>
                            Akses Selamanya
                        </li>
                        <li class="mb-2">
                            <i class="bi bi-check-circle text-success me-2"></i>
                            Sertifikat Penyelesaian
                        </li>
                        <li class="mb-2">
                            <i class="bi bi-check-circle text-success me-2"></i>
                            Level: <?php echo ucfirst($course['level']); ?>
                        </li>
                        <li class="mb-2">
                            <i class="bi bi-check-circle text-success me-2"></i>
                            Durasi: <?php echo $course['durasi_jam']; ?> jam
                        </li>
                    </ul>
                </div>

                <!-- Other Teachers -->
                <div class="card border-0 shadow-sm mt-4">
                    <div class="card-body">
                        <h6 class="card-title">Guru Lainnya</h6>
                        <?php while($teacher = $result_teachers->fetch_assoc()): ?>
                            <div class="d-flex align-items-center mb-3">
                                <div class="flex-shrink-0">
                                    <i class="bi bi-person-circle" style="font-size: 2rem; color: #6c757d;"></i>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h6 class="mb-0"><?php echo $teacher['nama_lengkap']; ?></h6>
                                    <small class="text-muted"><?php echo $teacher['total_lms_courses']; ?> kursus</small>
                                </div>
                            </div>
                        <?php endwhile; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Related lms_courses -->
        <?php if ($result_related->num_rows > 0): ?>
        <div class="row mt-5">
            <div class="col-12">
                <h4 class="mb-4">Kursus Terkait</h4>
                <div class="row">
                    <?php while($related = $result_related->fetch_assoc()): ?>
                    <div class="col-md-3 mb-4">
                        <div class="card h-100">
                            <div class="card-body">
                                <span class="badge bg-light text-dark mb-2"><?php echo $course['nama_kategori']; ?></span>
                                <h6 class="card-title"><?php echo $related['judul']; ?></h6>
                                <p class="card-text small text-muted">
                                    <?php echo strlen($related['deskripsi']) > 60 ? 
                                          substr($related['deskripsi'], 0, 60) . '...' : 
                                          $related['deskripsi']; ?>
                                </p>
                                <div class="d-flex justify-content-between align-items-center">
                                    <small class="text-muted"><?php echo $related['nama_guru']; ?></small>
                                    <a href="course_detail.php?id=<?php echo $related['id']; ?>" 
                                       class="btn btn-outline-primary btn-sm">Lihat</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endwhile; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <!-- Footer -->
    <footer class="bg-dark text-white py-4 mt-5">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <h5><i class="bi bi-book-half"></i> SMANSAB LMS</h5>
                    <p>Sistem Manajemen Pembelajaran untuk Sekolah Menengah Atas Negeri 1 Bogor</p>
                </div>
                <div class="col-md-3">
                    <h5>Tautan Cepat</h5>
                    <ul class="list-unstyled">
                        <li><a href="dashboard.php" class="text-white">Beranda</a></li>
                        <li><a href="courses.php" class="text-white">Kursus</a></li>
                        <li><a href="teachers.php" class="text-white">Guru</a></li>
                    </ul>
                </div>
                <div class="col-md-3">
                    <h5>Kontak</h5>
                    <ul class="list-unstyled">
                        <li><i class="bi bi-geo-alt"></i> Jl. Merdeka No. 123, Bogor</li>
                        <li><i class="bi bi-telephone"></i> (0251) 123456</li>
                        <li><i class="bi bi-envelope"></i> info@smansab-lms.id</li>
                    </ul>
                </div>
            </div>
            <hr class="bg-light">
            <div class="text-center">
                <p>&copy; 2023 SMANSAB LMS. All rights reserved.</p>
            </div>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>