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

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

$guru_id = $_SESSION['user_id'];

// Ambil data guru
$sql_guru = "SELECT * FROM lms_users WHERE id = ?";
$stmt_guru = $conn->prepare($sql_guru);
$stmt_guru->bind_param('i', $guru_id);
$stmt_guru->execute();
$result_guru = $stmt_guru->get_result();
$guru = $result_guru->fetch_assoc();

// Ambil statistik guru
$sql_stats = "SELECT 
    (SELECT COUNT(*) FROM lms_courses WHERE id_guru = ?) as total_lms_courses,
    (SELECT COUNT(*) FROM lms_enrollments e 
     JOIN lms_courses c ON e.id_course = c.id 
     WHERE c.id_guru = ?) as total_students,
    (SELECT AVG(rating) FROM lms_courses WHERE id_guru = ?) as avg_rating,
    (SELECT COUNT(*) FROM lms_lessons l 
     JOIN lms_courses c ON l.id_course = c.id 
     WHERE c.id_guru = ?) as total_lms_lessons";
$stmt_stats = $conn->prepare($sql_stats);
$stmt_stats->bind_param('iiii', $guru_id, $guru_id, $guru_id, $guru_id);
$stmt_stats->execute();
$stats = $stmt_stats->get_result()->fetch_assoc();

// Ambil kursus yang diajar
$sql_lms_courses = "SELECT c.*, cat.nama_kategori,
                COUNT(e.id) as total_lms_enrollments,
                AVG(e.progress) as avg_progress
                FROM lms_courses c 
                LEFT JOIN lms_categories cat ON c.id_kategori = cat.id 
                LEFT JOIN lms_enrollments e ON c.id = e.id_course 
                WHERE c.id_guru = ?
                GROUP BY c.id 
                ORDER BY c.created_at DESC 
                LIMIT 5";
$stmt_lms_courses = $conn->prepare($sql_lms_courses);
$stmt_lms_courses->bind_param('i', $guru_id);
$stmt_lms_courses->execute();
$result_lms_courses = $stmt_lms_courses->get_result();

// Ambil aktivitas terbaru (lms_enrollments baru)
$sql_recent_activity = "SELECT e.*, c.judul as course_title, 
                        u.nama_lengkap as student_name,
                        e.tanggal_enroll
                        FROM lms_enrollments e 
                        JOIN lms_courses c ON e.id_course = c.id 
                        JOIN lms_users u ON e.id_siswa = u.id 
                        WHERE c.id_guru = ? 
                        ORDER BY e.tanggal_enroll DESC 
                        LIMIT 5";
$stmt_activity = $conn->prepare($sql_recent_activity);
$stmt_activity->bind_param('i', $guru_id);
$stmt_activity->execute();
$result_activity = $stmt_activity->get_result();

// Ambil kursus dengan progress tertinggi
$sql_top_lms_courses = "SELECT c.*, 
                    COUNT(e.id) as total_students,
                    AVG(e.progress) as avg_progress
                    FROM lms_courses c 
                    LEFT JOIN lms_enrollments e ON c.id = e.id_course 
                    WHERE c.id_guru = ? 
                    GROUP BY c.id 
                    ORDER BY avg_progress DESC 
                    LIMIT 3";
