<?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'] ?? '';
    $sub_tinjauan2 = $_POST['sub_tinjauan2'] ?? '';
    $tinjauan3 = $_POST['tinjauan3'] ?? '';
    $tinjauan4 = $_POST['tinjauan4'] ?? '';
    $tujuan = $_POST['tujuan'] ?? '';
    $skenario_klinik = $_POST['skenario_klinik'] ?? '';
    $tugas_kandidat = $_POST['tugas_kandidat'] ?? '';
    $tugas_penguji = $_POST['tugas_penguji'] ?? '';
    $instruksi_pasien = $_POST['instruksi_pasien'] ?? '';
    $peralatan = $_POST['peralatan'] ?? '';
    $gambar_soal = $_POST['gambar_soal'] ?? '';
    $department = $_POST['department'] ?? '';
    $referensi = $_POST['referensi'] ?? '';
    $penulis = $_POST['penulis'] ?? '';
    
    // Validasi data
    if (empty($nomor_registrasi) || empty($tinjauan1) || empty($skenario_klinik)) {
        $error = 'Nomor Registrasi, Tinjauan 1, dan Skenario Klinik harus diisi!';
    } else {
        // Simpan ke database
        try {
            $stmt = $pdo->prepare("INSERT INTO bank_soal_praktik (
                nomor_registrasi, tinjauan1, tinjauan2, sub_tinjauan2, tinjauan3, tinjauan4,
                tujuan, skenario_klinik, tugas_kandidat, tugas_penguji, instruksi_pasien,
                peralatan, gambar_soal, department, referensi, penulis
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            
            $stmt->execute([
                $nomor_registrasi, $tinjauan1, $tinjauan2, $sub_tinjauan2, $tinjauan3, $tinjauan4,
                $tujuan, $skenario_klinik, $tugas_kandidat, $tugas_penguji, $instruksi_pasien,
                $peralatan, $gambar_soal, $department, $referensi, $penulis
            ]);
            
            $success = 'Soal praktik berhasil disimpan!';
            
            // Reset form jika berhasil
            $_POST = array();
            
        } catch (PDOException $e) {
            $error = 'Terjadi kesalahan: ' . $e->getMessage();
        }
    }
}

