<?php
/**
 * BUSCAR HASH_PEDIDO del pedido #17841233
 */

include 'includes/conexion.php';

echo "<!DOCTYPE html>
<html>
<head>
    <meta charset='UTF-8'>
    <title>Buscar hash_pedido</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 900px; margin: 30px auto; padding: 30px; }
        .section { background: #f8f9fa; padding: 20px; margin: 20px 0; border-radius: 10px; }
        .code { background: #2d3748; color: #e2e8f0; padding: 15px; border-radius: 8px; font-family: monospace; overflow-x: auto; }
        table { width: 100%; border-collapse: collapse; margin: 15px 0; }
        th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }
        th { background: #667eea; color: white; }
        .success { background: #d4edda; color: #155724; padding: 15px; border-radius: 8px; }
        .error { background: #f8d7da; color: #721c24; padding: 15px; border-radius: 8px; }
    </style>
</head>
<body>";

echo "<h1>🔍 Buscando hash_pedido del pedido #17841233</h1>";

// Buscar en tabla pedidos_pagopar
echo "<div class='section'>";
echo "<h2>Tabla: pedidos_pagopar</h2>";

$numero_pedido = "17841233";
$query = "SELECT * FROM pedidos_pagopar WHERE numero_pedido = '$numero_pedido' OR pedido_id LIKE '%17841233%' ORDER BY id DESC LIMIT 5";
$result = mysqli_query($conexion, $query);

if ($result && mysqli_num_rows($result) > 0) {
    echo "<div class='success'>✅ Encontrado en pedidos_pagopar</div>";
    
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<table>";
        foreach ($row as $campo => $valor) {
            echo "<tr><th>$campo</th><td>$valor</td></tr>";
        }
        echo "</table>";
        echo "<hr>";
    }
} else {
    echo "<div class='error'>❌ No encontrado en pedidos_pagopar</div>";
}

echo "</div>";

// Buscar en tabla ventas
echo "<div class='section'>";
echo "<h2>Tabla: ventas</h2>";

$query2 = "SELECT * FROM ventas WHERE pedido_pagopar LIKE '%17841233%' OR id = (SELECT MAX(id) FROM ventas) ORDER BY id DESC LIMIT 5";
$result2 = mysqli_query($conexion, $query2);

if ($result2 && mysqli_num_rows($result2) > 0) {
    echo "<div class='success'>✅ Encontrado en ventas</div>";
    
    while ($row = mysqli_fetch_assoc($result2)) {
        echo "<table>";
        foreach ($row as $campo => $valor) {
            echo "<tr><th>$campo</th><td>" . htmlspecialchars(substr($valor, 0, 200)) . "</td></tr>";
        }
        echo "</table>";
        echo "<hr>";
    }
} else {
    echo "<div class='error'>❌ No encontrado en ventas</div>";
}

echo "</div>";

// Buscar en logs
echo "<div class='section'>";
echo "<h2>Logs de PagoPar</h2>";

$log_file = 'logs/pagopar.log';
if (file_exists($log_file)) {
    $logs = file_get_contents($log_file);
    $lines = explode("\n", $logs);
    $relevant_lines = [];
    
    foreach ($lines as $line) {
        if (strpos($line, '17841233') !== false || strpos($line, 'hash_pedido') !== false) {
            $relevant_lines[] = $line;
        }
    }
    
    if (!empty($relevant_lines)) {
        echo "<div class='success'>✅ Encontrado en logs</div>";
        echo "<div class='code'>";
        echo implode("\n", array_slice($relevant_lines, -20));
        echo "</div>";
    } else {
        echo "<div class='error'>❌ No se encontró #17841233 en logs</div>";
    }
} else {
    echo "<div class='error'>❌ Archivo de log no existe</div>";
}

echo "</div>";

// Mostrar últimas 5 entradas de pedidos_pagopar
echo "<div class='section'>";
echo "<h2>Últimos 5 pedidos en pedidos_pagopar</h2>";

$query3 = "SELECT * FROM pedidos_pagopar ORDER BY id DESC LIMIT 5";
$result3 = mysqli_query($conexion, $query3);

if ($result3 && mysqli_num_rows($result3) > 0) {
    while ($row = mysqli_fetch_assoc($result3)) {
        echo "<table>";
        foreach ($row as $campo => $valor) {
            echo "<tr><th>$campo</th><td>" . htmlspecialchars($valor) . "</td></tr>";
        }
        echo "</table>";
        echo "<hr>";
    }
}

echo "</div>";

echo "</body></html>";
?>