$stmt_top = $conn->prepare($sql_top_lms_courses);
$stmt_top->bind_param('i', $guru_id);
$stmt_top->execute();
$result_top = $stmt_top->get_result();
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard Guru - 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);
        }
        
        .stat-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            transition: transform 0.3s;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
        }
        
        .course-card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            transition: transform 0.3s;
            margin-bottom: 15px;
        }
        
        .course-card:hover {
            transform: translateY(-3px);
        }
        
        .welcome-section {
            background: linear-gradient(135deg, var(--primary-color), #7986cb);
            color: white;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
        }
        
        .progress-course {
            height: 8px;
            border-radius: 4px;
        }
        
        .bg-lms_courses {
            background: linear-gradient(45deg, #667eea, #764ba2);
        }
        
        .bg-students {
            background: linear-gradient(45deg, #f093fb, #f5576c);
        }
        
        .bg-rating {
            background: linear-gradient(45deg, #4facfe, #00f2fe);
        }
        
        .bg-lms_lessons {
            background: linear-gradient(45deg, #43e97b, #38f9d7);
        }
        
        .activity-item {
            border-left: 3px solid var(--primary-color);
            padding-left: 15px;
            margin-bottom: 15px;
        }
    </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 active" 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">
                                <i class="bi bi-collection-play"></i> Kelola Materi
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="manage_quizzes.php">
                                <i class="bi bi-pencil-square"></i> Kelola Kuis
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="students_progress.php">
                                <i class="bi bi-graph-up"></i> Progress Siswa
                            </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">Dashboard Guru</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 $guru['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 mt-4">
                    <!-- Welcome Section -->
                    <div class="welcome-section">
                        <div class="row align-items-center">
                            <div class="col-md-8">
                                <h3 class="fw-bold">Selamat Datang, <?php echo $guru['nama_lengkap']; ?>! 👨‍🏫</h3>
                                <p class="mb-0">Kelola kursus dan pantau perkembangan siswa Anda dengan mudah.</p>
                            </div>
                            <div class="col-md-4 text-end">
                                <div class="bg-white rounded p-3 text-dark d-inline-block">
                                    <small class="text-muted">Hari Ini</small>
                                    <h4 class="mb-0 fw-bold"><?php echo date('d F Y'); ?></h4>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Statistics Cards -->
                    <div class="row mb-4">
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0 text-white bg-lms_courses">
                                <div class="card-body">
                                    <div class="row align-items-center">
                                        <div class="col-8">
                                            <h6 class="card-title mb-2">Total Kursus</h6>
                                            <h2 class="fw-bold"><?php echo $stats['total_lms_courses']; ?></h2>
                                            <small>Kursus yang diajar</small>
                                        </div>
                                        <div class="col-4 text-end">
                                            <i class="bi bi-journal-bookmark" style="font-size: 2.5rem;"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0 text-white bg-students">
                                <div class="card-body">
                                    <div class="row align-items-center">
                                        <div class="col-8">
                                            <h6 class="card-title mb-2">Total Siswa</h6>
                                            <h2 class="fw-bold"><?php echo $stats['total_students']; ?></h2>
                                            <small>Siswa terdaftar</small>
                                        </div>
                                        <div class="col-4 text-end">
                                            <i class="bi bi-people" style="font-size: 2.5rem;"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0 text-white bg-rating">
                                <div class="card-body">
                                    <div class="row align-items-center">
                                        <div class="col-8">
                                            <h6 class="card-title mb-2">Rating Rata-rata</h6>
                                            <h2 class="fw-bold"><?php echo number_format($stats['avg_rating'] ?? 0, 1); ?></h2>
                                            <small>Penilaian kursus</small>
                                        </div>
                                        <div class="col-4 text-end">
                                            <i class="bi bi-star" style="font-size: 2.5rem;"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-xl-3 col-md-6 mb-4">
                            <div class="card stat-card border-0 text-white bg-lms_lessons">
                                <div class="card-body">
                                    <div class="row align-items-center">
                                        <div class="col-8">
                                            <h6 class="card-title mb-2">Total Materi</h6>
                                            <h2 class="fw-bold"><?php echo $stats['total_lms_lessons']; ?></h2>
                                            <small>Materi pembelajaran</small>
                                        </div>
                                        <div class="col-4 text-end">
                                            <i class="bi bi-collection-play" style="font-size: 2.5rem;"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <!-- Kursus Saya -->
                        <div class="col-lg-6 mb-4">
                            <div class="card border-0 shadow-sm">
                                <div class="card-header bg-white border-0 d-flex justify-content-between align-items-center">
                                    <h6 class="mb-0">
                                        <i class="bi bi-journal-bookmark text-primary me-2"></i>
                                        Kursus Saya
                                    </h6>
                                    <a href="my_courses.php" class="btn btn-outline-primary btn-sm">
                                        Lihat Semua
                                    </a>
                                </div>
                                <div class="card-body">
                                    <?php if ($result_lms_courses->num_rows > 0): ?>
                                        <?php while($course = $result_lms_courses->fetch_assoc()): ?>
                                            <div class="course-card card">
                                                <div class="card-body">
                                                    <div class="row align-items-center">
                                                        <div class="col-8">
                                                            <h6 class="card-title mb-1"><?php echo $course['judul']; ?></h6>
                                                            <small class="text-muted">
                                                                <?php echo $course['nama_kategori']; ?> • 
                                                                <?php echo $course['total_lms_enrollments']; ?> siswa
                                                            </small>
                                                            <div class="mt-2">
                                                                <div class="d-flex justify-content-between mb-1">
                                                                    <small>Progress Rata-rata</small>
                                                                    <small><?php echo number_format($course['avg_progress'] ?? 0, 1); ?>%</small>
                                                                </div>
                                                                <div class="progress progress-course">
                                                                    <div class="progress-bar" 
                                                                         style="width: <?php echo $course['avg_progress'] ?? 0; ?>%">
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="col-4 text-end">
                                                            <div class="rating text-warning mb-2">
                                                                <i class="bi bi-star-fill"></i>
                                                                <small><?php echo $course['rating']; ?></small>
                                                            </div>
                                                            <a href="course_detail.php?id=<?php echo $course['id']; ?>" 
                                                               class="btn btn-primary btn-sm">
                                                                Kelola
                                                            </a>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endwhile; ?>
                                    <?php else: ?>
                                        <div class="text-center py-4">
                                            <i class="bi bi-journal-plus text-muted" style="font-size: 3rem;"></i>
                                            <p class="text-muted mt-2">Belum ada kursus</p>
                                            <a href="create_course.php" class="btn btn-primary btn-sm">
                                                Buat Kursus Pertama
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>

                        <!-- Aktivitas Terbaru -->
                        <div class="col-lg-6 mb-4">
                            <div class="card border-0 shadow-sm">
                                <div class="card-header bg-white border-0">
                                    <h6 class="mb-0">
                                        <i class="bi bi-clock text-warning me-2"></i>
                                        Aktivitas Terbaru
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <?php if ($result_activity->num_rows > 0): ?>
                                        <div class="list-group list-group-flush">
                                            <?php while($activity = $result_activity->fetch_assoc()): ?>
                                                <div class="list-group-item px-0">
                                                    <div class="activity-item">
                                                        <div class="d-flex align-items-center">
                                                            <div class="flex-shrink-0">
                                                                <div class="bg-light rounded p-2">
                                                                    <i class="bi bi-person-plus text-success"></i>
                                                                </div>
                                                            </div>
                                                            <div class="flex-grow-1 ms-3">
                                                                <h6 class="mb-0 small"><?php echo $activity['student_name']; ?></h6>
                                                                <small class="text-muted">
                                                                    Bergabung di "<?php echo $activity['course_title']; ?>"
                                                                </small>
                                                                <div class="mt-1">
                                                                    <small class="text-muted">
                                                                        <?php echo date('d M Y H:i', strtotime($activity['tanggal_enroll'])); ?>
                                                                    </small>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            <?php endwhile; ?>
                                        </div>
                                    <?php else: ?>
                                        <div class="text-center py-4">
                                            <i class="bi bi-clock text-muted" style="font-size: 3rem;"></i>
                                            <p class="text-muted mt-2">Belum ada aktivitas terbaru</p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <!-- Kursus dengan Progress Tertinggi -->
                            <div class="card border-0 shadow-sm mt-4">
                                <div class="card-header bg-white border-0">
                                    <h6 class="mb-0">
                                        <i class="bi bi-trophy text-success me-2"></i>
                                        Top Performing lms_courses
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <?php if ($result_top->num_rows > 0): ?>
                                        <?php while($course = $result_top->fetch_assoc()): ?>
                                            <div class="d-flex align-items-center mb-3">
                                                <div class="flex-shrink-0">
                                                    <div class="bg-success rounded p-2">
                                                        <i class="bi bi-graph-up text-white"></i>
                                                    </div>
                                                </div>
                                                <div class="flex-grow-1 ms-3">
                                                    <h6 class="mb-0 small"><?php echo $course['judul']; ?></h6>
                                                    <small class="text-muted">
                                                        Progress: <?php echo number_format($course['avg_progress'] ?? 0, 1); ?>%
                                                    </small>
                                                </div>
                                                <div class="flex-shrink-0">
                                                    <span class="badge bg-primary"><?php echo $course['total_students']; ?> siswa</span>
                                                </div>
                                            </div>
                                        <?php endwhile; ?>
                                    <?php else: ?>
                                        <div class="text-center py-3">
                                            <small class="text-muted">Belum ada data progress</small>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Quick Actions -->
                    <div class="row mt-4">
                        <div class="col-12">
                            <div class="card border-0 shadow-sm">
                                <div class="card-header bg-white border-0">
                                    <h6 class="mb-0">
                                        <i class="bi bi-lightning me-2"></i>
                                        Quick Actions
                                    </h6>
                                </div>
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-md-3 mb-3">
                                            <a href="create_course.php" class="btn btn-primary w-100 h-100 py-3">
                                                <i class="bi bi-plus-circle d-block mb-2" style="font-size: 1.5rem;"></i>
                                                Buat Kursus Baru
                                            </a>
                                        </div>
                                        <div class="col-md-3 mb-3">
                                            <a href="manage_lessons.php" class="btn btn-success w-100 h-100 py-3">
                                                <i class="bi bi-collection-play d-block mb-2" style="font-size: 1.5rem;"></i>
                                                Kelola Materi
                                            </a>
                                        </div>
                                        <div class="col-md-3 mb-3">
                                            <a href="manage_quizzes.php" class="btn btn-warning w-100 h-100 py-3">
                                                <i class="bi bi-pencil-square d-block mb-2" style="font-size: 1.5rem;"></i>
                                                Buat Kuis
                                            </a>
                                        </div>
                                        <div class="col-md-3 mb-3">
                                            <a href="students_progress.php" class="btn btn-info w-100 h-100 py-3">
                                                <i class="bi bi-graph-up d-block mb-2" style="font-size: 1.5rem;"></i>
                                                Lihat Progress
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </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>
        // Update greeting based on time
        function updateGreeting() {
            const hour = new Date().getHours();
            const greeting = document.querySelector('.welcome-section h3');
            let newGreeting = 'Selamat Datang, <?php echo $guru['nama_lengkap']; ?>! 👨‍🏫';
            
            if (hour < 12) newGreeting = 'Selamat Pagi, <?php echo $guru['nama_lengkap']; ?>! 🌅';
            else if (hour < 18) newGreeting = 'Selamat Siang, <?php echo $guru['nama_lengkap']; ?>! ☀️';
            else newGreeting = 'Selamat Malam, <?php echo $guru['nama_lengkap']; ?>! 🌙';
            
            if (greeting) greeting.textContent = newGreeting;
        }
        
        document.addEventListener('DOMContentLoaded', updateGreeting);
    </script>
</body>
</html>