<?php
include 'config.php';

// Ambil data kursus populer
$sql_lms_courses = "SELECT c.*, u.nama_lengkap as nama_guru, cat.nama_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 
                ORDER BY c.rating DESC, c.total_siswa DESC 
                LIMIT 8";
$result_lms_courses = $conn->query($sql_lms_courses);

// Ambil data guru
$sql_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' 
                 GROUP BY u.id 
                 ORDER BY total_lms_courses DESC 
                 LIMIT 4";
$result_teachers = $conn->query($sql_teachers);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SMANSAB LMS - Learning Management System</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;
        }
        
        .hero-section {
            background: linear-gradient(135deg, var(--primary-color), #7986cb);
            color: white;
            padding: 80px 0;
        }
        
        .card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s;
        }
        
        .card:hover {
            transform: translateY(-5px);
        }
        
        .rating {
            color: #ffc107;
        }
        
        .btn-detail {
            background-color: var(--primary-color);
            color: white;
            border: none;
        }
        
        .btn-detail:hover {
            background-color: #303f9f;
            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 bi bi-book-half" href="index.php"> SMANSAB LMS</a>
            
            <div class="navbar-nav ms-auto">
                <?php if (isLoggedIn()): ?>
                    <span class="navbar-text me-3">Halo, <?php echo $_SESSION['nama_lengkap']; ?></span>
                    <a href="logout.php" class="btn btn-outline-primary">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>

    <!-- Hero Section -->
    <section class="hero-section">
        <div class="container text-center">
            <h1 class="display-4 fw-bold mb-4">Mulai Berinvestasi dalam Masa Depan Anda Hari Ini</h1>
            <p class="lead mb-4">Buka kunci pengetahuan dan keterampilan dengan kursus-kursus berkualitas yang diajarkan oleh guru-guru terbaik.</p>
            <a href="courses.php" class="btn btn-light btn-lg">JELAJAHI KURSUS</a>
        </div>
    </section>

    <!-- Popular lms_courses -->
    <div class="container my-5">
        <h2 class="text-center mb-4">Kursus Populer</h2>
        <div class="row">
            <?php while($course = $result_lms_courses->fetch_assoc()): ?>
            <div class="col-md-3 mb-4">
                <div class="card h-100">
                    <div class="card-body d-flex flex-column">
                        <div class="d-flex justify-content-between align-items-start mb-2">
                            <span class="badge bg-<?php echo $course['harga'] == 0 ? 'success' : 'warning'; ?>">
                                <?php echo $course['harga'] == 0 ? 'Gratis' : 'Rp ' . number_format($course['harga'], 0, ',', '.'); ?>
                            </span>
                            <div class="rating">
                                <i class="bi bi-star-fill"></i>
                                <span><?php echo $course['rating']; ?></span>
                            </div>
                        </div>
                        <span class="badge bg-light text-dark mb-2"><?php echo $course['nama_kategori']; ?></span>
                        <h6 class="card-title"><?php echo $course['judul']; ?></h6>
                        <p class="card-text small text-muted">Oleh: <?php echo $course['nama_guru']; ?></p>
                        <div class="d-flex justify-content-between text-muted small mb-3">
                            <span><i class="bi bi-clock"></i> <?php echo $course['durasi_jam']; ?> jam</span>
                            <span><i class="bi bi-people"></i> <?php echo $course['total_siswa']; ?> siswa</span>
                        </div>
                        <a href="course_detail.php?id=<?php echo $course['id']; ?>" class="btn btn-detail mt-auto">Lihat Detail</a>
                    </div>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
    </div>

    <!-- Teacher of the Month -->
    <div class="container my-5">
        <h2 class="text-center mb-4">Guru Terbaik</h2>
        <div class="row">
            <?php while($teacher = $result_teachers->fetch_assoc()): ?>
            <div class="col-md-3 mb-4">
                <div class="card text-center">
                    <div class="card-body">
                        <div class="mb-3">
                            <i class="bi bi-person-circle" style="font-size: 3rem; color: #6c757d;"></i>
                        </div>
                        <h5 class="card-title"><?php echo $teacher['nama_lengkap']; ?></h5>
                        <p class="card-text text-muted"><?php echo $teacher['total_lms_courses']; ?> Kursus</p>
                        <a href="teacher_courses.php?id=<?php echo $teacher['id']; ?>" class="btn btn-outline-primary btn-sm">Lihat Kursus</a>
                    </div>
                </div>
            </div>
            <?php endwhile; ?>
        </div>
    </div>

    <!-- Footer -->
    <footer class="bg-dark text-white py-4 mt-5">
        <div class="container text-center">
            <p>&copy; 2023 SMANSAB LMS. All rights reserved.</p>
        </div>
    </footer>

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