<?php
// api_poligrafia/gestion_candidato/read_tdocumento.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: GET, OPTIONS");
header("Content-Type: application/json");

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

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

 $sql = "SELECT 
            ID_DOCUMENTO, 
            NOMBRE_DOCUMENTO
        FROM TIPO_DOCUMENTOS
        WHERE ESTADO_CODUMENTO = 1
        ORDER BY NOMBRE_DOCUMENTO ASC";

 $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->execute();
 $result = $stmt->get_result();

 $tipos_documento = [];
if ($result->num_rows > 0) {
    $tipos_documento = $result->fetch_all(MYSQLI_ASSOC);
}

echo json_encode([
    "success" => true,
    "data" => $tipos_documento
]);

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