<?php
$f = "/var/www/html/mikrotik/views/dashboard.php";
$c = file_get_contents($f);

if (strpos($c, 'Entrada</span>') !== false) { echo "SKIP\n"; exit; }

$old = '            <!-- Mini gr' . "\xc3\xa1" . 'fico -->
            <div style="padding:12px 20px 8px;position:relative;height:90px;">
                <div id="loading-<?= $chartId ?>" style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:0.8rem;">Carregando...</div>
                <canvas id="<?= $chartId ?>" style="display:none;"></canvas>
            </div>';

// tenta sem acento
if (strpos($c, $old) === false) {
    $old = '            <!-- Mini grafico -->
            <div style="padding:12px 20px 8px;position:relative;height:90px;">
                <div id="loading-<?= $chartId ?>" style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:0.8rem;">Carregando...</div>
                <canvas id="<?= $chartId ?>" style="display:none;"></canvas>
            </div>';
}

// tenta busca parcial pelo height:90px
if (strpos($c, $old) === false) {
    $old = 'height:90px;">
                <div id="loading-<?= $chartId ?>" style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:0.8rem;">Carregando...</div>
                <canvas id="<?= $chartId ?>" style="display:none;"></canvas>
            </div>';
    $new = 'height:100px;">
                    <div id="loading-<?= $chartId ?>" style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:0.8rem;">Carregando...</div>
                    <canvas id="<?= $chartId ?>" style="display:none;"></canvas>
                </div>
            </div>';
    if (strpos($c, $old) !== false) {
        // Precisa tambem adicionar o wrapper com legenda
        // Busca o inicio da div do mini grafico
        $old2 = '<!-- Mini gr';
        $pos = strrpos($c, $old2);
        if ($pos === false) $old2 = '<!-- Mini grafico';
        $pos = strrpos($c, $old2);
        if ($pos !== false) {
            // Encontra o inicio da div seguinte
            $divStart = strpos($c, '<div style="padding:12px 20px 8px', $pos);
            if ($divStart !== false) {
                $divEnd = strpos($c, '</div>', $divStart) + 6;
                $oldBlock = substr($c, $pos, $divEnd - $pos);
                $newBlock = '<!-- Mini gr' . "\xc3\xa1" . 'fico -->
            <div style="padding:10px 20px 0;">
                <div style="display:flex;gap:14px;margin-bottom:4px;">
                    <span style="font-size:0.7rem;font-weight:700;color:#22c55e;">&#9679; &#8595; Entrada</span>
                    <span style="font-size:0.7rem;font-weight:700;color:#ef4444;">&#9679; &#8593; Sa' . "\xc3\xad" . 'da</span>
                </div>
                <div style="position:relative;height:100px;">
                    <div id="loading-<?= $chartId ?>" style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:0.8rem;">Carregando...</div>
                    <canvas id="<?= $chartId ?>" style="display:none;"></canvas>
                </div>
            </div>';
                $c = str_replace($oldBlock, $newBlock, $c);
                file_put_contents($f, $c);
                echo "OK (partial)\n";
                exit;
            }
        }
    }
    echo "FAIL\n"; exit;
}

$new = '            <!-- Mini gr' . "\xc3\xa1" . 'fico -->
            <div style="padding:10px 20px 0;">
                <div style="display:flex;gap:14px;margin-bottom:4px;">
                    <span style="font-size:0.7rem;font-weight:700;color:#22c55e;">&#9679; &#8595; Entrada</span>
                    <span style="font-size:0.7rem;font-weight:700;color:#ef4444;">&#9679; &#8593; Sa' . "\xc3\xad" . 'da</span>
                </div>
                <div style="position:relative;height:100px;">
                    <div id="loading-<?= $chartId ?>" style="position:absolute;inset:0;display:flex;align-items:center;justify-content:center;color:#94a3b8;font-size:0.8rem;">Carregando...</div>
                    <canvas id="<?= $chartId ?>" style="display:none;"></canvas>
                </div>
            </div>';

$c = str_replace($old, $new, $c);
file_put_contents($f, $c);
echo "OK\n";
