<?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;

$ven_id=$_GET['ven_id'];
$clie_correo_envio=$_GET['correo'];

$query=mysqli_query($con,"SELECT * FROM ventas where ven_id='$ven_id' and empresa='$empresa' ") or die(mysqli_error($con)); 
$roww=mysqli_fetch_array($query);
$ven_nombre=$roww['ven_nombre'];
$ven_prefijo=$roww['ven_prefijo']; 
$ven_n_venta=$roww['ven_n_venta'];
$Cliente_clie_id=$roww['Cliente_clie_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__').'/facturas/factura_venta5.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_envio,
            "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);

$json1=json_encode($json, true);
$book=json_decode($response, true);

echo "<div class='alert alert-info alert-dismissable'<H2>"; echo $message=$book['message']; echo "</div>";

?>