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

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

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

// Proses form jika ada data yang dikirim
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Ambil data dari form
    $nomor_registrasi = $_POST['nomor_registrasi'] ?? '';
    $tinjauan1 = $_POST['tinjauan1'] ?? '';
    $tinjauan2 = $_POST['tinjauan2'] ?? '';
    $tinjauan3 = $_POST['tinjauan3'] ?? '';
    $vignette = $_POST['vignette'] ?? '';
    $pertanyaan = $_POST['pertanyaan'] ?? '';
    $pilihan_a = $_POST['pilihan_a'] ?? '';
    $pilihan_b = $_POST['pilihan_b'] ?? '';
    $pilihan_c = $_POST['pilihan_c'] ?? '';
    $pilihan_d = $_POST['pilihan_d'] ?? '';
    $pilihan_e = $_POST['pilihan_e'] ?? '';
    $kunci = $_POST['kunci'] ?? '';
    $department = $_POST['department'] ?? '';
    $alasan_singkat = $_POST['alasan_singkat'] ?? '';
    $referensi = $_POST['referensi'] ?? '';
    $penulis = $_POST['penulis'] ?? '';
    
    // Validasi data
    if (empty($nomor_registrasi) || empty($tinjauan1) || empty($pertanyaan)) {
        $error = 'Nomor Registrasi, Tinjauan 1, dan Pertanyaan harus diisi!';
    } else {
        // Simpan ke database
        try {
            $stmt = $pdo->prepare("INSERT INTO bank_soal_teori (
                nomor_registrasi, tinjauan1, tinjauan2, tinjauan3, vignette, pertanyaan,
                pilihan_a, pilihan_b, pilihan_c, pilihan_d, pilihan_e, kunci,
                department, alasan_singkat, referensi, penulis
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            
            $stmt->execute([
                $nomor_registrasi, $tinjauan1, $tinjauan2, $tinjauan3, $vignette, $pertanyaan,
                $pilihan_a, $pilihan_b, $pilihan_c, $pilihan_d, $pilihan_e, $kunci,
                $department, $alasan_singkat, $referensi, $penulis
            ]);
            
            $success = 'Soal teori berhasil disimpan!';
            
            // Reset form jika berhasil
            $_POST = array();
            
        } catch (PDOException $e) {
            $error = 'Terjadi kesalahan: ' . $e->getMessage();
        }
    }
}

