<?php
$authApp = 'autos';
session_start();
require __DIR__ . '/../../config/auth.php';
require __DIR__ . '/../../../../../config/db2.php';
require __DIR__ . '/../../lib/helpers.php';
header('Content-Type: application/json');

$id = (int)($_POST['id'] ?? 0);
if (!$id) { echo json_encode(['error' => 'id requerido']); exit; }

$stmt = $pdo->prepare("SELECT * FROM agj_autos_apertura WHERE id = ? AND deleted_at IS NULL");
$stmt->execute([$id]);
$aa = $stmt->fetch();
if (!$aa) { echo json_encode(['error' => 'no encontrado']); exit; }

$nueva = calcular_prioridad($aa);
$pdo->prepare("UPDATE agj_autos_apertura SET prioridad = ? WHERE id = ?")->execute([$nueva, $id]);

echo json_encode(['prioridad' => $nueva]);
