<?php
session_start();
include '../includes/session_control.php';
header('Content-Type: application/json; charset=utf-8');

require_once __DIR__ . '/../cotizaciones/api/_node.php';
require_once __DIR__ . '/../includes/user_activity_log.php';

$idG  = intval($_POST['idGasto']);
$file = basename($_POST['file']);
$path = __DIR__ . "/../archivoDigital/soporteGastos/$idG/$file";
if (file_exists($path)) unlink($path);

$total = count(glob(dirname($path) . '/*'));

$token = cpn_node_token_or_fail();
$url = cpn_node_api_base() . '/api/gastos/soportes/set-count';
cpn_node_request('POST', $url, $token, ['idGasto' => $idG, 'archivos' => $total]);

cpn_activity_log_event(
    'Gastos',
    'gastos.soporte_delete',
    'Eliminó soporte del gasto #' . $idG,
    'gasto',
    $idG,
    ['file' => $file, 'archivosTotal' => $total]
);

echo json_encode(['success' => true]);
