<?php
session_start();
header('Content-Type: application/json');

if (!isset($_SESSION['rol']) || !in_array($_SESSION['rol'], ['contabilidad', 'adminTi'])) {
  echo json_encode(['success' => false, 'message' => 'Acceso denegado']);
  exit;
}

require_once(__DIR__ . '/../db/conexion.php');

$data = json_decode(file_get_contents('php://input'), true);
$curp = trim($data['curp'] ?? '');
$estatus = trim($data['nuevoEstatus'] ?? '');

if (!$curp || !in_array($estatus, ['asignado', 'en_espera'])) {
  echo json_encode(['success' => false, 'message' => 'Datos inválidos']);
  exit;
}

$stmt = $conn->prepare("UPDATE Plantilla
SET estatusContaAlta = ?, fechaContaAlta = CURDATE()
WHERE CURP = ?");
$stmt->bind_param("ss", $estatus, $curp);

if ($stmt->execute()) {
  echo json_encode(['success' => true]);
} else {
  echo json_encode(['success' => false, 'message' => 'Error al actualizar estatus']);
}
