<?php
// verificar_log_agora.php
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Monitor de Log da API</title>
    <style>
        body { font-family: monospace; background: #1e1e1e; color: #d4d4d4; padding: 20px; }
        .log-entry { border-bottom: 1px solid #333; padding: 5px; }
        .timestamp { color: #569cd6; }
        .success { color: #6a9955; }
        .error { color: #f44747; }
        button { background: #0e639c; color: white; border: none; padding: 10px 20px; cursor: pointer; margin: 10px 0; }
        button:hover { background: #1177bb; }
    </style>
</head>
<body>
    <h2>📊 Monitor de Log da API</h2>
    <button onclick="atualizarLog()">🔄 Atualizar Log</button>
    <button onclick="limparLog()">🗑️ Limpar Log</button>
    <button onclick="testarAPI()">📞 Testar API Agora</button>
    
    <div id="log-content" style="margin-top: 20px;"></div>
    
    <script>
    function atualizarLog() {
        fetch('ler_log.php')
            .then(response => response.text())
            .then(data => {
                document.getElementById('log-content').innerHTML = '<pre>' + data + '</pre>';
            });
    }
    
    function limparLog() {
        if (confirm('Limpar arquivo de log?')) {
            fetch('limpar_log.php')
                .then(response => response.text())
                .then(data => {
                    alert(data);
                    atualizarLog();
                });
        }
    }
    
    function testarAPI() {
        const dados = {
            numero_destino: '+5511999999999',
            duracao: 30,
            status: 'realizada'
        };
        
        fetch('api_webphone.php?acao=registrar-chamada', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            credentials: 'same-origin',
            body: JSON.stringify(dados)
        })
        .then(response => response.json())
        .then(data => {
            alert('Resposta da API: ' + JSON.stringify(data, null, 2));
            atualizarLog();
        })
        .catch(error => {
            alert('Erro: ' + error);
        });
    }
    
    // Atualizar a cada 3 segundos
    setInterval(atualizarLog, 3000);
    atualizarLog();
    </script>
</body>
</html>