<?php
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/mj_deal_import_helpers.php';
$admin = require_admin();
mj_deal_import_ensure_schema();
$adminTitle = 'Deal Import Logs';
$logs = [];
try { $logs = all_rows('SELECT l.*, a.name AS admin_name FROM mj_deal_import_logs l LEFT JOIN mj_admin_users a ON a.id=l.admin_id ORDER BY l.id DESC LIMIT 100'); } catch (Throwable $e) { $logs = []; }
include __DIR__ . '/_header.php';
?>
<div class="card card-primary card-outline">
  <div class="card-header d-flex align-items-center"><h3 class="card-title"><i class="fas fa-history mr-2"></i>Deal Import Logs</h3><div class="ml-auto"><a href="deal-import.php" class="btn btn-primary btn-sm"><i class="fas fa-upload mr-1"></i>Import Deals</a></div></div>
  <div class="card-body table-responsive">
    <table class="table table-striped table-hover">
      <thead><tr><th>Date</th><th>Admin</th><th>File</th><th>Total</th><th>Imported</th><th>Updated</th><th>Skipped</th><th>Failed</th><th>Mode</th><th>Errors</th></tr></thead>
      <tbody>
        <?php if(!$logs): ?><tr><td colspan="10" class="text-center text-muted py-4">No import logs found.</td></tr><?php endif; ?>
        <?php foreach($logs as $l): $errs=json_decode((string)($l['error_json']??'[]'),true) ?: []; ?>
        <tr><td><?= e($l['created_at']) ?></td><td><?= e($l['admin_name'] ?? 'Admin') ?></td><td><?= e($l['filename']) ?></td><td><?= (int)$l['total_rows'] ?></td><td><?= (int)$l['imported_rows'] ?></td><td><?= (int)$l['updated_rows'] ?></td><td><?= (int)$l['skipped_rows'] ?></td><td><?= (int)$l['failed_rows'] ?></td><td><?= e($l['default_status'].' / '.$l['duplicate_mode']) ?></td><td><?php if($errs): ?><details><summary><?= count($errs) ?> message(s)</summary><small><?= e(implode("\n", array_slice($errs,0,30))) ?></small></details><?php else: ?><span class="text-muted">-</span><?php endif; ?></td></tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<?php include __DIR__ . '/_footer.php'; ?>
