<?php
session_start();
require_once '../includes/config.php';
require_once '../includes/functions.php';

// Cek apakah user sudah login dan memiliki role reviewer
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'reviewer') {
    header('Location: ../login.php');
    exit;
}

$error = '';
$success = '';

// Ambil ID soal untuk review
$review_soal_id = $_GET['review_id'] ?? '';

// Data soal yang akan direview
$review_soal = null;
if (!empty($review_soal_id)) {
    try {
        $stmt = $pdo->prepare("SELECT bt.*, u.nama_lengkap as nama_penulis 
                              FROM bank_soal_teori bt 
                              LEFT JOIN users u ON bt.penulis = u.id 
                              WHERE bt.id = ?");
        $stmt->execute([$review_soal_id]);
        $review_soal = $stmt->fetch(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        $error = 'Gagal mengambil data soal untuk review: ' . $e->getMessage();
    }
}

// Proses submit review
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit_review'])) {
    try {
        $id = $_POST['id'];
        $status_review = $_POST['status_review'];
        $catatan_review = $_POST['catatan_review'] ?? '';
        
        $stmt = $pdo->prepare("UPDATE bank_soal_teori SET 
                              direview_oleh = ?, 
                              tanggal_review = NOW(), 
                              status_review = ?, 
                              catatan_review = ?
                              WHERE id = ?");
        
        $stmt->execute([
            $_SESSION['user_id'],
            $status_review,
            $catatan_review,
            $id
        ]);
        
        $success = 'Review berhasil disimpan!';
        
        // Redirect untuk menghindari resubmit
        header("Location: review_soal_teori.php?review_id=" . $id);
        exit;
        
    } catch (PDOException $e) {
        $error = 'Gagal menyimpan review: ' . $e->getMessage();
    }
}

// Konfigurasi Pagination
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$page = max($page, 1);
$offset = ($page - 1) * $limit;

// Filter status review
$status_filter = $_GET['status'] ?? 'perlu'; // default: perlu review

$where_conditions = [];
$params = [];

if ($status_filter === 'perlu') {
    $where_conditions[] = "status_review IN ('belum', 'revisi')";
} elseif ($status_filter === 'sudah') {
    $where_conditions[] = "status_review = 'sudah' AND direview_oleh = ?";
    $params[] = $_SESSION['user_id'];
} elseif ($status_filter === 'semua') {
    // Tidak ada filter tambahan
}

// Bangun query dengan filter
$where_sql = '';
if (!empty($where_conditions)) {
    $where_sql = "WHERE " . implode(" AND ", $where_conditions);
}

// Hitung total data dengan filter
$total_data = 0;
$total_pages = 1;

try {
    $count_sql = "SELECT COUNT(*) FROM bank_soal_teori $where_sql";
    
    if (!empty($params)) {
        $stmt = $pdo->prepare($count_sql);
        $stmt->execute($params);
    } else {
        $stmt = $pdo->query($count_sql);
    }
    $total_data = $stmt->fetchColumn();
    
    $total_pages = ceil($total_data / $limit);
} catch (PDOException $e) {
    $error = 'Gagal menghitung total data: ' . $e->getMessage();
}

// Ambil data soal dengan pagination dan filter
$soal_teori = [];
try {
    $sql = "SELECT bt.*, u.nama_lengkap as nama_reviewer 
            FROM bank_soal_teori bt 
            LEFT JOIN users u ON bt.direview_oleh = u.id 
            $where_sql 
            ORDER BY 
                CASE WHEN status_review = 'revisi' THEN 1 
                     WHEN status_review = 'belum' THEN 2 
                     ELSE 3 END,
                created_at DESC 
            LIMIT $limit OFFSET $offset";
    
    if (!empty($params)) {
        $stmt = $pdo->prepare($sql);
        $stmt->execute($params);
    } else {
        $stmt = $pdo->query($sql);
    }
    $soal_teori = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
} catch (PDOException $e) {
    $error = 'Gagal mengambil data soal: ' . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Review Soal Teori - SOCA OSCE System</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="stylesheet" href="../assets/css/list_soal_teori.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .review-badge {
            display: inline-flex;
            align-items: center;
            gap: 5px;
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 0.8em;
            font-weight: bold;
        }

        .badge-belum {
            background: #dc3545;
            color: white;
        }

        .badge-sudah {
            background: #28a745;
            color: white;
        }

        .badge-revisi {
            background: #ffc107;
            color: #212529;
        }

        .review-form {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-top: 20px;
            border-left: 4px solid #667eea;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .form-group select,
        .form-group textarea {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ced4da;
            border-radius: 4px;
            font-size: 14px;
        }

        .form-group textarea {
            min-height: 100px;
            resize: vertical;
        }

        .btn-review {
            background: #28a745;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }

        .btn-review:hover {
            background: #218838;
        }

        .btn-cancel {
            background: #6c757d;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }

        .btn-cancel:hover {
            background: #5a6268;
        }

        .review-info {
            background: #e7f3ff;
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 15px;
            border-left: 4px solid #17a2b8;
        }

        .filter-tabs {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            flex-wrap: wrap;
        }

        .filter-tab {
            padding: 10px 20px;
            border: 2px solid #667eea;
            border-radius: 20px;
            background: white;
            color: #667eea;
            text-decoration: none;
            transition: all 0.3s;
            font-weight: bold;
        }

        .filter-tab:hover,
        .filter-tab.active {
            background: #667eea;
            color: white;
        }
    </style>
</head>
<body>
    <div class="sidebar">
        <ul class="sidebar-menu">
            <li><a href="dashboard_reviewer.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a></li>
            <li><a href="review_soal_teori.php" class="active"><i class="fas fa-book"></i> Review Soal Teori</a></li>
            <li><a href="review_soal_teori_s1.php"><i class="fas fa-book-open"></i> Review Soal Teori S1</a></li>
            <li><a href="riwayat_review.php"><i class="fas fa-history"></i> Riwayat Review</a></li>
            <li><a href="../logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
        </ul>
    </div>

    <div class="main-content">
        <div class="header">
            <h1>Review Soal Teori</h1>
            <div class="user-info">
                <span><i class="fas fa-user"></i> <?php echo $_SESSION['user_name']; ?></span>
                <span style="margin-left: 15px; background: #667eea; color: white; padding: 5px 10px; border-radius: 15px;">
                    <i class="fas fa-user-check"></i> Reviewer
                </span>
                <a href="../logout.php" class="btn btn-danger btn-sm">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </a>
            </div>
        </div>

        <div class="content">
            <?php if ($error): ?>
                <div class="alert alert-error"><?php echo $error; ?></div>
            <?php endif; ?>
            
            <?php if ($success): ?>
                <div class="alert alert-success"><?php echo $success; ?></div>
            <?php endif; ?>

            <!-- Filter Tabs -->
            <div class="filter-tabs">
                <a href="review_soal_teori.php?status=perlu" class="filter-tab <?php echo $status_filter === 'perlu' ? 'active' : ''; ?>">
                    <i class="fas fa-tasks"></i> Perlu Review (<?php 
                    $stmt = $pdo->query("SELECT COUNT(*) FROM bank_soal_teori WHERE status_review IN ('belum', 'revisi')");
                    echo $stmt->fetchColumn(); 
                    ?>)
                </a>
                <a href="review_soal_teori.php?status=sudah" class="filter-tab <?php echo $status_filter === 'sudah' ? 'active' : ''; ?>">
                    <i class="fas fa-check-circle"></i> Sudah Direview (<?php 
                    $stmt = $pdo->prepare("SELECT COUNT(*) FROM bank_soal_teori WHERE status_review = 'sudah' AND direview_oleh = ?");
                    $stmt->execute([$_SESSION['user_id']]);
                    echo $stmt->fetchColumn(); 
                    ?>)
                </a>
                <a href="review_soal_teori.php?status=semua" class="filter-tab <?php echo $status_filter === 'semua' ? 'active' : ''; ?>">
                    <i class="fas fa-layer-group"></i> Semua Soal (<?php 
                    $stmt = $pdo->query("SELECT COUNT(*) FROM bank_soal_teori");
                    echo $stmt->fetchColumn(); 
                    ?>)
                </a>
            </div>

            <!-- Daftar Soal -->
            <div class="soal-list-container">
                <div class="section-header">
                    <h2>
                        <i class="fas fa-clipboard-list"></i>
                        <?php 
                        if ($status_filter === 'perlu') echo 'Soal yang Perlu Direview';
                        elseif ($status_filter === 'sudah') echo 'Soal yang Sudah Anda Review';
                        else echo 'Semua Soal Teori';
                        ?>
                        <small style="color: #f8f9faff; font-size: 0.8em; margin-left: 10px;">
                            (<?php echo count($soal_teori); ?> soal dari <?php echo $total_data; ?> total)
                        </small>
                    </h2>
                </div>

                <?php if (empty($soal_teori)): ?>
                    <div class="empty-state">
                        <i class="fas fa-check-circle"></i>
                        <h3>Tidak ada soal yang perlu direview</h3>
                        <p>Semua soal sudah direview atau tidak ada soal yang memerlukan review saat ini.</p>
                    </div>
                <?php else: ?>
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>No.</th>
                                <th>No. Registrasi</th>
                                <th>Kompetensi Utama</th>
                                <th>Department</th>
                                <th>Status Review</th>
                                <th>Tanggal Dibuat</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            $counter = ($page - 1) * $limit + 1;
                            foreach ($soal_teori as $soal): 
                                $status_class = 'badge-belum';
                                $status_text = 'Belum Direview';
                                
                                if ($soal['status_review'] == 'sudah') {
                                    $status_class = 'badge-sudah';
                                    $status_text = 'Sudah Direview';
                                } elseif ($soal['status_review'] == 'revisi') {
                                    $status_class = 'badge-revisi';
                                    $status_text = 'Perlu Revisi';
                                }
                            ?>
                                <tr id="soal-<?php echo $soal['id']; ?>" 
                                    class="<?php echo $review_soal && $review_soal['id'] == $soal['id'] ? 'highlighted-row' : ''; ?>">
                                    <td><?php echo $counter++; ?></td>
                                    <td>
                                        <strong><?php echo htmlspecialchars($soal['nomor_registrasi']); ?></strong>
                                    </td>
                                    <td><?php echo htmlspecialchars($soal['tinjauan1']); ?></td>
                                    <td>
                                        <span class="badge badge-success">
                                            <?php echo htmlspecialchars($soal['department'] ?? '-'); ?>
                                        </span>
                                    </td>
                                    <td>
                                        <span class="review-badge <?php echo $status_class; ?>">
                                            <i class="fas 
                                                <?php 
                                                if ($soal['status_review'] == 'sudah') echo 'fa-check-circle';
                                                elseif ($soal['status_review'] == 'revisi') echo 'fa-exclamation-circle';
                                                else echo 'fa-clock';
                                                ?>
                                            "></i>
                                            <?php echo $status_text; ?>
                                        </span>
                                        <?php if ($soal['status_review'] == 'sudah' && !empty($soal['nama_reviewer'])): ?>
                                            <br><small>Oleh: <?php echo htmlspecialchars($soal['nama_reviewer']); ?></small>
                                        <?php endif; ?>
                                    </td>
                                    <td><?php echo date('d/m/Y H:i', strtotime($soal['created_at'])); ?></td>
                                    <td>
                                        <div class="action-buttons">
                                            <a href="review_soal_teori.php?review_id=<?php echo $soal['id']; ?>&status=<?php echo $status_filter; ?>&page=<?php echo $page; ?>" 
                                               class="btn btn-review" title="Review Soal">
                                                <i class="fas fa-eye"></i> 
                                                <?php echo $soal['status_review'] == 'belum' ? 'Review' : 'Lihat'; ?>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                    
                    <!-- Pagination -->
                    <?php if ($total_pages > 1): ?>
                    <div class="pagination">
                        <div class="pagination-info">
                            Menampilkan <?php echo (($page - 1) * $limit) + 1; ?> - 
                            <?php echo min($page * $limit, $total_data); ?> dari <?php echo $total_data; ?> soal
                        </div>
                        
                        <div class="pagination-links">
                            <!-- Previous Button -->
                            <?php if ($page > 1): ?>
                                <a href="review_soal_teori.php?status=<?php echo $status_filter; ?>&page=<?php echo $page - 1; ?><?php echo $review_soal_id ? '&review_id=' . $review_soal_id : ''; ?>">
                                    <i class="fas fa-chevron-left"></i> Prev
                                </a>
                            <?php else: ?>
                                <span class="disabled"><i class="fas fa-chevron-left"></i> Prev</span>
                            <?php endif; ?>

                            <!-- Page Numbers -->
                            <?php 
                            $start_page = max(1, $page - 2);
                            $end_page = min($total_pages, $page + 2);
                            
                            for ($i = $start_page; $i <= $end_page; $i++): 
                                if ($i == $page): ?>
                                    <span class="current"><?php echo $i; ?></span>
                                <?php else: ?>
                                    <a href="review_soal_teori.php?status=<?php echo $status_filter; ?>&page=<?php echo $i; ?><?php echo $review_soal_id ? '&review_id=' . $review_soal_id : ''; ?>">
                                        <?php echo $i; ?>
                                    </a>
                                <?php endif;
                            endfor;
                            ?>

                            <!-- Next Button -->
                            <?php if ($page < $total_pages): ?>
                                <a href="review_soal_teori.php?status=<?php echo $status_filter; ?>&page=<?php echo $page + 1; ?><?php echo $review_soal_id ? '&review_id=' . $review_soal_id : ''; ?>">
                                    Next <i class="fas fa-chevron-right"></i>
                                </a>
                            <?php else: ?>
                                <span class="disabled">Next <i class="fas fa-chevron-right"></i></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endif; ?>
                <?php endif; ?>
            </div>

            <!-- Review Section -->
            <?php if ($review_soal): ?>
                <div class="review-section" id="reviewSection">
                    <div class="review-header">
                        <h2 class="review-title">
                            <i class="fas fa-file-medical-alt"></i>
                            <?php echo $review_soal['status_review'] == 'belum' ? 'Review' : 'Lihat Review'; ?> Soal Teori - <?php echo htmlspecialchars($review_soal['nomor_registrasi']); ?>
                        </h2>
                        <a href="review_soal_teori.php?status=<?php echo $status_filter; ?>&page=<?php echo $page; ?>" 
                           class="close-review" title="Tutup Review">
                            <i class="fas fa-times"></i>
                        </a>
                    </div>

                    <!-- Tampilkan Soal -->
                    <div class="review-content">
                        <div class="review-column">
                            <!-- Informasi Dasar -->
                            <div class="info-group">
                                <div class="info-label">Nomor Registrasi</div>
                                <div class="info-value"><?php echo htmlspecialchars($review_soal['nomor_registrasi']); ?></div>
                            </div>

                            <div class="info-group">
                                <div class="info-label">Department</div>
                                <div class="info-value"><?php echo htmlspecialchars($review_soal['department'] ?? '-'); ?></div>
                            </div>

                            <div class="info-group">
                                <div class="info-label">Kompetensi Utama (Tinjauan 1)</div>
                                <div class="info-value"><?php echo htmlspecialchars($review_soal['tinjauan1']); ?></div>
                            </div>

                            <div class="info-group">
                                <div class="info-label">Penyakit/Kelainan (Tinjauan 2)</div>
                                <div class="info-value"><?php echo htmlspecialchars($review_soal['tinjauan2'] ?? '-'); ?></div>
                            </div>

                            <div class="info-group">
                                <div class="info-label">Bidang Ilmu & Keterampilan KG (Tinjauan 3)</div>
                                <div class="info-value"><?php echo htmlspecialchars($review_soal['tinjauan3'] ?? '-'); ?></div>
                            </div>

                            <div class="info-group">
                                <div class="info-label">Kunci Jawaban</div>
                                <div class="info-value"><strong><?php echo htmlspecialchars($review_soal['kunci'] ?? '-'); ?></strong></div>
                            </div>

                            <div class="info-group">
                                <div class="info-label">Penulis</div>
                                <div class="info-value"><?php echo htmlspecialchars($review_soal['nama_penulis'] ?? $review_soal['penulis'] ?? '-'); ?></div>
                            </div>

                            <?php if ($review_soal['status_review'] != 'belum'): ?>
                                <div class="info-group">
                                    <div class="info-label">Status Review</div>
                                    <div class="info-value">
                                        <span class="review-badge <?php 
                                            echo $review_soal['status_review'] == 'sudah' ? 'badge-sudah' : 'badge-revisi'; 
                                        ?>">
                                            <i class="fas 
                                                <?php 
                                                if ($review_soal['status_review'] == 'sudah') echo 'fa-check-circle';
                                                else echo 'fa-exclamation-circle';
                                                ?>
                                            "></i>
                                            <?php echo $review_soal['status_review'] == 'sudah' ? 'Sudah Direview' : 'Perlu Revisi'; ?>
                                        </span>
                                    </div>
                                </div>

                                <?php if (!empty($review_soal['catatan_review'])): ?>
                                    <div class="info-group">
                                        <div class="info-label">Catatan Review Sebelumnya</div>
                                        <div class="info-value"><?php echo nl2br(htmlspecialchars($review_soal['catatan_review'])); ?></div>
                                    </div>
                                <?php endif; ?>
                            <?php endif; ?>
                        </div>

                        <div class="review-column">
                            <!-- Konten Soal -->
                            <?php if (!empty($review_soal['vignette'])): ?>
                                <div class="info-group">
                                    <div class="info-label">Vignette</div>
                                    <div class="info-value rich-text"><?php echo $review_soal['vignette']; ?></div>
                                </div>
                            <?php endif; ?>

                            <div class="info-group">
                                <div class="info-label">Pertanyaan / Instruksi</div>
                                <div class="info-value rich-text"><?php echo $review_soal['pertanyaan']; ?></div>
                            </div>

                            <!-- Pilihan Jawaban -->
                            <div class="info-group">
                                <div class="info-label">Pilihan Jawaban</div>
                                <div class="info-value">
                                    <?php if (!empty($review_soal['pilihan_a'])): ?>
                                        <div style="margin-bottom: 8px; <?php echo $review_soal['kunci'] == 'A' ? 'background: #d4edda; padding: 8px; border-radius: 4px;' : ''; ?>">
                                            <strong>A:</strong> <?php echo htmlspecialchars($review_soal['pilihan_a']); ?>
                                            <?php if ($review_soal['kunci'] == 'A'): ?><span style="color: #28a745; margin-left: 10px;">✓ Kunci Jawaban</span><?php endif; ?>
                                        </div>
                                    <?php endif; ?>
                                    
                                    <?php if (!empty($review_soal['pilihan_b'])): ?>
                                        <div style="margin-bottom: 8px; <?php echo $review_soal['kunci'] == 'B' ? 'background: #d4edda; padding: 8px; border-radius: 4px;' : ''; ?>">
                                            <strong>B:</strong> <?php echo htmlspecialchars($review_soal['pilihan_b']); ?>
                                            <?php if ($review_soal['kunci'] == 'B'): ?><span style="color: #28a745; margin-left: 10px;">✓ Kunci Jawaban</span><?php endif; ?>
                                        </div>
                                    <?php endif; ?>
                                    
                                    <?php if (!empty($review_soal['pilihan_c'])): ?>
                                        <div style="margin-bottom: 8px; <?php echo $review_soal['kunci'] == 'C' ? 'background: #d4edda; padding: 8px; border-radius: 4px;' : ''; ?>">
                                            <strong>C:</strong> <?php echo htmlspecialchars($review_soal['pilihan_c']); ?>
                                            <?php if ($review_soal['kunci'] == 'C'): ?><span style="color: #28a745; margin-left: 10px;">✓ Kunci Jawaban</span><?php endif; ?>
                                        </div>
                                    <?php endif; ?>
                                    
                                    <?php if (!empty($review_soal['pilihan_d'])): ?>
                                        <div style="margin-bottom: 8px; <?php echo $review_soal['kunci'] == 'D' ? 'background: #d4edda; padding: 8px; border-radius: 4px;' : ''; ?>">
                                            <strong>D:</strong> <?php echo htmlspecialchars($review_soal['pilihan_d']); ?>
                                            <?php if ($review_soal['kunci'] == 'D'): ?><span style="color: #28a745; margin-left: 10px;">✓ Kunci Jawaban</span><?php endif; ?>
                                        </div>
                                    <?php endif; ?>
                                    
                                    <?php if (!empty($review_soal['pilihan_e'])): ?>
                                        <div style="margin-bottom: 8px; <?php echo $review_soal['kunci'] == 'E' ? 'background: #d4edda; padding: 8px; border-radius: 4px;' : ''; ?>">
                                            <strong>E:</strong> <?php echo htmlspecialchars($review_soal['pilihan_e']); ?>
                                            <?php if ($review_soal['kunci'] == 'E'): ?><span style="color: #28a745; margin-left: 10px;">✓ Kunci Jawaban</span><?php endif; ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>

                            <?php if (!empty($review_soal['alasan_singkat'])): ?>
                                <div class="info-group">
                                    <div class="info-label">Alasan Singkat</div>
                                    <div class="info-value rich-text"><?php echo $review_soal['alasan_singkat']; ?></div>
                                </div>
                            <?php endif; ?>

                            <?php if (!empty($review_soal['referensi'])): ?>
                                <div class="info-group">
                                    <div class="info-label">Referensi</div>
                                    <div class="info-value rich-text"><?php echo $review_soal['referensi']; ?></div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>

                    <!-- Form Review (hanya tampil jika belum direview) -->
                    <?php if ($review_soal['status_review'] == 'belum' || $review_soal['status_review'] == 'revisi'): ?>
                        <div class="review-form">
                            <h3><i class="fas fa-edit"></i> Form Review</h3>
                            <form method="POST" action="">
                                <input type="hidden" name="id" value="<?php echo $review_soal['id']; ?>">
                                <input type="hidden" name="submit_review" value="1">
                                
                                <div class="form-group">
                                    <label for="status_review">Status Review *</label>
                                    <select id="status_review" name="status_review" required>
                                        <option value="sudah">✓ Setujui (Soal sudah baik)</option>
                                        <option value="revisi">✎ Perlu Revisi (Ada yang perlu diperbaiki)</option>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label for="catatan_review">Catatan Review</label>
                                    <textarea id="catatan_review" name="catatan_review" placeholder="Berikan catatan atau saran perbaikan untuk soal ini..."><?php echo htmlspecialchars($review_soal['catatan_review'] ?? ''); ?></textarea>
                                    <small>Catatan ini akan dilihat oleh pembuat soal untuk perbaikan.</small>
                                </div>

                                <div style="display: flex; gap: 10px;">
                                    <button type="submit" class="btn-review">
                                        <i class="fas fa-paper-plane"></i> Submit Review
                                    </button>
                                    <a href="review_soal_teori.php?status=<?php echo $status_filter; ?>&page=<?php echo $page; ?>" class="btn-cancel">
                                        <i class="fas fa-times"></i> Batal
                                    </a>
                                </div>
                            </form>
                        </div>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <script>
    // JavaScript untuk enhanced user experience
    document.addEventListener('DOMContentLoaded', function() {
        // Highlight row yang sedang direview
        <?php if ($review_soal): ?>
            const highlightedId = <?php echo $review_soal['id']; ?>;
            if (highlightedId) {
                const highlightedRow = document.getElementById('soal-' + highlightedId);
                if (highlightedRow) {
                    highlightedRow.scrollIntoView({ behavior: 'smooth', block: 'center' });
                }
            }
        <?php endif; ?>

        // Scroll ke review section jika ada
        const reviewSection = document.getElementById('reviewSection');
        if (reviewSection) {
            setTimeout(() => {
                reviewSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
            }, 300);
        }

        // Konfirmasi sebelum submit review
        const reviewForm = document.querySelector('.review-form form');
        if (reviewForm) {
            reviewForm.addEventListener('submit', function(e) {
                const statusReview = document.getElementById('status_review').value;
                const message = statusReview === 'sudah' 
                    ? 'Apakah Anda yakin menyetujui soal ini? Soal akan ditandai sebagai sudah direview.'
                    : 'Apakah Anda yakin mengirimkan permintaan revisi? Pembuat soal akan menerima catatan perbaikan.';
                
                if (!confirm(message)) {
                    e.preventDefault();
                }
            });
        }
    });
    </script>
</body>
</html>