<?php
require_once __DIR__ . '/../../includes/header.php';
require_once __DIR__ . '/../../mikrotik/includes/bandwidth.php';

$pdo = db();
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$id) { header('Location: ' . NMS_URL . '?page=nms_devices'); exit; }

$devStmt = $pdo->prepare("SELECT * FROM nms_devices WHERE id=? AND user_id=?");
$devStmt->execute([$id, $user['id']]);
$dev = $devStmt->fetch();
if (!$dev) { echo "<p>Equipamento não encontrado.</p>"; require __DIR__.'/../../includes/footer.php'; exit; }

$search = trim($_GET['search'] ?? '');
$stmt = $pdo->prepare("
    SELECT * FROM nms_pppoe_clients
    WHERE device_id = ? AND DATE(snapshot_at) = CURRENT_DATE
    " . ($search ? "AND (username LIKE ? OR address LIKE ? OR caller_id LIKE ?)" : "") . "
    ORDER BY snapshot_at DESC, username ASC
    LIMIT 500
");
if ($search) {
    $like = "%{$search}%";
    $stmt->execute([$id, $like, $like, $like]);
} else {
    $stmt->execute([$id]);
}
$clients = $stmt->fetchAll();
?>

<div style="background:#fff; border-radius:20px; border:1px solid #e2e8f0; overflow:hidden;">
    <div style="padding:24px 32px; border-bottom:1px solid #e2e8f0; background:#f8fafc; display:flex; justify-content:space-between; align-items:center; flex-wrap:wrap; gap:16px;">
        <div>
            <div style="font-size:0.8rem; color:#64748b; margin-bottom:4px;">
                <a href="<?= NMS_URL ?>?page=nms_device_detail&id=<?= $id ?>" style="color:#3b82f6; text-decoration:none; font-weight:600;">← <?= h($dev['name']) ?></a>
            </div>
            <h2 style="margin:0; font-size:1.5rem; font-weight:800; color:#0f172a;">👥 Clientes PPPoE Ativos</h2>
            <p style="margin:4px 0 0; color:#64748b; font-size:0.9rem;"><?= count($clients) ?> cliente<?= count($clients)!==1?'s':'' ?> · Snapshot de hoje</p>
        </div>
    </div>

    <div style="padding:24px 32px;">
        <!-- Search -->
        <div style="position:relative; margin-bottom:20px;">
            <svg width="20" height="20" fill="none" stroke="#94a3b8" viewBox="0 0 24 24" style="position:absolute; left:14px; top:50%; transform:translateY(-50%);"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/></svg>
            <form method="get" action="" style="margin:0;">
                <input type="hidden" name="page" value="nms_pppoe">
                <input type="hidden" name="id" value="<?= $id ?>">
                <input type="text" name="search" value="<?= h($search) ?>" placeholder="Buscar por usuário, IP, MAC..."
                    style="width:100%; padding:14px 16px 14px 44px; border:1px solid #cbd5e1; border-radius:12px; font-size:1rem; font-family:inherit; outline:none;">
            </form>
        </div>

        <?php if (empty($clients)): ?>
            <div style="text-align:center; padding:48px; color:#94a3b8;">
                <div style="font-size:3rem;">👥</div>
                <p style="font-size:1.1rem; font-weight:600; margin-top:12px;">Nenhum cliente PPPoE ativo encontrado<?= $search ? ' para "'.h($search).'"' : '' ?>.</p>
                <p style="font-size:0.85rem;">O polling coleta dados a cada minuto via API RouterOS.</p>
            </div>
        <?php else: ?>
            <div style="overflow-x:auto; border-radius:12px; border:1px solid #e2e8f0;">
                <table style="width:100%; border-collapse:collapse; font-size:0.88rem;">
                    <thead>
                        <tr style="background:#f8fafc;">
                            <th style="padding:12px 16px; text-align:left; font-weight:700; color:#475569; font-size:0.75rem; text-transform:uppercase; border-bottom:2px solid #e2e8f0;">Usuário</th>
                            <th style="padding:12px 16px; text-align:left; font-weight:700; color:#475569; font-size:0.75rem; text-transform:uppercase; border-bottom:2px solid #e2e8f0;">IP Atribuído</th>
                            <th style="padding:12px 16px; text-align:left; font-weight:700; color:#475569; font-size:0.75rem; text-transform:uppercase; border-bottom:2px solid #e2e8f0;">Caller ID / MAC</th>
                            <th style="padding:12px 16px; text-align:center; font-weight:700; color:#475569; font-size:0.75rem; text-transform:uppercase; border-bottom:2px solid #e2e8f0;">Uptime</th>
                            <th style="padding:12px 16px; text-align:center; font-weight:700; color:#475569; font-size:0.75rem; text-transform:uppercase; border-bottom:2px solid #e2e8f0;">↓ RX</th>
                            <th style="padding:12px 16px; text-align:center; font-weight:700; color:#475569; font-size:0.75rem; text-transform:uppercase; border-bottom:2px solid #e2e8f0;">↑ TX</th>
                            <th style="padding:12px 16px; text-align:left; font-weight:700; color:#475569; font-size:0.75rem; text-transform:uppercase; border-bottom:2px solid #e2e8f0;">Interface</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($clients as $c): ?>
                            <tr style="border-bottom:1px solid #f1f5f9; transition:background 0.15s;" onmouseover="this.style.background='#f8fafc'" onmouseout="this.style.background='transparent'">
                                <td style="padding:12px 16px; font-weight:700; color:#0f172a;"><?= h($c['username']) ?></td>
                                <td style="padding:12px 16px;"><code style="background:#f1f5f9; padding:3px 8px; border-radius:5px; font-size:0.82rem;"><?= h($c['address'] ?? '—') ?></code></td>
                                <td style="padding:12px 16px; color:#64748b; font-size:0.82rem; font-family:monospace;"><?= h($c['caller_id'] ?? '—') ?></td>
                                <td style="padding:12px 16px; text-align:center; color:#64748b;"><?= $c['uptime_seconds'] ? format_uptime((int)$c['uptime_seconds']) : '—' ?></td>
                                <td style="padding:12px 16px; text-align:center; font-weight:600; color:#22c55e;">
                                    <?= $c['rx_bytes'] !== null ? round($c['rx_bytes']/1048576, 2).' MB' : '—' ?>
                                </td>
                                <td style="padding:12px 16px; text-align:center; font-weight:600; color:#ef4444;">
                                    <?= $c['tx_bytes'] !== null ? round($c['tx_bytes']/1048576, 2).' MB' : '—' ?>
                                </td>
                                <td style="padding:12px 16px; color:#94a3b8; font-size:0.82rem;"><?= h($c['interface'] ?? '—') ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>
