<?php
/* Renders the <tr> rows for the "Recent visits" table.
   Shared by visitors.php (initial paint) and ProjectController::visitorsAction
   (action=recent, AJAX pagination) so the markup never diverges.
   Expects: $visitors (array), $V (VisitorLog class-string). Self-contained helpers. */
$V = $V ?? \PpPhp\Core\Analytics\VisitorLog::class;
$visitors = $visitors ?? [];
$vhh = static fn($s): string => htmlspecialchars((string) $s, ENT_QUOTES);
$vago = static function (string $ts): string {
    $s = time() - (strtotime($ts) ?: time());
    if ($s < 60) return 'just now';
    if ($s < 3600) return intdiv($s, 60) . 'm ago';
    if ($s < 86400) return intdiv($s, 3600) . 'h ago';
    return intdiv($s, 86400) . 'd ago';
};
$vdur = static function (int $s): string {
    if ($s <= 0) return '—';
    if ($s < 60) return $s . 's';
    if ($s < 3600) return intdiv($s, 60) . 'm ' . ($s % 60) . 's';
    return intdiv($s, 3600) . 'h ' . intdiv($s % 3600, 60) . 'm';
};
$vdev = static fn(string $d): string => ['mobile' => 'bi-phone', 'tablet' => 'bi-tablet', 'desktop' => 'bi-display'][$d] ?? 'bi-question-circle';

if (!$visitors): ?>
    <tr><td colspan="6" class="vs-empty" style="border:0">No visits match your filter.</td></tr>
<?php else: foreach ($visitors as $r): ?>
    <tr data-row data-id="<?= (int) $r['id'] ?>">
        <td>
            <span class="vs-fl"><?= $V::flag((string) $r['country']) ?></span>
            <span class="vs-where">
                <span class="nm"><?= $vhh((string) ($r['country_name'] ?: ($r['country'] ?: 'Unknown'))) ?></span>
                <?php $loc = trim(implode(', ', array_filter([(string) ($r['city'] ?? ''), (string) ($r['region'] ?? '')]))); ?>
                <?php if ($loc !== ''): ?><span class="sub"><?= $vhh($loc) ?></span><?php endif; ?>
            </span>
        </td>
        <td>
            <code><?= $vhh((string) ($r['ip'] ?: '—')) ?></code>
            <?php if (!empty($r['username'])): ?><span class="vs-tag user"><?= $vhh((string) $r['username']) ?></span><?php endif; ?>
            <?php if ((int) ($r['is_return'] ?? 0) === 1): ?><span class="vs-tag ret">returning</span><?php endif; ?>
            <?php if ((int) $r['is_bot'] === 1): ?><span class="vs-tag bot">bot</span><?php endif; ?>
            <?php if (!empty($r['referrer_domain'])): ?><div class="sub"><?= $vhh((string) ($r['referrer_type'] ?: 'from')) ?> · <?= $vhh((string) $r['referrer_domain']) ?></div><?php endif; ?>
        </td>
        <td>
            <span class="vs-devline"><i class="bi <?= $vdev((string) $r['device']) ?>"></i> <?= $vhh(trim((string) $r['browser'] . ' ' . (string) ($r['browser_version'] ?? ''))) ?></span>
            <div class="sub"><?= $vhh(trim((string) $r['platform'] . ' ' . (string) ($r['platform_version'] ?? ''))) ?><?= !empty($r['device_model']) ? ' · ' . $vhh((string) $r['device_model']) : '' ?></div>
        </td>
        <td>
            <strong><?= (int) $r['page_views'] ?></strong> page<?= (int) $r['page_views'] === 1 ? '' : 's' ?>
            <div class="sub"><?= $vdur((int) ($r['active_seconds'] ?? 0)) ?><?= !empty($r['timezone']) ? ' · ' . $vhh((string) $r['timezone']) : '' ?></div>
        </td>
        <td>
            <?= $vhh($vago((string) $r['last_seen'])) ?>
            <div class="sub"><?= $vhh(date('j M, H:i', strtotime((string) $r['last_seen']) ?: time())) ?></div>
        </td>
        <td style="text-align:right">
            <button class="vs-del" data-del="<?= (int) $r['id'] ?>" title="Delete this row"><i class="bi bi-trash"></i></button>
        </td>
    </tr>
<?php endforeach; endif; ?>
