<?php
require_once '../config/conexion.php';

/* Cabeceras anti-cach¨¦ + JSON */
header("Content-Type: application/json; charset=utf-8");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Pragma: no-cache");
header("Expires: 0");

/* Zona horaria y charset */
date_default_timezone_set('America/Lima');
if (isset($conexion) && $conexion instanceof mysqli) {
  @mysqli_set_charset($conexion, 'utf8');
  @mysqli_query($conexion, "SET time_zone = '-05:00'");
}

if (isset($_GET['accion']) && $_GET['accion'] === 'obtener') {
  $sql = "SELECT * FROM trabajadores ORDER BY id DESC";
  $res = $conexion->query($sql);
  $data = [];
  while ($row = $res->fetch_assoc()) { $data[] = $row; }
  echo json_encode($data, JSON_UNESCAPED_UNICODE);
  exit;
}

echo json_encode(['success' => false, 'message' => 'Acci¨®n no v¨¢lida'], JSON_UNESCAPED_UNICODE);
exit;
