<?php
include 'config.php';

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

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

// Ambil data guru
$sql_teacher = "SELECT u.*, 
                COUNT(DISTINCT c.id) as total_lms_courses,
                COUNT(DISTINCT e.id) as total_students,
                AVG(c.rating) as avg_rating
                FROM lms_users u 
                LEFT JOIN lms_courses c ON u.id = c.id_guru 
                LEFT JOIN lms_enrollments e ON c.id = e.id_course 
                WHERE u.id = ? AND u.role = 'guru' 
                GROUP BY u.id";
$stmt_teacher = $conn->prepare($sql_teacher);
$stmt_teacher->bind_param('i', $teacher_id);
$stmt_teacher->execute();
$result_teacher = $stmt_teacher->get_result();

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

$teacher = $result_teacher->fetch_assoc();

// Ambil kursus yang diajarkan oleh guru ini
$sql_lms_courses = "SELECT c.*, cat.nama_kategori,
                COUNT(DISTINCT e.id) as total_lms_enrollments,
                (SELECT COUNT(*) FROM lms_lessons WHERE id_course = c.id) as total_lms_lessons
                FROM lms_courses c 
                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";
$stmt_lms_courses = $conn->prepare($sql_lms_courses);
$stmt_lms_courses->bind_param('i', $teacher_id);
$stmt_lms_courses->execute();
$result_lms_courses = $stmt_lms_courses->get_result();

// Ambil guru lainnya untuk rekomendasi
$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 4";
$stmt_other = $conn->prepare($sql_other_teachers);
$stmt_other->bind_param('i', $teacher_id);
$stmt_other->execute();
$result_other = $stmt_other->get_result();

