diff --git a/api.php b/api.php index 1f627bb..642adda 100644 --- a/api.php +++ b/api.php @@ -2,14 +2,40 @@ header('Content-Type: application/json'); require_once 'db/config.php'; +$action = $_GET['action'] ?? 'get_current_locations'; + try { $pdo = db(); - $stmt = $pdo->query("SELECT t.matricula, t.modelo, lt.latitud, lt.longitud, lt.ultima_actualizacion - FROM localizacion_taxis lt - JOIN taxis t ON lt.id_taxi = t.id - ORDER BY lt.ultima_actualizacion DESC"); - $localizaciones = $stmt->fetchAll(PDO::FETCH_ASSOC); - echo json_encode($localizaciones); + + switch ($action) { + case 'get_route_history': + $id_taxi = $_GET['id_taxi'] ?? null; + if (!$id_taxi) { + http_response_code(400); + echo json_encode(['error' => 'Se requiere id_taxi']); + exit; + } + + $stmt = $pdo->prepare( + "SELECT latitud, longitud FROM localizacion_historico WHERE id_taxi = ? ORDER BY fecha_registro ASC" + ); + $stmt->execute([$id_taxi]); + $history = $stmt->fetchAll(PDO::FETCH_ASSOC); + echo json_encode($history); + break; + + case 'get_current_locations': + default: + $stmt = $pdo->query( + "SELECT t.matricula, t.modelo, lt.latitud, lt.longitud, lt.ultima_actualizacion " . + "FROM localizacion_taxis lt " . + "JOIN taxis t ON lt.id_taxi = t.id " . + "ORDER BY lt.ultima_actualizacion DESC" + ); + $locations = $stmt->fetchAll(PDO::FETCH_ASSOC); + echo json_encode($locations); + break; + } } catch (PDOException $e) { http_response_code(500); echo json_encode(['error' => 'Error de base de datos: ' . $e->getMessage()]); diff --git a/localizacion.php b/localizacion.php index 2c0df26..3311707 100644 --- a/localizacion.php +++ b/localizacion.php @@ -16,13 +16,23 @@ try { // Create localizacion_taxis table if it doesn't exist $pdo->exec("CREATE TABLE IF NOT EXISTS localizacion_taxis ( id INT AUTO_INCREMENT PRIMARY KEY, - id_taxi INT NOT NULL, + id_taxi INT NOT NULL UNIQUE, -- Un taxi solo puede tener una última ubicación latitud DECIMAL(10, 8) NOT NULL, longitud DECIMAL(11, 8) NOT NULL, ultima_actualizacion TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (id_taxi) REFERENCES taxis(id) )"); + // Create location history table + $pdo->exec("CREATE TABLE IF NOT EXISTS localizacion_historico ( + id INT AUTO_INCREMENT PRIMARY KEY, + id_taxi INT NOT NULL, + latitud DECIMAL(10, 8) NOT NULL, + longitud DECIMAL(11, 8) NOT NULL, + fecha_registro TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (id_taxi) REFERENCES taxis(id) + )"); + // Handle form submission to add a new taxi for simplicity if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_taxi'])) { $matricula = trim($_POST['matricula']); @@ -40,21 +50,25 @@ try { $longitud = $_POST['longitud']; if (!empty($id_taxi) && is_numeric($latitud) && is_numeric($longitud)) { - // Check if a location for this taxi already exists - $stmt_check = $pdo->prepare("SELECT id FROM localizacion_taxis WHERE id_taxi = ?"); - $stmt_check->execute([$id_taxi]); - $existing_location = $stmt_check->fetch(); + $pdo->beginTransaction(); + try { + // Upsert (Update or Insert) the last known location + $stmt_upsert = $pdo->prepare( + "INSERT INTO localizacion_taxis (id_taxi, latitud, longitud) VALUES (?, ?, ?) " . + "ON DUPLICATE KEY UPDATE latitud = VALUES(latitud), longitud = VALUES(longitud)" + ); + $stmt_upsert->execute([$id_taxi, $latitud, $longitud]); - if ($existing_location) { - // Update existing location - $stmt = $pdo->prepare("UPDATE localizacion_taxis SET latitud = ?, longitud = ? WHERE id_taxi = ?"); - $stmt->execute([$latitud, $longitud, $id_taxi]); - } else { - // Insert new location - $stmt = $pdo->prepare("INSERT INTO localizacion_taxis (id_taxi, latitud, longitud) VALUES (?, ?, ?)"); - $stmt->execute([$id_taxi, $latitud, $longitud]); + // Insert into history + $stmt_history = $pdo->prepare("INSERT INTO localizacion_historico (id_taxi, latitud, longitud) VALUES (?, ?, ?)"); + $stmt_history->execute([$id_taxi, $latitud, $longitud]); + + $pdo->commit(); + echo '