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

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

// Get course ID from URL
if (!isset($_GET['id']) || empty($_GET['id'])) {
    $_SESSION['error'] = "Course ID tidak valid!";
    header("Location: manage_courses.php");
    exit();
}

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

// Get course data with joins
$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 = $course_id";
$course_result = $conn->query($sql_course);

if ($course_result->num_rows == 0) {
    $_SESSION['error'] = "Kursus tidak ditemukan!";
    header("Location: manage_courses.php");
    exit();
}

$course = $course_result->fetch_assoc();

// Get course statistics
$sql_stats = "SELECT 
    (SELECT COUNT(*) FROM lms_enrollments WHERE id_course = $course_id) as total_lms_enrollments,
    (SELECT COUNT(*) FROM lms_enrollments WHERE id_course = $course_id AND status = 'completed') as completed_lms_enrollments,
    (SELECT COUNT(*) FROM lms_lessons WHERE id_course = $course_id) as total_lms_lessons,
    (SELECT SUM(durasi_menit) FROM lms_lessons WHERE id_course = $course_id) as total_minutes";

$stats = $conn->query($sql_stats)->fetch_assoc();

// Get recent lms_enrollments
$sql_lms_enrollments = "SELECT e.*, u.nama_lengkap, u.email 
                   FROM lms_enrollments e 
                   JOIN lms_users u ON e.id_siswa = u.id 
                   WHERE e.id_course = $course_id 
                   ORDER BY e.tanggal_enroll DESC 
                   LIMIT 5";
$recent_lms_enrollments = $conn->query($sql_lms_enrollments);

// Get course lms_lessons
$sql_lms_lessons = "SELECT * FROM lms_lessons 
               WHERE id_course = $course_id 
               ORDER BY urutan ASC";
