<?php
// get_aspek_penilaian.php
require_once '../includes/auth.php';

if (!isPenguji()) {
    header('HTTP/1.1 403 Forbidden');
    exit('Unauthorized access');
}

header('Content-Type: text/html; charset=utf-8');

$station_id = $_POST['station_id'] ?? null;
$peserta_id = $_POST['peserta_id'] ?? null;
$ujian_id = $_POST['ujian_id'] ?? null;

if (!$station_id || !$peserta_id || !$ujian_id) {
    echo '<div class="alert alert-error">Data tidak lengkap!</div>';
    exit;
}

try {
    // Ambil aspek penilaian untuk station ini
    $stmt = $pdo->prepare("
        SELECT * FROM aspek_penilaian 
        WHERE station_id = ? 
        ORDER BY nomor_urut
    ");
    $stmt->execute([$station_id]);
    $aspeks = $stmt->fetchAll();

    if (empty($aspeks)) {
        echo '<div class="alert alert-warning">Tidak ada aspek penilaian untuk station ini.</div>';
        exit;
    }

    // Function untuk render HTML content dengan gambar
    function renderHTMLContent($content) {
        if (empty($content)) return '';
        
        // Decode HTML entities
        $content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
        
        // Security: Remove potentially dangerous tags and attributes
        $content = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', '', $content);
        $content = preg_replace('/on\w+=\s*"[^"]*"\s*/i', '', $content);
        $content = preg_replace('/on\w+=\s*\'[^\']*\'\s*/i', '', $content);
        
        return $content;
    }

    echo '<div class="aspek-container">';
    
    foreach ($aspeks as $index => $aspek) {
        // Cek apakah sudah ada penilaian sebelumnya
        $stmt = $pdo->prepare("
            SELECT nilai, catatan FROM penilaian 
            WHERE ujian_id = ? AND peserta_id = ? AND station_id = ? 
            AND penguji_id = ? AND aspek_id = ?
        ");
        $stmt->execute([$ujian_id, $peserta_id, $station_id, $_SESSION['user_id'], $aspek['id']]);
        $penilaian_sebelumnya = $stmt->fetch();
        
        $nilai_sebelumnya = $penilaian_sebelumnya ? $penilaian_sebelumnya['nilai'] : '';
        $catatan_sebelumnya = $penilaian_sebelumnya ? $penilaian_sebelumnya['catatan'] : '';
        
        // Render nama aspek dengan HTML (termasuk gambar)
        $nama_aspek_html = renderHTMLContent($aspek['nama_aspek']);
        
        echo '
        <div class="aspek-item">
            <div class="aspek-header">
                <div class="aspek-number">' . ($index + 1) . '</div>
                <div class="aspek-title">
                    <h4>Aspek Penilaian</h4>
                    <div class="aspek-nama">' . $nama_aspek_html . '</div>
                </div>
            </div>
            
            <div class="skor-options">';
        
        // Skor 0
        if ($aspek['skor_0']) {
            $deskripsi_0_html = renderHTMLContent($aspek['deskripsi_0']);
            $checked_0 = ($nilai_sebelumnya === '0' || $nilai_sebelumnya === 0) ? 'checked' : '';
            echo '
                <label class="skor-option">
                    <input type="radio" name="skor_' . $aspek['id'] . '" value="0" ' . $checked_0 . ' required>
                    <div class="skor-card skor-0">
                        <div class="skor-value">0</div>
                        <div class="skor-description">' . $deskripsi_0_html . '</div>
                    </div>
                </label>';
        }
        
        // Skor 1
        if ($aspek['skor_1']) {
            $deskripsi_1_html = renderHTMLContent($aspek['deskripsi_1']);
            $checked_1 = ($nilai_sebelumnya === '1' || $nilai_sebelumnya === 1) ? 'checked' : '';
            echo '
                <label class="skor-option">
                    <input type="radio" name="skor_' . $aspek['id'] . '" value="1" ' . $checked_1 . ' required>
                    <div class="skor-card skor-1">
                        <div class="skor-value">1</div>
                        <div class="skor-description">' . $deskripsi_1_html . '</div>
                    </div>
                </label>';
        }
        
        // Skor 2
        if ($aspek['skor_2']) {
            $deskripsi_2_html = renderHTMLContent($aspek['deskripsi_2']);
            $checked_2 = ($nilai_sebelumnya === '2' || $nilai_sebelumnya === 2) ? 'checked' : '';
            echo '
                <label class="skor-option">
                    <input type="radio" name="skor_' . $aspek['id'] . '" value="2" ' . $checked_2 . ' required>
                    <div class="skor-card skor-2">
                        <div class="skor-value">2</div>
                        <div class="skor-description">' . $deskripsi_2_html . '</div>
                    </div>
                </label>';
        }
        
        echo '
            </div>
            
            <!-- CATATAN DIPINDAHKAN KE BAGIAN INI - SETELAH SKOR OPTIONS -->
            <div class="catatan-group">
                <label for="catatan_' . $aspek['id'] . '">
                    <i class="fas fa-sticky-note"></i>
                    Catatan untuk Aspek Ini
                </label>
                <textarea 
                    id="catatan_' . $aspek['id'] . '" 
                    name="catatan_' . $aspek['id'] . '" 
                    placeholder="Masukkan catatan khusus untuk aspek ini (opsional)..."
                    rows="3"
                >' . htmlspecialchars($catatan_sebelumnya) . '</textarea>
            </div>
        </div>';
    }
    
    echo '</div>';

} catch (Exception $e) {
    echo '<div class="alert alert-error">Error: ' . htmlspecialchars($e->getMessage()) . '</div>';
}
?>