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

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

$guru_id = $_SESSION['user_id'];

// Get quiz ID from URL
$quiz_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($quiz_id <= 0) {
    $_SESSION['error'] = "Kuis tidak valid";
    redirect('manage_quizzes.php');
}

// Get quiz details with verification of ownership
$sql_quiz = "SELECT q.*, l.judul_lesson, c.judul as judul_course, c.id as course_id,
                    u.nama_lengkap as nama_guru
             FROM lms_quizzes q
             LEFT JOIN lms_lessons l ON q.id_lesson = l.id
             LEFT JOIN lms_courses c ON l.id_course = c.id
             LEFT JOIN lms_users u ON c.id_guru = u.id
             WHERE q.id = ? AND c.id_guru = ?";
$stmt_quiz = $conn->prepare($sql_quiz);
$stmt_quiz->bind_param('ii', $quiz_id, $guru_id);
$stmt_quiz->execute();
$quiz = $stmt_quiz->get_result()->fetch_assoc();

if (!$quiz) {
    $_SESSION['error'] = "Kuis tidak ditemukan atau Anda tidak memiliki akses";
    redirect('manage_quizzes.php');
}

// Get lms_questions for this quiz
$sql_lms_questions = "SELECT * FROM lms_questions WHERE id_quiz = ? ORDER BY id ASC";
$stmt_lms_questions = $conn->prepare($sql_lms_questions);
$stmt_lms_questions->bind_param('i', $quiz_id);
$stmt_lms_questions->execute();
$lms_questions = $stmt_lms_questions->get_result();

// Get quiz statistics
$sql_stats = "SELECT 
    COUNT(DISTINCT qa.id_siswa) as total_attempts,
    AVG(qa.nilai) as avg_score,
    MAX(qa.nilai) as highest_score,
    MIN(qa.nilai) as lowest_score,
    COUNT(CASE WHEN qa.nilai >= q.passing_grade THEN 1 END) as passed_attempts
    FROM lms_quiz_attempts qa
    LEFT JOIN lms_quizzes q ON qa.id_quiz = q.id
    WHERE qa.id_quiz = ?";
$stmt_stats = $conn->prepare($sql_stats);
$stmt_stats->bind_param('i', $quiz_id);
$stmt_stats->execute();
$stats = $stmt_stats->get_result()->fetch_assoc();

