<?php
require_once __DIR__ . '/../includes/functions.php';
$admin = require_admin();
$adminTitle = 'Error Logs';
if (function_exists('mj428_ensure_schema')) { mj428_ensure_schema(); }
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verify_csrf();
    if (isset($_POST['mark_resolved']) && !empty($_POST['ids'])) {
        $ids = array_values(array_filter(array_map('intval', (array)$_POST['ids'])));
        if ($ids) { q('UPDATE mj_error_logs SET is_resolved=1 WHERE id IN ('.implode(',', array_fill(0,count($ids),'?')).')', $ids); flash('success','Selected errors marked resolved.'); }
        redirect('error-logs.php');
    }
    if (isset($_POST['run_repair']) && function_exists('mj428_run_safe_repair')) { mj428_run_safe_repair('error_logs_page'); flash('success','Safe repair completed.'); redirect('error-logs.php'); }
}
$filter = preg_replace('/[^a-zA-Z0-9_\-]/','', (string)($_GET['type'] ?? ''));
$params=[]; $where='1=1'; if($filter){$where.=' AND error_type=?'; $params[]=$filter;}
$logs=[]; $types=[];
try { if(function_exists('mj428_table_exists') && mj428_table_exists('mj_error_logs')) { $logs=all_rows('SELECT * FROM mj_error_logs WHERE '.$where.' ORDER BY id DESC LIMIT 200',$params); $types=all_rows('SELECT error_type,COUNT(*) c FROM mj_error_logs GROUP BY error_type ORDER BY c DESC'); } } catch(Throwable $e) {}
include __DIR__ . '/_header.php';
?>
<div class="card card-danger card-outline">
  <div class="card-header d-flex justify-content-between align-items-center"><h3 class="card-title"><i class="fas fa-bug mr-2"></i>Error Logs</h3><div><a href="auto-repair.php" class="btn btn-sm btn-outline-primary">Auto Repair</a></div></div>
  <div class="card-body">
    <form method="get" class="form-inline mb-3"><label class="mr-2">Type</label><select name="type" class="form-control mr-2"><option value="">All</option><?php foreach($types as $t): ?><option value="<?= e($t['error_type']) ?>" <?= $filter===$t['error_type']?'selected':'' ?>><?= e($t['error_type']) ?> (<?= e($t['c']) ?>)</option><?php endforeach; ?></select><button class="btn btn-secondary">Filter</button></form>
    <form method="post"><?= csrf_input() ?>
      <div class="mb-2"><button name="mark_resolved" value="1" class="btn btn-success btn-sm"><i class="fas fa-check mr-1"></i>Mark Selected Resolved</button> <button name="run_repair" value="1" class="btn btn-primary btn-sm"><i class="fas fa-tools mr-1"></i>Run Safe Repair</button></div>
      <div class="table-responsive"><table class="table table-sm table-hover"><thead><tr><th></th><th>ID</th><th>Type</th><th>Severity</th><th>Message</th><th>File</th><th>Line</th><th>When</th></tr></thead><tbody><?php foreach($logs as $l): ?><tr class="<?= !empty($l['is_resolved'])?'text-muted':'' ?>"><td><input type="checkbox" name="ids[]" value="<?= e($l['id']) ?>"></td><td><?= e($l['id']) ?></td><td><span class="badge badge-light"><?= e($l['error_type']) ?></span></td><td><?= e($l['severity']) ?></td><td><details><summary><?= e(mb_substr((string)$l['message'],0,90)) ?></summary><pre class="small bg-light p-2 mt-2"><?= e($l['message']) ?>

SQL:
<?= e($l['sql_text'] ?? '') ?>

Stack:
<?= e($l['stack_trace'] ?? '') ?></pre></details></td><td><?= e(basename((string)$l['file_path'])) ?></td><td><?= e($l['line_number']) ?></td><td><?= e($l['created_at']) ?></td></tr><?php endforeach; if(!$logs): ?><tr><td colspan="8" class="text-center text-muted p-4">No error logs found.</td></tr><?php endif; ?></tbody></table></div>
    </form>
  </div>
</div>
<?php include __DIR__ . '/_footer.php'; ?>
