<?php
// elimina un anexo
session_start();
require '../includes/session_control.php';
require_once __DIR__ . '/../cotizaciones/api/_node.php';
require_once __DIR__ . '/../includes/user_activity_log.php';

$id = intval($_POST['id'] ?? 0);
if (!$id) {
    echo json_encode(['success' => false]);
    exit;
}

header('Content-Type: application/json; charset=utf-8');

$token = cpn_node_token_or_fail();
$url = cpn_node_api_base() . '/api/gastos/anexos/delete';
$resp = cpn_node_request('POST', $url, $token, ['id' => $id]);

if (!($resp['ok'] ?? false)) {
    http_response_code(intval($resp['_http'] ?? 500));
    echo json_encode(['success' => false]);
    exit;
}

cpn_activity_log_event(
    'Gastos',
    'gastos.anexo_delete',
    'Eliminó anexo (id #' . $id . ')',
    'gasto_anexo',
    $id
);

echo json_encode($resp['data'] ?? ['success' => false]);
