<?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'] ?? '');

if (!$curp) {
  echo json_encode(['success' => false, 'message' => 'CURP no recibido']);
  exit;
}

$stmt = $conn->prepare("
  UPDATE Plantilla
    SET
    RFCActivo = 0,
    NSSActivo = 0,
    estatusContaBaja = 'concluido',
    fechaContaBaja = CURDATE()
    WHERE CURP = ?
");
$stmt->bind_param("s", $curp);

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