$lms_lessons = $conn->query($sql_lms_lessons);
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Detail Kursus - 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>
        .course-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 2rem 0;
            margin-bottom: 2rem;
        }
        .course-image {
            width: 100%;
            height: 300px;
            object-fit: cover;
            border-radius: 10px;
        }
        .stat-card {
            border: none;
            border-radius: 10px;
            transition: transform 0.3s;
        }
        .stat-card:hover {
            transform: translateY(-5px);
        }
        .badge-level-pemula { background-color: #28a745; }
        .badge-level-menengah { background-color: #ffc107; color: #000; }
        .badge-level-lanjutan { background-color: #dc3545; }
        .lesson-item {
            border-left: 4px solid #007bff;
            padding-left: 1rem;
            margin-bottom: 1rem;
        }
        .progress {
            height: 8px;
        }
    </style>
</head>
<body>
    <!-- Navbar Admin -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand fw-bold" href="dashboard.php">SMANSAB LMS - Admin</a>
            <div class="navbar-nav ms-auto">
                <a class="nav-link" href="manage_users.php">Kelola User</a>
                <a class="nav-link" href="manage_courses.php">Kelola Kursus</a>
                <a class="nav-link" href="../logout.php">Logout</a>
            </div>
        </div>
    </nav>

    <!-- Course Header -->
    <div class="course-header">
        <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="dashboard.php" class="text-white">Dashboard</a></li>
                            <li class="breadcrumb-item"><a href="manage_courses.php" class="text-white">Kelola Kursus</a></li>
                            <li class="breadcrumb-item active text-white">Detail Kursus</li>
                        </ol>
                    </nav>
                    <h1 class="display-5 fw-bold"><?php echo htmlspecialchars($course['judul']); ?></h1>
                    <p class="lead mb-0">Oleh: <?php echo htmlspecialchars($course['nama_guru']); ?></p>
                </div>
                <div class="col-md-4 text-end">
                    <a href="edit_course.php?id=<?php echo $course_id; ?>" class="btn btn-light me-2">
                        <i class="bi bi-pencil"></i> Edit Kursus
                    </a>
                    <a href="manage_lessons.php?course_id=<?php echo $course_id; ?>" class="btn btn-outline-light">
                        <i class="bi bi-journals"></i> Kelola Materi
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="container">
        <!-- Alert Messages -->
        <?php if (isset($_SESSION['success'])): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <?php echo $_SESSION['success']; unset($_SESSION['success']); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <?php if (isset($_SESSION['error'])): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <?php echo $_SESSION['error']; unset($_SESSION['error']); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <div class="row">
            <!-- Main Content -->
            <div class="col-lg-8">
                <!-- Course Info Card -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="card-title mb-0"><i class="bi bi-info-circle"></i> Informasi Kursus</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-4">
                                <?php if (!empty($course['gambar_course'])): ?>
                                    <img src="../<?php echo htmlspecialchars($course['gambar_course']); ?>" 
                                         alt="Course Image" class="course-image mb-3">
                                <?php else: ?>
                                    <div class="course-image bg-light d-flex align-items-center justify-content-center mb-3">
                                        <i class="bi bi-image text-muted" style="font-size: 3rem;"></i>
                                    </div>
                                <?php endif; ?>
                            </div>
                            <div class="col-md-8">
                                <p><strong>Deskripsi:</strong></p>
                                <p><?php echo nl2br(htmlspecialchars($course['deskripsi'])); ?></p>
                                
                                <div class="row mt-3">
                                    <div class="col-md-6">
                                        <p><strong>Kategori:</strong><br>
                                            <span class="badge bg-info"><?php echo htmlspecialchars($course['nama_kategori']); ?></span>
                                        </p>
                                        <p><strong>Level:</strong><br>
                                            <span class="badge badge-level-<?php echo $course['level']; ?>">
                                                <?php echo ucfirst($course['level']); ?>
                                            </span>
                                        </p>
                                    </div>
                                    <div class="col-md-6">
                                        <p><strong>Harga:</strong><br>
                                            <span class="badge bg-<?php echo $course['harga'] == 0 ? 'success' : 'warning'; ?> fs-6">
                                                <?php echo $course['harga'] == 0 ? 'Gratis' : 'Rp ' . number_format($course['harga'], 0, ',', '.'); ?>
                                            </span>
                                        </p>
                                        <p><strong>Durasi:</strong><br>
                                            <span class="badge bg-secondary"><?php echo $course['durasi_jam']; ?> jam</span>
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- lms_lessons Section -->
                <div class="card mb-4">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="card-title mb-0"><i class="bi bi-journals"></i> Materi Pembelajaran</h5>
                        <span class="badge bg-primary"><?php echo $lms_lessons->num_rows; ?> Materi</span>
                    </div>
                    <div class="card-body">
                        <?php if ($lms_lessons->num_rows > 0): ?>
                            <div class="list-group">
                                <?php while($lesson = $lms_lessons->fetch_assoc()): ?>
                                <div class="list-group-item lesson-item">
                                    <div class="d-flex justify-content-between align-items-start">
                                        <div>
                                            <h6 class="mb-1"><?php echo htmlspecialchars($lesson['judul_lesson']); ?></h6>
                                            <p class="mb-1 text-muted small">
                                                <i class="bi bi-clock"></i> <?php echo $lesson['durasi_menit']; ?> menit • 
                                                <i class="bi bi-list-ol"></i> Urutan: <?php echo $lesson['urutan']; ?> • 
                                                <span class="badge bg-light text-dark"><?php echo ucfirst($lesson['tipe_lesson']); ?></span>
                                            </p>
                                        </div>
                                        <div class="btn-group">
                                            <a href="edit_lesson.php?id=<?php echo $lesson['id']; ?>" 
                                               class="btn btn-sm btn-outline-primary" title="Edit">
                                                <i class="bi bi-pencil"></i>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                                <?php endwhile; ?>
                            </div>
                        <?php else: ?>
                            <div class="text-center py-4">
                                <i class="bi bi-journals display-1 text-muted"></i>
                                <h5 class="text-muted mt-3">Belum ada materi</h5>
                                <p class="text-muted">Tambahkan materi pertama untuk kursus ini.</p>
                                <a href="manage_lessons.php?course_id=<?php echo $course_id; ?>" class="btn btn-primary">
                                    <i class="bi bi-plus-circle"></i> Tambah Materi
                                </a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <!-- Sidebar -->
            <div class="col-lg-4">
                <!-- Statistics -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="card-title mb-0"><i class="bi bi-graph-up"></i> Statistik</h5>
                    </div>
                    <div class="card-body">
                        <div class="row text-center">
                            <div class="col-6 mb-3">
                                <div class="stat-card card bg-primary text-white">
                                    <div class="card-body py-3">
                                        <i class="bi bi-people display-6"></i>
                                        <h4><?php echo $stats['total_lms_enrollments']; ?></h4>
                                        <small>Total Siswa</small>
                                    </div>
                                </div>
                            </div>
                            <div class="col-6 mb-3">
                                <div class="stat-card card bg-success text-white">
                                    <div class="card-body py-3">
                                        <i class="bi bi-check-circle display-6"></i>
                                        <h4><?php echo $stats['completed_lms_enrollments']; ?></h4>
                                        <small>Selesai</small>
                                    </div>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="stat-card card bg-info text-white">
                                    <div class="card-body py-3">
                                        <i class="bi bi-journals display-6"></i>
                                        <h4><?php echo $stats['total_lms_lessons']; ?></h4>
                                        <small>Materi</small>
                                    </div>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="stat-card card bg-warning text-dark">
                                    <div class="card-body py-3">
                                        <i class="bi bi-clock display-6"></i>
                                        <h4><?php echo ceil($stats['total_minutes'] / 60); ?></h4>
                                        <small>Jam Konten</small>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Course Details -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="card-title mb-0"><i class="bi bi-gear"></i> Detail Kursus</h5>
                    </div>
                    <div class="card-body">
                        <table class="table table-sm">
                            <tr>
                                <td><strong>Rating:</strong></td>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <i class="bi bi-star-fill text-warning me-1"></i>
                                        <span><?php echo number_format($course['rating'], 1); ?>/5.0</span>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td><strong>Total Siswa:</strong></td>
                                <td><?php echo $course['total_siswa']; ?> siswa</td>
                            </tr>
                            <tr>
                                <td><strong>Dibuat:</strong></td>
                                <td><?php echo date('d/m/Y H:i', strtotime($course['created_at'])); ?></td>
                            </tr>
                            <tr>
                                <td><strong>Diupdate:</strong></td>
                                <td><?php echo date('d/m/Y H:i', strtotime($course['updated_at'])); ?></td>
                            </tr>
                        </table>
                    </div>
                </div>

                <!-- Recent lms_enrollments -->
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0"><i class="bi bi-clock-history"></i> Pendaftaran Terbaru</h5>
                    </div>
                    <div class="card-body">
                        <?php if ($recent_lms_enrollments->num_rows > 0): ?>
                            <div class="list-group list-group-flush">
                                <?php while($enrollment = $recent_lms_enrollments->fetch_assoc()): ?>
                                <div class="list-group-item px-0">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <h6 class="mb-0 small"><?php echo htmlspecialchars($enrollment['nama_lengkap']); ?></h6>
                                            <small class="text-muted"><?php echo date('d/m/Y', strtotime($enrollment['tanggal_enroll'])); ?></small>
                                        </div>
                                        <span class="badge bg-<?php 
                                            echo $enrollment['status'] == 'completed' ? 'success' : 
                                                 ($enrollment['status'] == 'active' ? 'primary' : 'secondary'); 
                                        ?>">
                                            <?php echo ucfirst($enrollment['status']); ?>
                                        </span>
                                    </div>
                                    <?php if ($enrollment['progress'] > 0): ?>
                                    <div class="mt-2">
                                        <div class="progress">
                                            <div class="progress-bar" style="width: <?php echo $enrollment['progress']; ?>%">
                                                <?php echo $enrollment['progress']; ?>%
                                            </div>
                                        </div>
                                    </div>
                                    <?php endif; ?>
                                </div>
                                <?php endwhile; ?>
                            </div>
                        <?php else: ?>
                            <p class="text-muted text-center mb-0">Belum ada pendaftaran</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- Quick Actions -->
        <div class="card mt-4">
            <div class="card-body text-center">
                <div class="btn-group">
                    <a href="edit_course.php?id=<?php echo $course_id; ?>" class="btn btn-primary">
                        <i class="bi bi-pencil"></i> Edit Kursus
                    </a>
                    <a href="manage_lessons.php?course_id=<?php echo $course_id; ?>" class="btn btn-success">
                        <i class="bi bi-journals"></i> Kelola Materi
                    </a>
                    <a href="manage_courses.php" class="btn btn-secondary">
                        <i class="bi bi-arrow-left"></i> Kembali
                    </a>
                </div>
            </div>
        </div>
    </div>

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