<?php
$server = $server ?? [];
$logOutput = $logOutput ?? '';
$logType = $logType ?? 'syslog';
$lines = $lines ?? 100;
$logFiles = $logFiles ?? [];
?>

<div class="page-header">
    <div class="page-header-left">
        <div class="back-link">
            <a href="/servers/<?= $server['id'] ?>"><i class="fas fa-arrow-left"></i> Back to Server</a>
        </div>
        <h1 class="page-title">System Logs</h1>
        <p class="page-subtitle"><?= htmlspecialchars($server['name']) ?></p>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <div class="card-filters">
            <select id="logType" class="form-select" onchange="updateLog()">
                <?php foreach ($logFiles as $file): ?>
                    <option value="<?= $file ?>" <?= $logType === $file ? 'selected' : '' ?>>
                        <?= ucfirst($file) ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <select id="logLines" class="form-select" onchange="updateLog()">
                <option value="50" <?= $lines == 50 ? 'selected' : '' ?>>50 lines</option>
                <option value="100" <?= $lines == 100 ? 'selected' : '' ?>>100 lines</option>
                <option value="200" <?= $lines == 200 ? 'selected' : '' ?>>200 lines</option>
                <option value="500" <?= $lines == 500 ? 'selected' : '' ?>>500 lines</option>
            </select>
            <button class="btn btn-secondary btn-sm" onclick="updateLog()">
                <i class="fas fa-sync-alt"></i> Refresh
            </button>
        </div>
    </div>
    <div class="card-body">
        <?php if (empty(trim($logOutput))): ?>
            <div class="empty-state">
                <i class="fas fa-file-alt empty-icon"></i>
                <p>No log data available.</p>
            </div>
        <?php else: ?>
            <div class="log-viewer">
                <?php
                $logLines = explode("\n", trim($logOutput));
                foreach ($logLines as $line):
                    $lineClass = 'log-line';
                    $lower = mb_strtolower($line);
                    if (str_contains($lower, 'error') || str_contains($lower, 'critical') || str_contains($lower, 'emergency')) {
                        $lineClass .= ' log-line-error';
                    } elseif (str_contains($lower, 'warning') || str_contains($lower, 'warn')) {
                        $lineClass .= ' log-line-warning';
                    } elseif (str_contains($lower, 'notice') || str_contains($lower, 'info')) {
                        $lineClass .= ' log-line-info';
                    }
                ?>
                    <div class="<?= $lineClass ?>">
                        <code><?= htmlspecialchars($line) ?></code>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<script>
function updateLog() {
    const type = document.getElementById('logType').value;
    const lines = document.getElementById('logLines').value;
    window.location.href = '/servers/<?= $server['id'] ?>/logs?type=' + type + '&lines=' + lines;
}
</script>
