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

if (!isAdmin()) {
    die('Unauthorized');
}

echo "<h2>🖼️ Debug Images in Database</h2>";

// Cek gambar di database
$tables = [
    'aspek_penilaian' => ['nama_aspek', 'deskripsi_0', 'deskripsi_1', 'deskripsi_2'],
    'stations' => ['deskripsi', 'instruksi_peserta', 'instruksi_penguji']
];

foreach ($tables as $table => $columns) {
    echo "<h3>Table: $table</h3>";
    
    foreach ($columns as $column) {
        $stmt = $pdo->prepare("SELECT id, $column FROM $table WHERE $column LIKE '%<img%' LIMIT 5");
        $stmt->execute();
        $records = $stmt->fetchAll();
        
        echo "<h4>Column: $column (" . count($records) . " records with images)</h4>";
        
        foreach ($records as $record) {
            echo "<div style='border: 1px solid #ccc; padding: 10px; margin: 10px;'>";
            echo "<strong>ID: {$record['id']}</strong><br>";
            
            preg_match_all('/<img[^>]+src="([^"]*)"[^>]*>/i', $record[$column], $matches);
            
            foreach ($matches[1] as $imgSrc) {
                echo "Image SRC: <code>" . htmlspecialchars($imgSrc) . "</code><br>";
                
                // Test URL
                $testUrls = [
                    $imgSrc,
                    '/soca-osce-system/uploads/images/' . basename($imgSrc),
                    'http://localhost/soca-osce-system/uploads/images/' . basename($imgSrc)
                ];
                
                foreach ($testUrls as $testUrl) {
                    echo "Test: <a href='$testUrl' target='_blank'>$testUrl</a> ";
                    echo "<img src='$testUrl' style='width: 50px; height: 50px; border: 1px solid red;' onerror='this.style.border=\"3px solid red\"' onload='this.style.border=\"3px solid green\"'><br>";
                }
            }
            
            echo "</div>";
        }
    }
}
?>