// Calculate pass rate
$pass_rate = $stats['total_attempts'] > 0 ? 
    ($stats['passed_attempts'] / $stats['total_attempts']) * 100 : 0;
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Preview Kuis - <?php echo htmlspecialchars($quiz['judul_quiz']); ?> - 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);
        }
        
        .quiz-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 40px 0;
            margin-bottom: 30px;
        }
        
        .quiz-container {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }
        
        .stats-card {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            border: none;
        }
        
        .question-card {
            background: white;
            border-radius: 10px;
            padding: 25px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 25px;
            border: 2px solid #e9ecef;
            transition: all 0.3s;
        }
        
        .question-card:hover {
            border-color: var(--primary-color);
            box-shadow: 0 4px 8px rgba(0,0,0,0.15);
        }
        
        .question-number {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: var(--primary-color);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            margin-right: 15px;
            flex-shrink: 0;
        }
        
        .option-item {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 10px;
            border: 2px solid transparent;
            transition: all 0.3s;
            cursor: pointer;
        }
        
        .option-item:hover {
            background: #e9ecef;
            border-color: #dee2e6;
        }
        
        .option-item.selected {
            background: #e3f2fd;
            border-color: var(--primary-color);
        }
        
        .option-item.correct {
            background: #d4edda;
            border-color: #28a745;
        }
        
        .option-item.incorrect {
            background: #f8d7da;
            border-color: #dc3545;
        }
        
        .badge-mc { background: #17a2b8; }
        .badge-essay { background: #6c757d; }
        
        .progress-quiz {
            height: 8px;
            border-radius: 4px;
        }
        
        .preview-badge {
            background: #ffc107;
            color: #000;
            font-weight: 500;
        }
        
        .answer-key {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
            margin-top: 10px;
            border-left: 4px solid #28a745;
        }
        
        .timer-display {
            background: #fff3cd;
            border: 2px solid #ffc107;
            border-radius: 10px;
            padding: 15px;
            text-align: center;
            font-weight: bold;
            color: #856404;
        }
    </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" 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="course_management.php">
                                <i class="bi bi-gear"></i> Kelola Kursus
                            </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">Preview Kuis</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 $_SESSION['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">
                    <!-- Quiz Header -->
                    <div class="quiz-header">
                        <div class="container">
                            <nav aria-label="breadcrumb">
                                <ol class="breadcrumb breadcrumb-dark">
                                    <li class="breadcrumb-item"><a href="manage_quizzes.php" class="text-white-50">Kelola Kuis</a></li>
                                    <li class="breadcrumb-item active text-white">Preview</li>
                                </ol>
                            </nav>
                            
                            <div class="row align-items-center">
                                <div class="col-lg-8">
                                    <span class="badge preview-badge mb-3">
                                        <i class="bi bi-eye me-1"></i>PREVIEW MODE - GURU
                                    </span>
                                    <h1 class="display-5 fw-bold mb-3"><?php echo htmlspecialchars($quiz['judul_quiz']); ?></h1>
                                    <p class="lead mb-4 opacity-75"><?php echo htmlspecialchars($quiz['deskripsi']); ?></p>
                                    
                                    <div class="row">
                                        <div class="col-md-3 col-6 mb-3">
                                            <div class="text-white">
                                                <i class="bi bi-clock display-6 mb-2"></i>
                                                <h4 class="fw-bold"><?php echo $quiz['waktu_menit']; ?>m</h4>
                                                <small>Waktu Pengerjaan</small>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-6 mb-3">
                                            <div class="text-white">
                                                <i class="bi bi-question-circle display-6 mb-2"></i>
                                                <h4 class="fw-bold"><?php echo $quiz['jumlah_soal']; ?></h4>
                                                <small>Jumlah Soal</small>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-6 mb-3">
                                            <div class="text-white">
                                                <i class="bi bi-check-circle display-6 mb-2"></i>
                                                <h4 class="fw-bold"><?php echo $quiz['passing_grade']; ?></h4>
                                                <small>Nilai Kelulusan</small>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-6 mb-3">
                                            <div class="text-white">
                                                <i class="bi bi-journal-bookmark display-6 mb-2"></i>
                                                <h4 class="fw-bold"><?php echo htmlspecialchars($quiz['judul_course']); ?></h4>
                                                <small>Kursus</small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-4 text-center">
                                    <div class="bg-white rounded-3 p-4 shadow">
                                        <h5 class="fw-bold mb-3">Aksi Cepat</h5>
                                        <div class="d-grid gap-2">
                                            <a href="manage_lms_questions.php?quiz_id=<?php echo $quiz_id; ?>" class="btn btn-primary">
                                                <i class="bi bi-pencil me-2"></i>Kelola Soal
                                            </a>
                                            <a href="manage_quizzes.php?course_id=<?php echo $quiz['course_id']; ?>" class="btn btn-outline-primary">
                                                <i class="bi bi-arrow-left me-2"></i>Kembali ke Kuis
                                            </a>
                                            <a href="quiz_results.php?id=<?php echo $quiz_id; ?>" class="btn btn-outline-success">
                                                <i class="bi bi-graph-up me-2"></i>Lihat Hasil
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="container">
                        <div class="row">
                            <!-- Main Quiz Content -->
                            <div class="col-lg-8">
                                <!-- Quiz Statistics -->
                                <div class="quiz-container">
                                    <h4 class="fw-bold mb-4">
                                        <i class="bi bi-graph-up me-2"></i>Statistik Kuis
                                    </h4>
                                    
                                    <div class="row">
                                        <div class="col-md-3 col-6 mb-3">
                                            <div class="stats-card text-center">
                                                <i class="bi bi-people display-6 text-primary mb-3"></i>
                                                <h3 class="fw-bold"><?php echo $stats['total_attempts'] ?? 0; ?></h3>
                                                <p class="text-muted mb-0">Total Attempt</p>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-6 mb-3">
                                            <div class="stats-card text-center">
                                                <i class="bi bi-star display-6 text-warning mb-3"></i>
                                                <h3 class="fw-bold"><?php echo number_format($stats['avg_score'] ?? 0, 1); ?></h3>
                                                <p class="text-muted mb-0">Rata-rata Nilai</p>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-6 mb-3">
                                            <div class="stats-card text-center">
                                                <i class="bi bi-trophy display-6 text-success mb-3"></i>
                                                <h3 class="fw-bold"><?php echo $stats['highest_score'] ?? 0; ?></h3>
                                                <p class="text-muted mb-0">Nilai Tertinggi</p>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-6 mb-3">
                                            <div class="stats-card text-center">
                                                <i class="bi bi-check-circle display-6 text-info mb-3"></i>
                                                <h3 class="fw-bold"><?php echo number_format($pass_rate, 1); ?>%</h3>
                                                <p class="text-muted mb-0">Tingkat Kelulusan</p>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="mt-4">
                                        <div class="d-flex justify-content-between mb-2">
                                            <span class="fw-semibold">Distribusi Kelulusan</span>
                                            <span><?php echo $stats['passed_attempts'] ?? 0; ?> dari <?php echo $stats['total_attempts'] ?? 0; ?> siswa</span>
                                        </div>
                                        <div class="progress progress-quiz">
                                            <div class="progress-bar 
                                                <?php echo $pass_rate >= 70 ? 'bg-success' : 
                                                      ($pass_rate >= 50 ? 'bg-warning' : 'bg-danger'); ?>" 
                                                 style="width: <?php echo $pass_rate; ?>%">
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- Quiz Instructions -->
                                <div class="quiz-container">
                                    <h4 class="fw-bold mb-4">
                                        <i class="bi bi-info-circle me-2"></i>Instruksi Pengerjaan
                                    </h4>
                                    
                                    <div class="alert alert-info">
                                        <h6 class="alert-heading fw-bold">
                                            <i class="bi bi-lightbulb me-2"></i>Informasi untuk Siswa:
                                        </h6>
                                        <ul class="mb-0">
                                            <li>Kuis ini terdiri dari <strong><?php echo $quiz['jumlah_soal']; ?> soal</strong> dengan waktu pengerjaan <strong><?php echo $quiz['waktu_menit']; ?> menit</strong></li>
                                            <li>Nilai minimal untuk lulus adalah <strong><?php echo $quiz['passing_grade']; ?></strong></li>
                                            <li>Bacalah setiap pertanyaan dengan seksama sebelum menjawab</li>
                                            <li>Untuk soal pilihan ganda, pilih satu jawaban yang paling tepat</li>
                                            <li>Untuk soal essay, tulis jawaban dengan jelas dan lengkap</li>
                                            <li>Pastikan untuk mengirim jawaban sebelum waktu habis</li>
                                        </ul>
                                    </div>
                                </div>

                                <!-- lms_questions Preview -->
                                <div class="quiz-container">
                                    <div class="d-flex justify-content-between align-items-center mb-4">
                                        <h4 class="fw-bold mb-0">
                                            <i class="bi bi-question-circle me-2"></i>Preview Soal
                                        </h4>
                                        <span class="badge bg-primary">
                                            <?php echo $lms_questions->num_rows; ?> Soal
                                        </span>
                                    </div>

                                    <?php if ($lms_questions->num_rows > 0): ?>
                                        <div class="lms_questions-preview">
                                            <?php 
                                            $question_number = 1;
                                            while($question = $lms_questions->fetch_assoc()): 
                                            ?>
                                                <div class="question-card">
                                                    <div class="d-flex align-items-start mb-3">
                                                        <div class="question-number"><?php echo $question_number; ?></div>
                                                        <div class="flex-grow-1">
                                                            <div class="d-flex justify-content-between align-items-start mb-2">
                                                                <h6 class="fw-bold mb-1"><?php echo htmlspecialchars($question['pertanyaan']); ?></h6>
                                                                <div class="d-flex gap-2">
                                                                    <span class="badge <?php echo $question['tipe'] === 'pilihan_ganda' ? 'badge-mc' : 'badge-essay'; ?>">
                                                                        <?php echo $question['tipe'] === 'pilihan_ganda' ? 'Pilihan Ganda' : 'Essay'; ?>
                                                                    </span>
                                                                    <span class="badge bg-light text-dark">
                                                                        <?php echo $question['poin']; ?> Poin
                                                                    </span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <?php if ($question['tipe'] === 'pilihan_ganda'): ?>
                                                        <div class="options-preview">
                                                            <div class="option-item correct">
                                                                <strong>A.</strong> <?php echo htmlspecialchars($question['opsi_a']); ?>
                                                                <span class="badge bg-success float-end">Jawaban Benar</span>
                                                            </div>
                                                            <div class="option-item">
                                                                <strong>B.</strong> <?php echo htmlspecialchars($question['opsi_b']); ?>
                                                            </div>
                                                            <?php if (!empty($question['opsi_c'])): ?>
                                                                <div class="option-item">
                                                                    <strong>C.</strong> <?php echo htmlspecialchars($question['opsi_c']); ?>
                                                                </div>
                                                            <?php endif; ?>
                                                            <?php if (!empty($question['opsi_d'])): ?>
                                                                <div class="option-item">
                                                                    <strong>D.</strong> <?php echo htmlspecialchars($question['opsi_d']); ?>
                                                                </div>
                                                            <?php endif; ?>
                                                        </div>
                                                        
                                                        <div class="answer-key mt-3">
                                                            <strong><i class="bi bi-key me-2"></i>Kunci Jawaban:</strong> 
                                                            Opsi <?php echo $question['jawaban_benar']; ?> - 
                                                            <?php 
                                                            $correct_option = 'opsi_' . strtolower($question['jawaban_benar']);
                                                            echo htmlspecialchars($question[$correct_option]);
                                                            ?>
                                                        </div>
                                                    <?php else: ?>
                                                        <div class="alert alert-warning">
                                                            <i class="bi bi-pencil me-2"></i>
                                                            <strong>Soal Essay</strong> - Jawaban akan dinilai manual oleh guru berdasarkan kelengkapan dan ketepatan.
                                                        </div>
                                                        
                                                        <div class="answer-key">
                                                            <strong><i class="bi bi-lightbulb me-2"></i>Panduan Penilaian:</strong>
                                                            <ul class="mb-0 mt-2">
                                                                <li>Periksa relevansi jawaban dengan pertanyaan</li>
                                                                <li>Nilai kelengkapan dan kedalaman jawaban</li>
                                                                <li>Perhatikan struktur dan kejelasan penulisan</li>
                                                                <li>Bobot nilai: <?php echo $question['poin']; ?> poin</li>
                                                            </ul>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>
                                                <?php $question_number++; ?>
                                            <?php endwhile; ?>
                                        </div>
                                    <?php else: ?>
                                        <div class="text-center py-5">
                                            <i class="bi bi-question-circle display-1 text-muted"></i>
                                            <h4 class="text-muted mt-3">Belum ada soal</h4>
                                            <p class="text-muted mb-4">Tambahkan soal terlebih dahulu untuk melihat preview</p>
                                            <a href="manage_lms_questions.php?quiz_id=<?php echo $quiz_id; ?>" class="btn btn-primary">
                                                <i class="bi bi-plus-circle me-2"></i>Tambah Soal
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <!-- Sidebar -->
                            <div class="col-lg-4">
                                <!-- Quiz Info Card -->
                                <div class="stats-card">
                                    <h5 class="fw-bold mb-4">
                                        <i class="bi bi-info-square me-2"></i>Informasi Kuis
                                    </h5>
                                    
                                    <div class="mb-3">
                                        <div class="d-flex justify-content-between align-items-center mb-2">
                                            <small class="text-muted">Kursus</small>
                                            <small class="fw-semibold"><?php echo htmlspecialchars($quiz['judul_course']); ?></small>
                                        </div>
                                        <div class="d-flex justify-content-between align-items-center mb-2">
                                            <small class="text-muted">Materi</small>
                                            <small class="fw-semibold"><?php echo htmlspecialchars($quiz['judul_lesson']); ?></small>
                                        </div>
                                        <div class="d-flex justify-content-between align-items-center mb-2">
                                            <small class="text-muted">Dibuat oleh</small>
                                            <small class="fw-semibold"><?php echo htmlspecialchars($quiz['nama_guru']); ?></small>
                                        </div>
                                        <div class="d-flex justify-content-between align-items-center mb-2">
                                            <small class="text-muted">Tanggal Dibuat</small>
                                            <small class="fw-semibold"><?php echo date('d M Y', strtotime($quiz['created_at'])); ?></small>
                                        </div>
                                    </div>
                                    
                                    <hr>
                                    
                                    <h6 class="fw-bold mb-3">Ringkasan Soal</h6>
                                    <div class="row text-center">
                                        <div class="col-6 mb-3">
                                            <div class="text-primary">
                                                <i class="bi bi-list-ol display-6 mb-2"></i>
                                                <h5 class="fw-bold"><?php echo $lms_questions->num_rows; ?></h5>
                                                <small>Total Soal</small>
                                            </div>
                                        </div>
                                        <div class="col-6 mb-3">
                                            <?php
                                            $total_points = 0;
                                            $lms_questions_temp = $lms_questions;
                                            while($q = $lms_questions_temp->fetch_assoc()) {
                                                $total_points += $q['poin'];
                                            }
                                            $lms_questions->data_seek(0);
                                            ?>
                                            <div class="text-success">
                                                <i class="bi bi-star display-6 mb-2"></i>
                                                <h5 class="fw-bold"><?php echo $total_points; ?></h5>
                                                <small>Total Poin</small>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- Quick Actions -->
                                <div class="stats-card">
                                    <h5 class="fw-bold mb-4">
                                        <i class="bi bi-lightning me-2"></i>Aksi Cepat
                                    </h5>
                                    
                                    <div class="d-grid gap-2">
                                        <a href="manage_lms_questions.php?quiz_id=<?php echo $quiz_id; ?>" class="btn btn-outline-primary">
                                            <i class="bi bi-pencil me-2"></i>Edit Soal
                                        </a>
                                        <a href="quiz_results.php?id=<?php echo $quiz_id; ?>" class="btn btn-outline-success">
                                            <i class="bi bi-graph-up me-2"></i>Lihat Hasil
                                        </a>
                                        <a href="manage_quizzes.php?course_id=<?php echo $quiz['course_id']; ?>" class="btn btn-outline-secondary">
                                            <i class="bi bi-arrow-left me-2"></i>Kembali
                                        </a>
                                    </div>
                                </div>

                                <!-- Preview Tips -->
                                <div class="stats-card">
                                    <h5 class="fw-bold mb-4">
                                        <i class="bi bi-eye me-2"></i>Tips Preview
                                    </h5>
                                    
                                    <div class="small">
                                        <div class="mb-3">
                                            <strong>Sebagai Guru:</strong>
                                            <ul class="mt-2 mb-0">
                                                <li>Periksa kejelasan setiap pertanyaan</li>
                                                <li>Pastikan jawaban benar sudah tepat</li>
                                                <li>Verifikasi waktu pengerjaan cukup</li>
                                                <li>Test kesulitan soal sesuai level siswa</li>
                                            </ul>
                                        </div>
                                        
                                        <div class="mb-0">
                                            <strong>Yang Akan Dilihat Siswa:</strong>
                                            <ul class="mt-2 mb-0">
                                                <li>Interface yang clean dan mudah</li>
                                                <li>Timer countdown otomatis</li>
                                                <li>Navigasi soal yang intuitif</li>
                                                <li>Konfirmasi sebelum submit</li>
                                            </ul>
                                        </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>
        document.addEventListener('DOMContentLoaded', function() {
            // Simulate student interaction for preview
            const optionItems = document.querySelectorAll('.option-item:not(.correct)');
            
            optionItems.forEach(option => {
                option.addEventListener('click', function() {
                    // Remove selected class from all options in the same question
                    const questionCard = this.closest('.question-card');
                    const allOptions = questionCard.querySelectorAll('.option-item');
                    allOptions.forEach(opt => opt.classList.remove('selected'));
                    
                    // Add selected class to clicked option
                    this.classList.add('selected');
                    
                    // Show feedback for incorrect selection
                    if (!this.classList.contains('correct')) {
                        this.classList.add('incorrect');
                        setTimeout(() => {
                            this.classList.remove('incorrect');
                        }, 2000);
                    }
                });
            });

            // Auto-scroll to lms_questions section
            const lms_questionsSection = document.querySelector('.lms_questions-preview');
            if (lms_questionsSection && lms_questionsSection.children.length > 0) {
                setTimeout(() => {
                    lms_questionsSection.scrollIntoView({ 
                        behavior: 'smooth', 
                        block: 'start' 
                    });
                }, 1000);
            }

            // Add animation to question cards
            const questionCards = document.querySelectorAll('.question-card');
            questionCards.forEach((card, index) => {
                card.style.animationDelay = `${index * 0.1}s`;
                card.classList.add('animate__animated', 'animate__fadeInUp');
            });

            // Simulate timer for preview
            const timerDisplay = document.createElement('div');
            timerDisplay.className = 'timer-display mb-4';
            timerDisplay.innerHTML = `
                <i class="bi bi-clock me-2"></i>
                <span id="timer">00:00</span> - 
                <small>Simulasi Timer Preview</small>
            `;
            
            const quizContainer = document.querySelector('.quiz-container');
            if (quizContainer) {
                quizContainer.parentNode.insertBefore(timerDisplay, quizContainer);
                
                let seconds = 0;
                setInterval(() => {
                    seconds++;
                    const minutes = Math.floor(seconds / 60);
                    const remainingSeconds = seconds % 60;
                    document.getElementById('timer').textContent = 
                        `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
                }, 1000);
            }
        });
    </script>
</body>
</html>
<?php
// Tutup koneksi database
if (isset($stmt_quiz)) $stmt_quiz->close();
if (isset($stmt_lms_questions)) $stmt_lms_questions->close();
if (isset($stmt_stats)) $stmt_stats->close();
$conn->close();
?>