<?php
// referencias_laborales/delete.php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Content-Type: application/json; charset=utf-8");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
  http_response_code(200);
  exit;
}

require_once(__DIR__ . '/../config.php'); // $mysqli

$raw  = file_get_contents("php://input");
$data = json_decode($raw, true);

if (!$data) {
  http_response_code(400);
  echo json_encode(["success" => false, "message" => "JSON inválido o vacío.", "raw" => $raw]);
  exit;
}

$ID = isset($data['ID_REFERENCIA_LABORAL']) ? (int)$data['ID_REFERENCIA_LABORAL'] : null;
if (!$ID) {
  http_response_code(400);
  echo json_encode(["success" => false, "message" => "ID_REFERENCIA_LABORAL es obligatorio."]);
  exit;
}

$sql = "UPDATE REFERENCIAS_LABORALES
        SET ESTADO_REGISTRO = 0
        WHERE ID_REFERENCIA_LABORAL = ?";

$stmt = $mysqli->prepare($sql);
if ($stmt === false) {
  http_response_code(500);
  echo json_encode(["success" => false, "message" => "Error prepare: " . $mysqli->error]);
  exit;
}

$stmt->bind_param("i", $ID);

if ($stmt->execute()) {
  echo json_encode(["success" => true, "message" => "Referencia laboral eliminada (lógica) correctamente."]);
} else {
  http_response_code(500);
  echo json_encode(["success" => false, "message" => "Error execute: " . $stmt->error]);
}

$stmt->close();
$mysqli->close();
