<?php
// ── 1. device_detail.php — busca nas interfaces ────────────────────────────
$f1 = "/var/www/html/mikrotik/views/device_detail.php";
$c1 = file_get_contents($f1);

// 1a. Adiciona input de busca no header da secao de interfaces
$old1a = '        <div style="padding:20px 24px; border-bottom:1px solid #e2e8f0; background:#f8fafc; display:flex; justify-content:space-between; align-items:center;">
            <h3 style="margin:0; font-size:1.1rem; font-weight:800; color:#0f172a;">🔌 Interfaces (<?= count($interfaces) ?>)</h3>
            <?php if ($pppoeCount > 0): ?>
                <a href="<?= NMS_URL ?>?page=nms_pppoe&id=<?= $id ?>" style="font-size:0.95rem; color:#8b5cf6; font-weight:700; text-decoration:none;">Ver PPPoE ativos (<?= $pppoeCount ?>) →</a>
            <?php endif; ?>
        </div>';

$new1a = '        <div style="padding:20px 24px; border-bottom:1px solid #e2e8f0; background:#f8fafc;">
            <div style="display:flex; justify-content:space-between; align-items:center; flex-wrap:wrap; gap:12px;">
                <h3 style="margin:0; font-size:1.1rem; font-weight:800; color:#0f172a;">🔌 Interfaces (<span id="iface-count"><?= count($interfaces) ?></span>/<span id="iface-total"><?= count($interfaces) ?></span>)</h3>
                <?php if ($pppoeCount > 0): ?>
                    <a href="<?= NMS_URL ?>?page=nms_pppoe&id=<?= $id ?>" style="font-size:0.95rem; color:#8b5cf6; font-weight:700; text-decoration:none;">Ver PPPoE ativos (<?= $pppoeCount ?>) →</a>
                <?php endif; ?>
            </div>
            <div style="position:relative; margin-top:14px;">
                <svg width="18" height="18" fill="none" stroke="#94a3b8" viewBox="0 0 24 24" style="position:absolute;left:13px;top:50%;transform:translateY(-50%);pointer-events:none;"><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>
                <input type="text" id="iface-search" placeholder="Buscar interface por nome ou descrição..."
                    oninput="filterIfaces(this.value)"
                    style="width:100%;padding:11px 16px 11px 42px;border:1px solid #cbd5e1;border-radius:10px;font-size:0.95rem;font-family:inherit;outline:none;box-sizing:border-box;transition:border-color .15s;"
                    onfocus="this.style.borderColor=\'#6366f1\'" onblur="this.style.borderColor=\'#cbd5e1\'">
            </div>
        </div>';

// 1b. Adiciona data-search e data-iface no <tr> principal de cada interface
$old1b = '                        <?php foreach ($interfaces as $iface): ?>
                            <tr style="border-bottom:1px solid #f1f5f9; transition:background 0.15s;" onmouseover="this.style.background=\'#f8fafc\'" onmouseout="this.style.background=\'transparent\'">';

$new1b = '                        <?php foreach ($interfaces as $iface): ?>
                            <tr data-iface-id="<?= (int)$iface[\'id\'] ?>" data-search="<?= strtolower(h($iface[\'if_name\'])) ?> <?= strtolower(h($iface[\'if_alias\'] ?? \'\')) ?>"
                                style="border-bottom:1px solid #f1f5f9; transition:background 0.15s;" onmouseover="this.style.background=\'#f8fafc\'" onmouseout="this.style.background=\'transparent\'">';