// Ambil data soal praktik untuk ditampilkan
$soal_praktik = [];
try {
    $stmt = $pdo->query("SELECT * FROM bank_soal_praktik ORDER BY created_at DESC");
    $soal_praktik = $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 Praktik - SOCA OSCE System</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="stylesheet" href="../assets/css/bank_soal_praktik.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">
                    <i class="fas fa-question-circle"></i> Bank Soal Teori
                </a></li>
                <li><a href="list_soal_teori.php">
                    <i class="fas fa-question-circle"></i> List 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-stethoscope"></i> Bank Soal Praktik
                </a></li>
                <li><a href="soal.php">
                    <i class="fas fa-question-circle"></i> Kelola Soal
                </a></li>
                <li><a href="ujian.php">
                    <i class="fas fa-clipboard-list"></i> Kelola Ujian
                </a></li>
                <li><a href="peserta.php">
                    <i class="fas fa-users"></i> Kelola Peserta
                </a></li>
                <li><a href="penguji.php">
                    <i class="fas fa-user-check"></i> Kelola Penguji
                </a></li>
                <li><a href="nilai.php">
                    <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 Uji Praktik</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><i class="fas fa-plus-circle"></i> Tambah Soal Praktik Baru</h2>
                    <form method="POST" action="" id="soalPraktikForm" enctype="multipart/form-data">
                        <!-- Bagian 1: Informasi Dasar -->
                        <div class="form-section">
                            <h3 class="section-title">Informasi Dasar Soal</h3>
                            
                            <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-row">
                                <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: Kelompok Penyakit/Kelainan</label>
                                    <input type="text" id="tinjauan2" name="tinjauan2" 
                                           value="<?php echo htmlspecialchars($_POST['tinjauan2'] ?? ''); ?>">
                                </div>
                            </div>
                            
                            <div class="form-row">
                                <div class="form-group">
                                    <label for="sub_tinjauan2">4. Sub Tinjauan 2: Penyakit/Kelainan</label>
                                    <input type="text" id="sub_tinjauan2" name="sub_tinjauan2" 
                                           value="<?php echo htmlspecialchars($_POST['sub_tinjauan2'] ?? ''); ?>">
                                </div>
                                
                                <div class="form-group">
                                    <label for="tinjauan3">5. Tinjauan 3: Ranah Keterampilan Klinis</label>
                                    <input type="text" id="tinjauan3" name="tinjauan3" 
                                           value="<?php echo htmlspecialchars($_POST['tinjauan3'] ?? ''); ?>">
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label for="tinjauan4">6. Tinjauan 4: Bidang Ilmu</label>
                                <input type="text" id="tinjauan4" name="tinjauan4" 
                                       value="<?php echo htmlspecialchars($_POST['tinjauan4'] ?? ''); ?>">
                            </div>
                        </div>

                        <!-- Bagian 2: Konten Soal -->
                        <div class="form-section">
                            <h3 class="section-title">Konten Soal Praktik</h3>
                            
                            <div class="form-group">
                                <label for="tujuan">7. Tujuan</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="tujuan" name="tujuan" class="rich-text"><?php echo htmlspecialchars($_POST['tujuan'] ?? ''); ?></textarea>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label for="skenario_klinik">8. Skenario Klinik *</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="skenario_klinik" name="skenario_klinik" class="rich-text" required><?php echo htmlspecialchars($_POST['skenario_klinik'] ?? ''); ?></textarea>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label for="tugas_kandidat">9. Tugas untuk Kandidat</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="tugas_kandidat" name="tugas_kandidat" class="rich-text"><?php echo htmlspecialchars($_POST['tugas_kandidat'] ?? ''); ?></textarea>
                                </div>
                            </div>
                        </div>

                        <!-- Bagian 3: Instruksi dan Perlengkapan -->
                        <div class="form-section">
                            <h3 class="section-title">Instruksi dan Perlengkapan</h3>
                            
                            <div class="form-group">
                                <label for="tugas_penguji">10. Tugas untuk Penguji</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="tugas_penguji" name="tugas_penguji" class="rich-text"><?php echo htmlspecialchars($_POST['tugas_penguji'] ?? ''); ?></textarea>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label for="instruksi_pasien">11. Instruksi Untuk Pasien Simulasi</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="instruksi_pasien" name="instruksi_pasien" class="rich-text"><?php echo htmlspecialchars($_POST['instruksi_pasien'] ?? ''); ?></textarea>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label for="peralatan">12. Peralatan yang dibutuhkan</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="peralatan" name="peralatan" class="rich-text"><?php echo htmlspecialchars($_POST['peralatan'] ?? ''); ?></textarea>
                                </div>
                            </div>
                        </div>

                        <!-- Bagian 4: Informasi Tambahan -->
                        <div class="form-section">
                            <h3 class="section-title">Informasi Tambahan</h3>
                            
                            <div class="form-group">
                                <label for="gambar_soal">13. Gambar Soal (URL)</label>
                                <input type="text" id="gambar_soal" name="gambar_soal" 
                                       value="<?php echo htmlspecialchars($_POST['gambar_soal'] ?? ''); ?>"
                                       placeholder="Masukkan URL gambar atau path file">
                                <small class="form-help">Contoh: https://example.com/gambar.jpg atau /assets/images/soal1.png</small>
                            </div>
                            
                            <div class="form-group">
                                <label for="department">14. Department</label>
                                <input type="text" id="department" name="department" 
                                       value="<?php echo htmlspecialchars($_POST['department'] ?? ''); ?>">
                            </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>

                        <!-- Tombol Submit -->
                        <div class="form-actions">
                            <button type="submit" class="btn btn-primary btn-large">
                                <i class="fas fa-save"></i> 17. Simpan Soal Praktik
                            </button>
                            <button type="reset" class="btn btn-secondary">
                                <i class="fas fa-undo"></i> Reset Form
                            </button>
                        </div>
                    </form>
                </div>

                <!-- Daftar Soal Praktik yang Sudah Ada -->
                <div class="soal-list">
                    <h2><i class="fas fa-list"></i> Daftar Soal Praktik</h2>
                    <?php if (empty($soal_praktik)): ?>
                        <div class="empty-state">
                            <i class="fas fa-stethoscope"></i>
                            <h3>Belum ada soal praktik</h3>
                            <p>Silakan tambah soal praktik menggunakan form di atas</p>
                        </div>
                    <?php else: ?>
                        <table class="data-table">
                            <thead>
                                <tr>
                                    <th>No. Registrasi</th>
                                    <th>Skenario Klinik</th>
                                    <th>Kompetensi Utama</th>
                                    <th>Department</th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($soal_praktik as $soal): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($soal['nomor_registrasi']); ?></td>
                                        <td><?php echo strip_tags(substr($soal['skenario_klinik'], 0, 100)) . '...'; ?></td>
                                        <td><?php echo htmlspecialchars($soal['tinjauan1']); ?></td>
                                        <td><?php echo htmlspecialchars($soal['department'] ?? '-'); ?></td>
                                        <td>
                                            <a href="edit_soal_praktik.php?id=<?php echo $soal['id']; ?>" class="btn btn-edit">
                                                <i class="fas fa-edit"></i> Edit
                                            </a>
                                            <a href="hapus_soal_praktik.php?id=<?php echo $soal['id']; ?>" 
                                               class="btn btn-delete" 
                                               onclick="return confirm('Yakin hapus soal praktik ini?')">
                                                <i class="fas fa-trash"></i> Hapus
                                            </a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
    
    <script src="../assets/js/bank_soal_praktik.js"></script>
</body>
</html>