<?php
/**
 * view_bulk_hide_log.php
 * Zeigt Debug-Log für Bulk-Hide
 */

require_once 'db.php';
requireAdmin();

$logFile = __DIR__ . '/debug_bulk_hide.log';
$exists = file_exists($logFile);
$content = $exists ? file_get_contents($logFile) : '';
$lines = $exists ? count(file($logFile)) : 0;

// Löschen wenn angefragt
if (isset($_POST['clear_log'])) {
    file_put_contents($logFile, '');
    header('Location: view_bulk_hide_log.php');
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Bulk Hide Debug Log</title>
    <style>
        body { 
            font-family: 'Courier New', monospace; 
            margin: 20px; 
            background: #1e1e1e;
            color: #d4d4d4;
        }
        .header {
            background: #2d2d30;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        h1 { 
            margin: 0;
            color: #569cd6;
            font-size: 20px;
        }
        .info {
            background: #264f78;
            padding: 10px 15px;
            border-radius: 6px;
            margin-bottom: 20px;
        }
        .log-container {
            background: #1e1e1e;
            border: 1px solid #3e3e42;
            border-radius: 8px;
            padding: 20px;
            max-height: 80vh;
            overflow-y: auto;
        }
        .log-line {
            margin-bottom: 8px;
            padding: 4px 0;
            border-bottom: 1px solid #2d2d30;
        }
        .timestamp {
            color: #6a9955;
        }
        .error {
            color: #f48771;
            font-weight: bold;
        }
        .success {
            color: #4ec9b0;
        }
        .sql {
            color: #ce9178;
        }
        .separator {
            color: #569cd6;
            font-weight: bold;
            margin: 10px 0;
        }
        .btn {
            background: #0e639c;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }
        .btn:hover {
            background: #1177bb;
        }
        .btn-danger {
            background: #c72e0f;
        }
        .btn-danger:hover {
            background: #e81123;
        }
        .no-log {
            text-align: center;
            padding: 50px;
            color: #858585;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>🔍 Bulk Hide Debug Log</h1>
        <div>
            <a href="index.php" class="btn">← Zurück</a>
            <?php if ($exists && $lines > 0): ?>
            <form method="POST" style="display: inline;" onsubmit="return confirm('Log wirklich löschen?');">
                <button type="submit" name="clear_log" class="btn btn-danger">🗑️ Log löschen</button>
            </form>
            <?php endif; ?>
        </div>
    </div>

    <div class="info">
        📄 Log-Datei: <code><?php echo htmlspecialchars($logFile); ?></code><br>
        📊 Einträge: <strong><?php echo $lines; ?></strong> Zeilen<br>
        🕐 Zuletzt geändert: <?php echo $exists ? date('d.m.Y H:i:s', filemtime($logFile)) : 'Nie'; ?>
    </div>

    <div class="log-container">
        <?php if (!$exists || $lines === 0): ?>
        <div class="no-log">
            ℹ️ Noch keine Log-Einträge vorhanden.<br>
            Führe eine Bulk-Hide Operation aus um Logs zu generieren.
        </div>
        <?php else: ?>
            <?php
            $lines = explode("\n", $content);
            foreach ($lines as $line) {
                if (empty(trim($line))) continue;
                
                $class = '';
                if (strpos($line, 'ERROR') !== false || strpos($line, 'EXCEPTION') !== false) {
                    $class = 'error';
                } elseif (strpos($line, 'SUCCESS') !== false || strpos($line, 'COMPLETED') !== false) {
                    $class = 'success';
                } elseif (strpos($line, 'SQL:') !== false) {
                    $class = 'sql';
                } elseif (strpos($line, '===') !== false) {
                    $class = 'separator';
                }
                
                echo '<div class="log-line ' . $class . '">';
                
                // Timestamp hervorheben
                $line = preg_replace('/\[([\d\-: ]+)\]/', '<span class="timestamp">[$1]</span>', htmlspecialchars($line));
                
                echo $line;
                echo '</div>';
            }
            ?>
        <?php endif; ?>
    </div>

    <script>
        // Auto-scroll zum Ende
        document.querySelector('.log-container').scrollTop = document.querySelector('.log-container').scrollHeight;
        
        // Auto-Reload alle 5 Sekunden
        setTimeout(() => location.reload(), 5000);
    </script>
</body>
</html>
