<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

    <div class="d-flex flex-wrap justify-content-between align-items-center mb-3 gap-2">
        <div>
            <h3 class="mb-0"><i class="bi bi-bug-fill text-danger me-2"></i><?= __('system.error_logs','Error Logs') ?></h3>
            <small class="text-muted"><?= __('system.error_logs_subtitle','Review application issues and important security log entries in one place.') ?></small>
        </div>
        <div class="d-flex gap-2">
            <a href="/admin/system/cleanup" class="btn btn-outline-secondary"><i class="bi bi-arrow-left me-1"></i><?= __('system.back_to_cleanup','Back to Cleanup') ?></a>
        </div>
    </div>

    <?php if (isset($_GET['done'])): ?>
        <div class="alert alert-success py-2">
            <i class="bi bi-check-circle me-1"></i>
            <?= __('system.cleanup_done','Cleanup finished successfully.') ?>
            <?php if (isset($_GET['rows'])): ?><span class="ms-1">(<?= (int)$_GET['rows'] ?>)</span><?php endif; ?>
        </div>
    <?php elseif (isset($_GET['error'])): ?>
        <div class="alert alert-danger py-2">
            <i class="bi bi-exclamation-triangle me-1"></i>
            <?= __('system.invalid_cleanup_action','Invalid cleanup action.') ?>
        </div>
    <?php endif; ?>

    <div class="row g-3 mb-4">
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="card-body">
                    <form method="GET" class="row g-2 align-items-end">
                        <div class="col-md-3">
                            <label class="form-label"><?= __('system.type','Type') ?></label>
                            <select class="form-select" name="type">
                                <option value="issues" <?= ($requestedType ?? 'issues') === 'issues' ? 'selected' : '' ?>><?= __('system.issues_only','Errors & warnings') ?> (<?= (int)($stats['issues'] ?? 0) ?>)</option>
                                <?php foreach (($filterOptions ?? []) as $opt): if ($opt === 'issues') continue; ?>
                                    <option value="<?= htmlspecialchars($opt) ?>" <?= ($requestedType ?? '') === $opt ? 'selected' : '' ?>><?= htmlspecialchars($opt) ?><?= isset($stats['all_types'][$opt]) ? ' (' . (int)$stats['all_types'][$opt] . ')' : '' ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-5">
                            <label class="form-label"><?= __('system.search','Search') ?></label>
                            <input type="text" class="form-control" name="q" value="<?= htmlspecialchars($search ?? '', ENT_QUOTES) ?>" placeholder="<?= __('system.error_search_placeholder','message / url / ip / user agent') ?>">
                        </div>
                        <div class="col-md-2">
                            <label class="form-label"><?= __('system.limit','Limit') ?></label>
                            <input type="number" class="form-control" name="limit" min="20" max="500" value="<?= (int)($limit ?? 200) ?>">
                        </div>
                        <div class="col-md-2 d-grid"><button class="btn btn-primary" type="submit"><i class="bi bi-funnel me-1"></i><?= __('actions.filter','Filter') ?></button></div>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body">
                    <div class="d-flex flex-wrap gap-2">
                        <span class="badge rounded-pill text-bg-danger"><?= __('system.errors','Errors') ?>: <?= (int)($stats['error'] ?? 0) ?></span>
                        <span class="badge rounded-pill text-bg-warning"><?= __('system.warnings','Warnings') ?>: <?= (int)($stats['warning'] ?? 0) ?></span>
                        <span class="badge rounded-pill text-bg-secondary"><?= __('system.issues_only','Errors & warnings') ?>: <?= (int)($stats['issues'] ?? 0) ?></span>
                    </div>
                    <div class="text-muted small mt-3">
                        <?= __('system.error_logs_help','This page also includes the old Security Settings log tab content, with filtering and copy support.') ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-md-4">
            <form method="POST" action="/admin/system/errors/clear" class="card border-0 shadow-sm h-100">
                <?= \Core\System\Csrf::field(); ?>
                <input type="hidden" name="action" value="clear_errors_old">
                <div class="card-body d-grid gap-2">
                    <div class="fw-semibold"><?= __('system.clean_old_errors','Clean old errors') ?></div>
                    <div class="input-group"><span class="input-group-text"><?= __('system.days','Days') ?></span><input type="number" class="form-control" name="days" min="1" value="30"></div>
                    <button class="btn btn-outline-danger" type="submit"><?= __('system.delete_old_errors','Delete old errors') ?></button>
                </div>
            </form>
        </div>
        <div class="col-md-4">
            <form method="POST" action="/admin/system/errors/clear" class="card border-0 shadow-sm h-100">
                <?= \Core\System\Csrf::field(); ?>
                <input type="hidden" name="action" value="clear_warnings_old">
                <div class="card-body d-grid gap-2">
                    <div class="fw-semibold"><?= __('system.clean_old_warnings','Clean old warnings') ?></div>
                    <div class="input-group"><span class="input-group-text"><?= __('system.days','Days') ?></span><input type="number" class="form-control" name="days" min="1" value="30"></div>
                    <button class="btn btn-outline-warning" type="submit"><?= __('system.delete_old_warnings','Delete old warnings') ?></button>
                </div>
            </form>
        </div>
        <div class="col-md-4">
            <form method="POST" action="/admin/system/errors/clear" class="card border-0 shadow-sm h-100">
                <?= \Core\System\Csrf::field(); ?>
                <input type="hidden" name="action" value="clear_all_issue_logs">
                <div class="card-body d-grid gap-2">
                    <div class="fw-semibold"><?= __('system.delete_all_issue_logs','Delete all error & warning logs') ?></div>
                    <div class="text-muted small"><?= __('system.delete_all_issue_logs_help','Does not remove other security log types such as ddos, ban, or 404 unless you filter and review them separately.') ?></div>
                    <button class="btn btn-danger" type="submit" onclick="return confirm('<?= __('system.confirm_delete_all','Are you sure?') ?>')"><?= __('system.delete_all_issue_logs_button','Delete all issue logs') ?></button>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive" style="max-height:70vh">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light sticky-top">
                        <tr>
                            <th style="width:110px"><?= __('system.type','Type') ?></th>
                            <th style="width:130px"><?= __('system.ip','IP') ?></th>
                            <th><?= __('system.message','Message') ?></th>
                            <th style="width:220px"><?= __('system.url','URL') ?></th>
                            <th style="width:180px"><?= __('system.date','Date') ?></th>
                            <th style="width:120px"><?= __('actions.actions','Actions') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php if (!empty($logs)): foreach ($logs as $log): ?>
                        <?php
                            $type = (string)($log['type'] ?? '');
                            $badgeMap = ['error' => 'danger', 'warning' => 'warning', 'ddos' => 'dark', '404' => 'secondary', 'ban' => 'danger', 'blocked' => 'dark'];
                            $badge = $badgeMap[$type] ?? 'secondary';
                            $copyText = trim(
                                'Type: ' . $type . "\n" .
                                'Date: ' . (string)($log['created_at'] ?? '') . "\n" .
                                'IP: ' . (string)($log['ip'] ?? '') . "\n" .
                                'URL: ' . (string)($log['url'] ?? '') . "\n" .
                                'Message: ' . (string)($log['message'] ?? '')
                            );
                        ?>
                        <tr>
                            <td><span class="badge bg-<?= $badge ?>"><?= htmlspecialchars(strtoupper($type)) ?></span></td>
                            <td><code><?= htmlspecialchars((string)($log['ip'] ?? '')) ?></code></td>
                            <td><div style="min-width:280px; white-space:pre-wrap"><?= htmlspecialchars((string)($log['message'] ?? '')) ?></div></td>
                            <td class="small text-muted" style="word-break:break-all"><?= htmlspecialchars((string)($log['url'] ?? '')) ?></td>
                            <td class="small text-nowrap"><?= htmlspecialchars((string)($log['created_at'] ?? '')) ?></td>
                            <td>
                                <button type="button"
                                        class="btn btn-sm btn-outline-secondary js-copy-log"
                                        data-copy="<?= htmlspecialchars($copyText, ENT_QUOTES) ?>"
                                        title="<?= __('system.copy_log','Copy log') ?>">
                                    <i class="bi bi-copy"></i>
                                </button>
                            </td>
                        </tr>
                    <?php endforeach; else: ?>
                        <tr><td colspan="6" class="text-center text-muted py-4"><?= __('system.no_logs_found','No logs found.') ?></td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</section>
</div>

<script>
document.addEventListener('click', async function (e) {
  const btn = e.target.closest('.js-copy-log');
  if (!btn) return;
  const text = btn.getAttribute('data-copy') || '';
  try {
    await navigator.clipboard.writeText(text);
    const original = btn.innerHTML;
    btn.innerHTML = '<i class="bi bi-check2"></i>';
    setTimeout(() => btn.innerHTML = original, 1200);
  } catch (err) {
    const ta = document.createElement('textarea');
    ta.value = text;
    document.body.appendChild(ta);
    ta.select();
    document.execCommand('copy');
    ta.remove();
  }
});
</script>
