<?php
ob_start();
session_start();
require '../../../conexion.php';
require_once('../../../include/sesiones.php');
require "../../php_qr/qrlib.php";    
require_once('../../../php_electronica/api.php');
require_once dirname(__FILE__).'../../vendor/autoload.php';

use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;

$not_id=$_GET['not_id'];
$query=mysqli_query($con,"SELECT *  FROM dian_nota_credito where not_id='$not_id' and empresa='$empresa' ");
$roww=mysqli_fetch_array($query);

$ven_prefijo=$roww['not_prefijo']; 
$ven_n_venta=$roww['not_numero']; 
//$observaciones=$roww['observaciones'];
$Cliente_clie_id=$roww['cliente_id'];
$dir=mysqli_query($con,"SELECT * FROM cliente where clie_id='$Cliente_clie_id'") or die(mysqli_error($con)); 
$dire=mysqli_fetch_array($dir);
$clie_correo=$dire['clie_correo']; 

try 
{
    ob_start();
    include(dirname('__FILE__').'/nota_debito/nota_debito5.php');
    $content = ob_get_clean();

    $html2pdf = new Html2Pdf('P', 'A4', 'fr', true, 'UTF-8', 0);
    $html2pdf->pdf->SetDisplayMode('fullpage');
    $html2pdf->writeHTML($content);

    $pdfdoc = $html2pdf->Output('', 'S');
    $base64 = chunk_split(base64_encode($pdfdoc));
} 
catch (Html2PdfException $e) 
{
    $html2pdf->clean();
    $formatter = new ExceptionFormatter($e);
    $formatter->getHtmlMessage();
}


$json = array(
            "prefix"                        =>  $ven_prefijo,
            "number"                        =>  $ven_n_venta,
            "alternate_email"               =>  $clie_correo,
            "base64graphicrepresentation"   =>  $base64
);


$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "{$configuration->base_url}ubl2.1/send-email",
  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);
$book=json_decode($response, true);
echo "<div class='alert alert-info alert-dismissable'<H2>"; echo $message=$book['message']; echo "</div>";

?>