<?php ob_start(); ?>

<?php admin_content_header(__('admin.system.error_log_title'), 'alert-triangle', [], __('admin.system.error_log_subtitle')); ?>

<?php if ($flash = flash('success')): ?>
<div class="alert alert-success"><?= e($flash) ?></div>
<?php endif; ?>

<div class="system-grid">

    <!-- ── Log Overview ──────────────────────── -->
    <div class="sys-card">
        <h3 class="sys-card__title"><?= __('admin.system.error_log_overview') ?></h3>
        <table class="sys-table">
            <tr>
                <td><?= __('admin.system.error_log_size') ?></td>
                <td>
                    <?php if ($logSize > 0): ?>
                        <?= $logSize < 1048576 ? round($logSize / 1024, 1) . ' KB' : round($logSize / 1048576, 1) . ' MB' ?>
                    <?php else: ?>
                        <span class="badge badge-success"><?= __('admin.system.error_log_empty') ?></span>
                    <?php endif; ?>
                </td>
            </tr>
            <tr>
                <td><?= __('admin.system.error_log_entries_shown') ?></td>
                <td><?= count($entries) ?></td>
            </tr>
            <?php if (!empty($archives)): ?>
            <tr>
                <td><?= __('admin.system.error_log_archives') ?></td>
                <td><?= count($archives) ?> <?= __('admin.system.error_log_files') ?></td>
            </tr>
            <?php endif; ?>
        </table>

        <?php if ($logSize > 0): ?>
        <div style="margin-top: 1rem;">
            <form method="POST" action="<?= admin_url('system/error-log/clear') ?>" style="display:inline"
                  onsubmit="return confirm('<?= e(__('admin.system.error_log_clear_confirm')) ?>')">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-danger btn-sm">
                    <?= icon('trash', 16) ?> <?= __('admin.system.error_log_clear') ?>
                </button>
            </form>
        </div>
        <?php endif; ?>
    </div>

    <!-- ── Archives ──────────────────────── -->
    <?php if (!empty($archives)): ?>
    <div class="sys-card">
        <h3 class="sys-card__title"><?= __('admin.system.error_log_archive_list') ?></h3>
        <table class="sys-table">
            <thead>
                <tr>
                    <th><?= __('admin.system.error_log_file') ?></th>
                    <th><?= __('admin.system.error_log_size') ?></th>
                    <th><?= __('admin.system.error_log_date') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($archives as $a): ?>
                <tr>
                    <td><code><?= e($a['name']) ?></code></td>
                    <td><?= $a['size'] < 1048576 ? round($a['size'] / 1024, 1) . ' KB' : round($a['size'] / 1048576, 1) . ' MB' ?></td>
                    <td><?= e($a['date']) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>

</div>

<!-- ── Log Entries ──────────────────────── -->
<?php if (!empty($entries)): ?>
<div class="card" style="margin-top: 1.5rem;">
    <div class="card-header">
        <h3><?= __('admin.system.error_log_recent') ?></h3>
    </div>
    <div class="card-body" style="max-height: 600px; overflow-y: auto;">
        <pre class="error-log-output" style="font-size: 0.75rem; line-height: 1.6; white-space: pre-wrap; word-break: break-all; margin: 0; padding: 1rem; background: var(--sem-bg-surface-sunken, #f8fafc); border-radius: var(--p-radius-md, 6px);"><?php
            foreach ($entries as $line):
                $escaped = e($line);
                // Highlight error levels
                $escaped = preg_replace('/\b(Fatal error|Parse error|Error)\b/i', '<strong style="color:var(--sem-feedback-error,#dc2626)">$1</strong>', $escaped);
                $escaped = preg_replace('/\b(Warning)\b/i', '<strong style="color:var(--sem-feedback-warning,#d97706)">$1</strong>', $escaped);
                $escaped = preg_replace('/\b(Notice|Deprecated)\b/i', '<strong style="color:var(--sem-text-tertiary,#94a3b8)">$1</strong>', $escaped);
                echo $escaped . "\n";
            endforeach;
        ?></pre>
    </div>
</div>
<?php else: ?>
<div class="alert alert-info" style="margin-top: 1.5rem;">
    <?= icon('check-circle', 16) ?> <?= __('admin.system.error_log_no_errors') ?>
</div>
<?php endif; ?>

<?php $content = ob_get_clean(); include __DIR__ . '/../layouts/admin.php'; ?>