// Hitung pengalaman guru (default 2 tahun jika tidak ada data)
$teacher_experience = 2; // Default experience in years
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kursus oleh <?php echo $teacher['nama_lengkap']; ?> - 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;
        }
        
        .teacher-hero {
            background: linear-gradient(135deg, var(--primary-color), #7986cb);
            color: white;
            padding: 60px 0;
        }
        
        .teacher-avatar {
            width: 120px;
            height: 120px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 3rem;
            margin: 0 auto 20px;
            border: 4px solid rgba(255, 255, 255, 0.3);
        }
        
        .course-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            transition: transform 0.3s;
            margin-bottom: 20px;
        }
        
        .course-card:hover {
            transform: translateY(-5px);
        }
        
        .course-image {
            height: 120px;
            background: linear-gradient(45deg, #667eea, #764ba2);
            border-radius: 15px 15px 0 0;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 2.5rem;
        }
        
        .rating {
            color: #ffc107;
        }
        
        .stats-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        
        .teacher-card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            transition: transform 0.3s;
        }
        
        .teacher-card:hover {
            transform: translateY(-3px);
        }
        
        .empty-state {
            text-align: center;
            padding: 60px 20px;
        }
        
        .empty-state i {
            font-size: 4rem;
            color: #6c757d;
            margin-bottom: 20px;
        }
    </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="siswa/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>

    <!-- Teacher Hero Section -->
    <section class="teacher-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="index.php#teachers" class="text-white">Guru</a></li>
                            <li class="breadcrumb-item active text-white" aria-current="page"><?php echo $teacher['nama_lengkap']; ?></li>
                        </ol>
                    </nav>
                    
                    <div class="teacher-avatar">
                        <i class="bi bi-person-circle"></i>
                    </div>
                    
                    <h1 class="display-5 fw-bold mb-3"><?php echo $teacher['nama_lengkap']; ?></h1>
                    <p class="lead mb-4">Guru berpengalaman dengan spesialisasi dalam berbagai bidang pembelajaran</p>
                    
                    <div class="d-flex flex-wrap gap-4">
                        <div class="text-center">
                            <h3 class="fw-bold mb-0"><?php echo $teacher['total_lms_courses']; ?></h3>
                            <small>Total Kursus</small>
                        </div>
                        <div class="text-center">
                            <h3 class="fw-bold mb-0"><?php echo $teacher['total_students']; ?></h3>
                            <small>Total Siswa</small>
                        </div>
                        <div class="text-center">
                            <h3 class="fw-bold mb-0"><?php echo number_format($teacher['avg_rating'] ?? 0, 1); ?></h3>
                            <small>Rating Rata-rata</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <div class="container my-5">
        <div class="row">
            <!-- Main Content -->
            <div class="col-lg-8">
                <!-- Teacher's lms_courses -->
                <div class="card border-0 shadow-sm mb-4">
                    <div class="card-header bg-white border-0">
                        <h4 class="mb-0">
                            <i class="bi bi-journal-bookmark text-primary me-2"></i>
                            Kursus oleh <?php echo $teacher['nama_lengkap']; ?>
                        </h4>
                    </div>
                    <div class="card-body">
                        <?php if ($result_lms_courses->num_rows > 0): ?>
                            <div class="row">
                                <?php while($course = $result_lms_courses->fetch_assoc()): ?>
                                    <div class="col-md-6 mb-4">
                                        <div class="card course-card h-100">
                                            <div class="course-image">
                                                <i class="bi bi-laptop"></i>
                                            </div>
                                            
                                            <div class="card-body">
                                                <div class="d-flex justify-content-between align-items-start mb-2">
                                                    <span class="badge bg-light text-dark">
                                                        <?php echo $course['nama_kategori']; ?>
                                                    </span>
                                                    <div class="rating">
                                                        <i class="bi bi-star-fill"></i>
                                                        <small><?php echo $course['rating']; ?></small>
                                                    </div>
                                                </div>
                                                
                                                <h6 class="card-title fw-bold"><?php echo $course['judul']; ?></h6>
                                                <p class="card-text small text-muted">
                                                    <?php echo strlen($course['deskripsi']) > 100 ? 
                                                        substr($course['deskripsi'], 0, 100) . '...' : 
                                                        $course['deskripsi']; ?>
                                                </p>
                                                
                                                <div class="mb-3">
                                                    <div class="d-flex justify-content-between text-muted small mb-1">
                                                        <span><i class="bi bi-clock"></i> <?php echo $course['durasi_jam']; ?> jam</span>
                                                        <span><i class="bi bi-journal-text"></i> <?php echo $course['total_lms_lessons']; ?> materi</span>
                                                    </div>
                                                    <div class="d-flex justify-content-between text-muted small">
                                                        <span><i class="bi bi-people"></i> <?php echo $course['total_lms_enrollments']; ?> siswa</span>
                                                        <span class="badge <?php echo $course['level'] == 'pemula' ? 'bg-success' : ($course['level'] == 'menengah' ? 'bg-warning' : 'bg-danger'); ?>">
                                                            <?php echo ucfirst($course['level']); ?>
                                                        </span>
                                                    </div>
                                                </div>
                                                
                                                <div class="d-flex justify-content-between align-items-center">
                                                    <?php if ($course['harga'] == 0): ?>
                                                        <span class="text-success fw-bold">Gratis</span>
                                                    <?php else: ?>
                                                        <span class="text-primary fw-bold">Rp <?php echo number_format($course['harga'], 0, ',', '.'); ?></span>
                                                    <?php endif; ?>
                                                    <a href="course_detail.php?id=<?php echo $course['id']; ?>" 
                                                       class="btn btn-primary btn-sm">
                                                        Lihat Detail
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endwhile; ?>
                            </div>
                        <?php else: ?>
                            <div class="empty-state">
                                <i class="bi bi-journal-x"></i>
                                <h4 class="text-muted">Belum ada kursus</h4>
                                <p class="text-muted">Guru ini belum membuat kursus apapun.</p>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- About Teacher -->
                <div class="card border-0 shadow-sm">
                    <div class="card-header bg-white border-0">
                        <h4 class="mb-0">
                            <i class="bi bi-person text-primary me-2"></i>
                            Tentang <?php echo $teacher['nama_lengkap']; ?>
                        </h4>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-8">
                                <h5>Biografi</h5>
                                <p>
                                    <?php echo $teacher['nama_lengkap']; ?> adalah seorang guru berpengalaman dengan 
                                    <?php echo $teacher['total_lms_courses']; ?> kursus dan telah mengajar 
                                    <?php echo $teacher['total_students']; ?> siswa. Dengan rating rata-rata 
                                    <?php echo number_format($teacher['avg_rating'] ?? 0, 1); ?>, 
                                    <?php echo $teacher['nama_lengkap']; ?> berkomitmen untuk memberikan 
                                    pengalaman belajar yang berkualitas dan menyenangkan bagi semua siswa.
                                </p>
                                
                                <h5 class="mt-4">Spesialisasi</h5>
                                <div class="d-flex flex-wrap gap-2">
                                    <?php 
                                    // Ambil kategori unik dari kursus guru ini
                                    $sql_lms_categories = "SELECT DISTINCT cat.nama_kategori 
                                                     FROM lms_courses c 
                                                     JOIN lms_categories cat ON c.id_kategori = cat.id 
                                                     WHERE c.id_guru = ?";
                                    $stmt_cat = $conn->prepare($sql_lms_categories);
                                    $stmt_cat->bind_param('i', $teacher_id);
                                    $stmt_cat->execute();
                                    $result_cat = $stmt_cat->get_result();
                                    
                                    while($category = $result_cat->fetch_assoc()): 
                                    ?>
                                        <span class="badge bg-primary"><?php echo $category['nama_kategori']; ?></span>
                                    <?php endwhile; ?>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <h5>Informasi Kontak</h5>
                                <ul class="list-unstyled">
                                    <li class="mb-2">
                                        <i class="bi bi-envelope text-primary me-2"></i>
                                        <?php echo $teacher['email']; ?>
                                    </li>
                                    <li class="mb-2">
                                        <i class="bi bi-journal text-primary me-2"></i>
                                        <?php echo $teacher['total_lms_courses']; ?> Kursus dibuat
                                    </li>
                                    <li class="mb-2">
                                        <i class="bi bi-people text-primary me-2"></i>
                                        <?php echo $teacher['total_students']; ?> Total siswa
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Sidebar -->
            <div class="col-lg-4">
                <!-- Teacher Stats -->
                <div class="stats-card">
                    <h6 class="mb-3">Statistik Guru</h6>
                    <div class="row text-center">
                        <div class="col-6 mb-3">
                            <div class="bg-primary rounded-circle p-3 mx-auto d-inline-flex mb-2">
                                <i class="bi bi-journal-bookmark text-white"></i>
                            </div>
                            <h4 class="fw-bold mb-0"><?php echo $teacher['total_lms_courses']; ?></h4>
                            <small class="text-muted">Kursus</small>
                        </div>
                        <div class="col-6 mb-3">
                            <div class="bg-success rounded-circle p-3 mx-auto d-inline-flex mb-2">
                                <i class="bi bi-people text-white"></i>
                            </div>
                            <h4 class="fw-bold mb-0"><?php echo $teacher['total_students']; ?></h4>
                            <small class="text-muted">Siswa</small>
                        </div>
                        <div class="col-6">
                            <div class="bg-warning rounded-circle p-3 mx-auto d-inline-flex mb-2">
                                <i class="bi bi-star text-white"></i>
                            </div>
                            <h4 class="fw-bold mb-0"><?php echo number_format($teacher['avg_rating'] ?? 0, 1); ?></h4>
                            <small class="text-muted">Rating</small>
                        </div>
                        <div class="col-6">
                            <div class="bg-info rounded-circle p-3 mx-auto d-inline-flex mb-2">
                                <i class="bi bi-award text-white"></i>
                            </div>
                            <h4 class="fw-bold mb-0"><?php echo $teacher_experience; ?>+</h4>
                            <small class="text-muted">Tahun</small>
                        </div>
                    </div>
                </div>

                <!-- Other Teachers -->
                <div class="card border-0 shadow-sm">
                    <div class="card-body">
                        <h6 class="card-title mb-3">Guru Lainnya</h6>
                        <?php if ($result_other->num_rows > 0): ?>
                            <?php while($other_teacher = $result_other->fetch_assoc()): ?>
                                <div class="d-flex align-items-center mb-3">
                                    <div class="flex-shrink-0">
                                        <div class="bg-light rounded-circle p-2">
                                            <i class="bi bi-person-circle text-primary"></i>
                                        </div>
                                    </div>
                                    <div class="flex-grow-1 ms-3">
                                        <h6 class="mb-0 small"><?php echo $other_teacher['nama_lengkap']; ?></h6>
                                        <small class="text-muted"><?php echo $other_teacher['total_lms_courses']; ?> kursus</small>
                                    </div>
                                    <div class="flex-shrink-0">
                                        <a href="teacher_courses.php?id=<?php echo $other_teacher['id']; ?>" 
                                           class="btn btn-outline-primary btn-sm">
                                            Lihat
                                        </a>
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <p class="text-muted small">Tidak ada guru lainnya</p>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Popular lms_courses by This Teacher -->
                <div class="card border-0 shadow-sm mt-4">
                    <div class="card-body">
                        <h6 class="card-title mb-3">Kursus Populer</h6>
                        <?php 
                        // Ambil kursus populer guru ini
                        $sql_popular = "SELECT c.*, COUNT(e.id) as total_lms_enrollments 
                                       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 total_lms_enrollments DESC 
                                       LIMIT 3";
                        $stmt_popular = $conn->prepare($sql_popular);
                        $stmt_popular->bind_param('i', $teacher_id);
                        $stmt_popular->execute();
                        $result_popular = $stmt_popular->get_result();
                        ?>
                        
                        <?php if ($result_popular->num_rows > 0): ?>
                            <?php while($popular = $result_popular->fetch_assoc()): ?>
                                <div class="d-flex align-items-center mb-3">
                                    <div class="flex-shrink-0">
                                        <div class="bg-primary rounded p-2 text-white">
                                            <i class="bi bi-play-circle"></i>
                                        </div>
                                    </div>
                                    <div class="flex-grow-1 ms-3">
                                        <h6 class="mb-0 small"><?php echo $popular['judul']; ?></h6>
                                        <small class="text-muted"><?php echo $popular['total_lms_enrollments']; ?> siswa</small>
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <p class="text-muted small">Belum ada kursus populer</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </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="index.php" class="text-white">Beranda</a></li>
                        <li><a href="courses.php" class="text-white">Kursus</a></li>
                        <li><a href="index.php#teachers" 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>