// Ambil data soal untuk ditampilkan
$soal_teori = [];
try {
    $stmt = $pdo->query("SELECT * FROM bank_soal_teori ORDER BY created_at DESC");
    $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>Bank Soal Teori - SOCA OSCE System</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="stylesheet" href="../assets/css/bank_soal_teori.css">
</head>
<body>
    <div class="container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>SOCA/OSCE System</h2>
                <p>Admin Panel</p>
            </div>
            <ul class="sidebar-menu">
                <li><a href="dashboard.php" class="<?php echo basename($_SERVER['PHP_SELF']) == 'dashboard.php' ? 'active' : ''; ?>">
                    <i class="fas fa-tachometer-alt"></i> Dashboard
                </a></li>
                <li><a href="bank_soal_teori.php" class="<?php echo basename($_SERVER['PHP_SELF']) == 'bank_soal_teori.php' ? 'active' : ''; ?>">
                    <i class="fas fa-question-circle"></i> Bank Soal Teori
                </a></li>
                <li><a href="list_soal_teori.php" class="<?php echo basename($_SERVER['PHP_SELF']) == 'list_soal_teori.php' ? 'active' : ''; ?>">
                    <i class="fas fa-eye"></i> Lihat Soal Teori
                </a></li>
                <li><a href="bank_soal_praktik.php" class="<?php echo basename($_SERVER['PHP_SELF']) == 'bank_soal_praktik.php' ? 'active' : ''; ?>">
                    <i class="fas fa-question-circle"></i> Bank Soal Uji Praktik
                </a></li>
                <li><a href="soal.php" class="<?php echo basename($_SERVER['PHP_SELF']) == 'soal.php' ? 'active' : ''; ?>">
                    <i class="fas fa-question-circle"></i> Kelola Soal
                </a></li>
                <li><a href="ujian.php" class="<?php echo basename($_SERVER['PHP_SELF']) == 'ujian.php' ? 'active' : ''; ?>">
                    <i class="fas fa-clipboard-list"></i> Kelola Ujian
                </a></li>
                <li><a href="peserta.php" class="<?php echo basename($_SERVER['PHP_SELF']) == 'peserta.php' ? 'active' : ''; ?>">
                    <i class="fas fa-users"></i> Kelola Peserta
                </a></li>
                <li><a href="penguji.php" class="<?php echo basename($_SERVER['PHP_SELF']) == 'penguji.php' ? 'active' : ''; ?>">
                    <i class="fas fa-user-check"></i> Kelola Penguji
                </a></li>
                <li><a href="nilai.php" class="<?php echo basename($_SERVER['PHP_SELF']) == 'nilai.php' ? 'active' : ''; ?>">
                    <i class="fas fa-chart-bar"></i> Lihat Nilai
                </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>Bank Soal Teori</h1>
                <div class="user-info">
                    <span><i class="fas fa-user"></i> Halo, <?php echo $_SESSION['user_name'] ?? 'Admin'; ?></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; ?>
                
                <div class="form-container">
                    <h2>Tambah Soal Teori Baru</h2>
                    <form method="POST" action="" id="soalTeoriForm">
                        <div class="form-group">
                            <label for="nomor_registrasi">1. Nomor Registrasi Soal *</label>
                            <input type="text" id="nomor_registrasi" name="nomor_registrasi" 
                                   value="<?php echo htmlspecialchars($_POST['nomor_registrasi'] ?? ''); ?>" required>
                        </div>
                        
                        <div class="form-group">
                            <label for="tinjauan1">2. Tinjauan 1: Kompetensi Utama *</label>
                            <input type="text" id="tinjauan1" name="tinjauan1" 
                                   value="<?php echo htmlspecialchars($_POST['tinjauan1'] ?? ''); ?>" required>
                        </div>
                        
                        <div class="form-group">
                            <label for="tinjauan2">3. Tinjauan 2: Penyakit/Kelainan</label>
                            <input type="text" id="tinjauan2" name="tinjauan2" 
                                   value="<?php echo htmlspecialchars($_POST['tinjauan2'] ?? ''); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="tinjauan3">4. Tinjauan 3: Bidang Ilmu & Keterampilan KG</label>
                            <input type="text" id="tinjauan3" name="tinjauan3" 
                                   value="<?php echo htmlspecialchars($_POST['tinjauan3'] ?? ''); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="vignette">5. Vignette</label>
                            <div class="text-editor">
                                <div class="toolbar">
                                    <button type="button" data-command="bold" title="Bold"><b>B</b></button>
                                    <button type="button" data-command="italic" title="Italic"><i>I</i></button>
                                    <button type="button" data-command="underline" title="Underline"><u>U</u></button>
                                    <button type="button" data-command="insertUnorderedList" title="Bullet List">• List</button>
                                    <button type="button" data-command="insertOrderedList" title="Numbered List">1. List</button>
                                </div>
                                <textarea id="vignette" name="vignette" class="rich-text"><?php echo htmlspecialchars($_POST['vignette'] ?? ''); ?></textarea>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="pertanyaan">6. Pertanyaan/Instruksi *</label>
                            <div class="text-editor">
                                <div class="toolbar">
                                    <button type="button" data-command="bold" title="Bold"><b>B</b></button>
                                    <button type="button" data-command="italic" title="Italic"><i>I</i></button>
                                    <button type="button" data-command="underline" title="Underline"><u>U</u></button>
                                    <button type="button" data-command="insertUnorderedList" title="Bullet List">• List</button>
                                    <button type="button" data-command="insertOrderedList" title="Numbered List">1. List</button>
                                </div>
                                <textarea id="pertanyaan" name="pertanyaan" class="rich-text" required><?php echo htmlspecialchars($_POST['pertanyaan'] ?? ''); ?></textarea>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="pilihan_a">7. Pilihan A</label>
                            <input type="text" id="pilihan_a" name="pilihan_a" 
                                   value="<?php echo htmlspecialchars($_POST['pilihan_a'] ?? ''); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="pilihan_b">8. Pilihan B</label>
                            <input type="text" id="pilihan_b" name="pilihan_b" 
                                   value="<?php echo htmlspecialchars($_POST['pilihan_b'] ?? ''); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="pilihan_c">9. Pilihan C</label>
                            <input type="text" id="pilihan_c" name="pilihan_c" 
                                   value="<?php echo htmlspecialchars($_POST['pilihan_c'] ?? ''); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="pilihan_d">10. Pilihan D</label>
                            <input type="text" id="pilihan_d" name="pilihan_d" 
                                   value="<?php echo htmlspecialchars($_POST['pilihan_d'] ?? ''); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="pilihan_e">11. Pilihan E</label>
                            <input type="text" id="pilihan_e" name="pilihan_e" 
                                   value="<?php echo htmlspecialchars($_POST['pilihan_e'] ?? ''); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="kunci">12. Kunci</label>
                            <select id="kunci" name="kunci">
                                <option value="">-- Pilih Kunci Jawaban --</option>
                                <option value="A" <?php echo ($_POST['kunci'] ?? '') == 'A' ? 'selected' : ''; ?>>A</option>
                                <option value="B" <?php echo ($_POST['kunci'] ?? '') == 'B' ? 'selected' : ''; ?>>B</option>
                                <option value="C" <?php echo ($_POST['kunci'] ?? '') == 'C' ? 'selected' : ''; ?>>C</option>
                                <option value="D" <?php echo ($_POST['kunci'] ?? '') == 'D' ? 'selected' : ''; ?>>D</option>
                                <option value="E" <?php echo ($_POST['kunci'] ?? '') == 'E' ? 'selected' : ''; ?>>E</option>
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <label for="department">13. Department</label>
                            <input type="text" id="department" name="department" 
                                   value="<?php echo htmlspecialchars($_POST['department'] ?? ''); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="alasan_singkat">14. Alasan Singkat</label>
                            <div class="text-editor">
                                <div class="toolbar">
                                    <button type="button" data-command="bold" title="Bold"><b>B</b></button>
                                    <button type="button" data-command="italic" title="Italic"><i>I</i></button>
                                    <button type="button" data-command="underline" title="Underline"><u>U</u></button>
                                </div>
                                <textarea id="alasan_singkat" name="alasan_singkat" class="rich-text"><?php echo htmlspecialchars($_POST['alasan_singkat'] ?? ''); ?></textarea>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="referensi">15. Referensi</label>
                            <div class="text-editor">
                                <div class="toolbar">
                                    <button type="button" data-command="bold" title="Bold"><b>B</b></button>
                                    <button type="button" data-command="italic" title="Italic"><i>I</i></button>
                                    <button type="button" data-command="underline" title="Underline"><u>U</u></button>
                                </div>
                                <textarea id="referensi" name="referensi" class="rich-text"><?php echo htmlspecialchars($_POST['referensi'] ?? ''); ?></textarea>
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label for="penulis">16. Penulis</label>
                            <input type="text" id="penulis" name="penulis" 
                                   value="<?php echo htmlspecialchars($_POST['penulis'] ?? ''); ?>">
                        </div>
                        
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">17. Simpan</button>
                            <button type="reset" class="btn btn-secondary">Reset</button>
                        </div>
                    </form>
                </div>

                <!-- Daftar Soal Teori yang Sudah Ada -->
                <div class="soal-list">
                    <h2>Daftar Soal Teori</h2>
                    <?php if (empty($soal_teori)): ?>
                        <p>Belum ada soal teori.</p>
                    <?php else: ?>
                        <table class="data-table">
                            <thead>
                                <tr>
                                    <th>No. Registrasi</th>
                                    <th>Pertanyaan</th>
                                    <th>Kompetensi Utama</th>
                                    <th>Kunci</th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($soal_teori as $soal): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($soal['nomor_registrasi']); ?></td>
                                        <td><?php echo strip_tags(substr($soal['pertanyaan'], 0, 100)) . '...'; ?></td>
                                        <td><?php echo htmlspecialchars($soal['tinjauan1']); ?></td>
                                        <td><?php echo htmlspecialchars($soal['kunci']); ?></td>
                                        <td>
                                            <a href="edit_soal_teori.php?id=<?php echo $soal['id']; ?>" class="btn btn-edit">Edit</a>
                                            <a href="hapus_soal_teori.php?id=<?php echo $soal['id']; ?>" class="btn btn-delete" onclick="return confirm('Yakin hapus soal?')">Hapus</a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
    
    <script src="../assets/js/bank_soal_teori.js"></script>
</body>
</html>