// 1c. Adiciona a funcao JS de filtro antes do </script> final
$old1c = 'function toggleBwChart(ifaceId) {';
$new1c = 'function filterIfaces(q) {
    q = q.toLowerCase().trim();
    const tbody = document.querySelector(\'#iface-table-body\');
    if (!tbody) return;
    let shown = 0, total = 0;
    tbody.querySelectorAll(\'tr[data-iface-id]\').forEach(tr => {
        total++;
        const id = tr.dataset.ifaceId;
        const match = !q || tr.dataset.search.includes(q);
        tr.style.display = match ? \'\' : \'none\';
        // Esconde tambem os rows expandiveis
        const alertRow = document.getElementById(\'alertrow-\' + id);
        const bwRow    = document.getElementById(\'bwrow-\' + id);
        if (alertRow) alertRow.style.display = \'none\';
        if (bwRow)    bwRow.style.display    = \'none\';
        if (match) shown++;
    });
    const el = document.getElementById(\'iface-count\');
    if (el) el.textContent = shown;
}

function toggleBwChart(ifaceId) {';

// 1d. Adiciona id ao tbody
$old1d = '                    <tbody>';
$new1d = '                    <tbody id="iface-table-body">';

$c1 = str_replace($old1a, $new1a, $c1);
$c1 = str_replace($old1b, $new1b, $c1);
$c1 = str_replace($old1c, $new1c, $c1);
$c1 = str_replace($old1d, $new1d, $c1);
file_put_contents($f1, $c1);
echo "OK device_detail.php\n";

// ── 2. pppoe_clients.php — busca instantanea JS ────────────────────────────
$f2 = "/var/www/html/mikrotik/views/pppoe_clients.php";
$c2 = file_get_contents($f2);

// 2a. Converte o input para live search
$old2a = '                <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;">';

$new2a = '                <input type="text" id="pppoe-search" name="search" value="<?= h($search) ?>" placeholder="Buscar por usuário, IP, MAC..."
                    oninput="filterPppoe(this.value)"
                    style="width:100%; padding:14px 16px 14px 44px; border:1px solid #cbd5e1; border-radius:12px; font-size:1rem; font-family:inherit; outline:none; transition:border-color .15s;"
                    onfocus="this.style.borderColor=\'#6366f1\'" onblur="this.style.borderColor=\'#cbd5e1\'">';

// 2b. Adiciona data-search a cada tr de cliente
$old2b = '                        <?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\'">';

$new2b = '                        <?php foreach ($clients as $c): ?>
                            <tr data-search="<?= strtolower(h($c[\'username\'])) ?> <?= strtolower(h($c[\'address\'] ?? \'\')) ?> <?= strtolower(h($c[\'caller_id\'] ?? \'\')) ?> <?= strtolower(h($c[\'interface\'] ?? \'\')) ?>"
                                style="border-bottom:1px solid #f1f5f9; transition:background 0.15s;" onmouseover="this.style.background=\'#f8fafc\'" onmouseout="this.style.background=\'transparent\'">';

// 2c. Adiciona id ao tbody e contador
$old2c = '                    <tbody>';
$new2c = '                    <tbody id="pppoe-tbody">';

// 2d. Adiciona contador ao titulo
$old2d = '            <p style="margin:4px 0 0; color:#64748b; font-size:0.9rem;"><?= count($clients) ?> cliente<?= count($clients)!==1?\'s\':\'\' ?> · Snapshot de hoje</p>';
$new2d = '            <p style="margin:4px 0 0; color:#64748b; font-size:0.9rem;"><span id="pppoe-shown"><?= count($clients) ?></span>/<?= count($clients) ?> cliente<?= count($clients)!==1?\'s\':\'\' ?> · Snapshot de hoje</p>';

// 2e. Adiciona JS antes do footer
$old2e = '<?php require_once __DIR__ . \'/../../includes/footer.php\'; ?>';
$new2e = '<script>
function filterPppoe(q) {
    q = q.toLowerCase().trim();
    const tbody = document.getElementById(\'pppoe-tbody\');
    if (!tbody) return;
    let shown = 0;
    tbody.querySelectorAll(\'tr[data-search]\').forEach(tr => {
        const match = !q || tr.dataset.search.includes(q);
        tr.style.display = match ? \'\' : \'none\';
        if (match) shown++;
    });
    const el = document.getElementById(\'pppoe-shown\');
    if (el) el.textContent = shown;
}
// Inicializa com o valor atual (caso carregou com ?search=)
document.addEventListener(\'DOMContentLoaded\', function() {
    const input = document.getElementById(\'pppoe-search\');
    if (input && input.value) filterPppoe(input.value);
    if (input) input.focus();
});
</script>
<?php require_once __DIR__ . \'/../../includes/footer.php\'; ?>';

$c2 = str_replace($old2a, $new2a, $c2);
$c2 = str_replace($old2b, $new2b, $c2);
$c2 = str_replace($old2c, $new2c, $c2);
$c2 = str_replace($old2d, $new2d, $c2);
$c2 = str_replace($old2e, $new2e, $c2);
file_put_contents($f2, $c2);
echo "OK pppoe_clients.php\n";

echo "DONE\n";
