File "equiposColaborador.php"

Full Path: C:/wamp64/www/RegistroEquipos2/backend/equiposColaborador.php
File size: 2.36 KB
MIME-type: text/x-php
Charset: utf-8

<?php
require __DIR__ . '/../bd/conexion.php';
header('Content-Type: application/json; charset=utf-8');

if (!isset($_GET['buscar'])) {
    echo json_encode(['error' => 'Falta parámetro buscar']);
    exit;
}

$buscar = trim($_GET['buscar']);
error_log("🟢 Script iniciado\n🔍 Valor recibido: $buscar\n", 3, __DIR__ . '/debug_equipos.txt');

try {
    // --- Buscar en registrocomputers (EXCLUYENDO DEVUELTOS) ---
    $stmt1 = $conexion->prepare("
        SELECT id, marca, referencia, tipo, serial, estado
        FROM registrocomputers 
        WHERE REPLACE(REPLACE(TRIM(cedula), '>', ''), '<', '') = ?
        AND estado != 'Devuelto'
    ");
    $stmt1->bind_param("s", $buscar);
    $stmt1->execute();
    $result1 = $stmt1->get_result();
    $datosComputers = $result1->fetch_all(MYSQLI_ASSOC);

    // --- Buscar en registrosmartphones (EXCLUYENDO DEVUELTOS) ---
    $stmt2 = $conexion->prepare("
        SELECT id, marca, referencia, color, estado
        FROM registrosmartphones 
        WHERE REPLACE(REPLACE(TRIM(cedula), '>', ''), '<', '') = ?
        AND estado != 'Devuelto'
    ");
    $stmt2->bind_param("s", $buscar);
    $stmt2->execute();
    $result2 = $stmt2->get_result();
    $datosSmartphones = $result2->fetch_all(MYSQLI_ASSOC);

    // --- Combinar resultados ---
    $datosTotales = [];

    foreach ($datosComputers as $comp) {
        $datosTotales[] = [
            'id' => $comp['id'],
            'marca' => $comp['marca'],
            'referencia' => $comp['referencia'],
            'tipo' => $comp['tipo'],
            'detalle' => $comp['serial'] ?? '',
            'origen' => 'Computer',
            'estado' => $comp['estado'] ?? ''
        ];
    }

    foreach ($datosSmartphones as $movil) {
        $datosTotales[] = [
            'id' => $movil['id'],
            'marca' => $movil['marca'],
            'referencia' => $movil['referencia'],
            'tipo' => 'Smartphone',
            'detalle' => $movil['color'] ?? '',
            'origen' => 'Smartphone',
            'estado' => $movil['estado'] ?? ''
        ];
    }

    error_log("✅ Registros encontrados: " . count($datosTotales) . "\n", 3, __DIR__ . '/debug_equipos.txt');

    echo json_encode(['datos' => $datosTotales], JSON_UNESCAPED_UNICODE);
} catch (Exception $e) {
    echo json_encode(['error' => $e->getMessage()]);
}