<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
require_once('../php_api/api.php');


$id=$_GET['id'];
$event=$_GET['event'];

$sql2=mysqli_query($api, "SELECT * FROM received_documents where id='$id'");
$rece=mysqli_fetch_array($sql2);
$cufe=$rece['cufe'];
$prefix=$rece['prefix'];
$number=$rece['number'];

$json = array(
    "event_id" => $event,
    "document_reference" =>  array(
                                "cufe" => $cufe,
                            ),
);

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "{$configuration->base_url}ubl2.1/send-event-data",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>json_encode($json),
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'accept: application/json',
    "Authorization: Bearer {$configuration->token}"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
$response;


$book=json_decode($response, true);
$message=$book['message'];

if($message<>'ApplicationResponse #'.$prefix.$number.' generada con éxito')
{
    echo '<div class="alert alert-info alert-dismissable">'.$message.'</div>';
}
else
{
    $resultado = json_decode($response);
    $StatusDescription=$resultado->ResponseDian->Envelope->Body->SendEventUpdateStatusResponse->SendEventUpdateStatusResult->StatusDescription;

    echo '<div class="alert alert-info alert-dismissable">'.$StatusDescription.'</div>';
}

?>