<?php
/**
 * Teste API Rota 72 - Valores Brutos
 */

header('Content-Type: text/html; charset=utf-8');

echo "<h1>🧪 Teste API Rota 72 - Valores Brutos</h1>";

$rotaId = 72;
$apiUrl = "https://sistemas.pinheiroempresas.com.br/controlefrota/backend-php/api-simples-v2.php?acao=detalhes-rota-completa&id=$rotaId&_t=" . time();

echo "<p>📡 API: $apiUrl</p>";

// Fazer requisição
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Cache-Control: no-cache',
    'Pragma: no-cache'
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "<p>📊 HTTP Code: $httpCode</p>";

if ($response) {
    $data = json_decode($response, true);
    
    if ($data && isset($data['sucesso']) && $data['sucesso']) {
        echo "<p>✅ Sucesso!</p>";
        
        $estatisticas = $data['dados']['estatisticas'];
        
        echo "<h2>📊 Valores Brutos da API</h2>";
        echo "<table border='1' style='border-collapse: collapse; width: 100%;'>";
        echo "<tr style='background: #f0f0f0;'>";
        echo "<th>Campo</th><th>Valor</th><th>Tipo</th>";
        echo "</tr>";
        
        echo "<tr>";
        echo "<td><strong>km_planejado</strong></td>";
        echo "<td style='background: #e8f5e8;'>{$estatisticas['km_planejado']}</td>";
        echo "<td>" . gettype($estatisticas['km_planejado']) . "</td>";
        echo "</tr>";
        
        echo "<tr>";
        echo "<td><strong>km_real</strong></td>";
        echo "<td style='background: #fff3cd;'>{$estatisticas['km_real']}</td>";
        echo "<td>" . gettype($estatisticas['km_real']) . "</td>";
        echo "</tr>";
        
        echo "<tr>";
        echo "<td><strong>km_gps</strong></td>";
        echo "<td style='background: #ffe6e6;'>{$estatisticas['km_gps']}</td>";
        echo "<td>" . gettype($estatisticas['km_gps']) . "</td>";
        echo "</tr>";
        
        echo "<tr>";
        echo "<td><strong>diferenca_km</strong></td>";
        echo "<td style='background: #fff3cd;'>{$estatisticas['diferenca_km']}</td>";
        echo "<td>" . gettype($estatisticas['diferenca_km']) . "</td>";
        echo "</tr>";
        
        echo "<tr>";
        echo "<td><strong>percentual_diferenca</strong></td>";
        echo "<td style='background: #ffe6e6;'>{$estatisticas['percentual_diferenca']}</td>";
        echo "<td>" . gettype($estatisticas['percentual_diferenca']) . "</td>";
        echo "</tr>";
        
        echo "</table>";
        
        echo "<h2>🔍 Análise</h2>";
        $kmGps = $estatisticas['km_gps'];
        echo "<p><strong>km_gps = $kmGps</strong></p>";
        
        if ($kmGps < 100) {
            echo "<p>✅ Valor < 100, tratando como KM: <strong>$kmGps km</strong></p>";
        } else {
            echo "<p>📏 Valor >= 100, tratando como METROS: <strong>" . ($kmGps / 1000) . " km</strong></p>";
        }
        
        echo "<h2>🔄 Próximo Passo</h2>";
        echo "<p>Agora teste o dashboard: <a href='https://sistemas.pinheiroempresas.com.br/controlefrota/backend-php/admin/dashboard.html' target='_blank'>Dashboard</a></p>";
        
    } else {
        echo "<p>❌ Erro na API:</p>";
        echo "<pre>" . htmlspecialchars($response) . "</pre>";
    }
} else {
    echo "<p>❌ Erro ao acessar API</p>";
}
?>
