<?php
// api_gold/tipo_documento/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");

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

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

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

if (!isset($data['ID_DOCUMENTO']) || empty($data['ID_DOCUMENTO'])) {
    http_response_code(400);
    echo json_encode(["success" => false, "message" => "El ID del documento es requerido."]);
    exit;
}

 $id = $mysqli->real_escape_string($data['ID_DOCUMENTO']);

 $sql = "DELETE FROM TIPO_DOCUMENTOS WHERE ID_DOCUMENTO = ?";
 $stmt = $mysqli->prepare($sql);

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

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

if ($stmt->execute()) {
    if ($stmt->affected_rows > 0) {
        echo json_encode([
            "success" => true, 
            "message" => "Tipo de documento eliminado exitosamente."
        ]);
    } else {
        http_response_code(404);
        echo json_encode(["success" => false, "message" => "No se encontró el documento con el ID proporcionado."]);
    }
} else {
    http_response_code(500);
    echo json_encode(["success" => false, "message" => "Error al ejecutar la consulta: " . $stmt->error]);
}

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