<?php	
ob_start(); 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

use Dompdf\Dompdf;
use Dompdf\Options;

require_once __DIR__ . "/vendor/autoload.php";

$options = new Options();
$options->set('isRemoteEnabled', true);
$options->set('isHtml5ParserEnabled', true);
$options->set('chroot', __DIR__); 
$dompdf = new Dompdf($options);

	include_once("config/init.php");
	
	if(isset($_POST['submitReport'])){
	    
	    $inicio = $_POST['data_inicio'];
	    $fim = $_POST['data_fim'];
	    $conta_id = $_POST['conta_id'];


   $html6 = '';
			 
	$html6 .= '<table width="100%">';	
	$html6 .= '<thead>';
	$html6 .= '<tr>';
	$html6 .= '<th>#.</th>';
	$html6 .= '<th>Descricao</th>';
	$html6 .= '<th>Entradas</th>';
	$html6 .= '<th>Saidas</th>';
	$html6 .= '<th>Data</th>';
	$html6 .= '<th>Usuario</th>';
	$html6 .= '</tr>';
	$html6 .= '</thead>';
	$html6 .= '<tbody>';
	
	$item=1;
	$estado1 = 'Pago';

	$sqlConta = $database->query("SELECT * FROM contas WHERE id='$conta_id'");
	$resultConta = $database->fetch_assoc($sqlConta);
	
	$result = $database->query("SELECT * FROM transacoes WHERE caixa='$conta_id' AND data BETWEEN '$inicio' AND '$fim'");
	while($row = $database->fetch_assoc($result)){
		$html6 .= '<tr>';
		$html6 .= '<td>'.$item++. "</td>";
		$html6 .= '<td>'.$row['descricao']. "</td>";
		$html6 .= '<td>'.number_format($row['entrada'],2,",",".") . "</td>";
		$html6 .= '<td>'.number_format($row['saida'],2,",",".") . "</td>";
		$html6 .= '<td>'.$row['data']. "</td>";
		$html6 .= '<td>'.$row['user'] . "</td></tr>";
		
		$sum1 = $database->query("SELECT SUM(entrada) AS soma FROM transacoes WHERE caixa='$conta_id' AND data BETWEEN '$inicio' AND '$fim'");
		$dados = $database->fetch_assoc($sum1);
		$somaEntradas = $dados['soma'];
		
		$sum2 = $database->query("SELECT SUM(saida) AS soma FROM transacoes WHERE caixa='$conta_id' AND data BETWEEN '$inicio' AND '$fim'");
		$dados = $database->fetch_assoc($sum2);
		$somaSaidas = $dados['soma'];
				
	}
}

	$html6 .= '</tbody>';

 $html6 .='<tfoot>';
  $html6 .='<tr>';
   $html6 .='<td colspan="2">Totais</td>';
    $html6 .='<td>'.number_format($somaEntradas,2,",",".").'</td>';
    $html6 .='<td>'.number_format($somaSaidas,2,",",".").'</td>';
      $html6 .='</tr>';
       $html6 .='<tfoot>';
   	$html6 .= '</table>';
         
         
  
      
    

		 
		 
	$buscar_empresa = $database->query("SELECT * FROM empresa");
	$associar = $database->fetch_assoc($buscar_empresa);
	$nome_empresa = $associar['nome_empresa'];
	$nuit_empresa = $associar['nuit'];
	$endereco_empresa = $associar['endereco'];
	$email_empresa = $associar['email'];
	$contactos = $associar['contactos'];
	$logo_empresa = $associar['logo'];

	
	
	$html = '
	
	 '.$html6.'


	
';	

	
	
		 
	$buscar_empresa = $database->query("SELECT * FROM empresa");
	$associar = $database->fetch_assoc($buscar_empresa);
	$nome_empresa = $associar['nome_empresa'];
	$nuit_empresa = $associar['nuit'];
	$endereco_empresa = $associar['endereco'];
	$email_empresa = $associar['email'];
	$contactos = $associar['contactos'];
	$logo_empresa = $associar['logo'];
	
	// Carrega seu HTML
	$dompdf->load_html('
	
	
	<style>
 table {
      width: 100%;
      border-collapse: collapse;
      background-color: #fff;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
    }

    thead {
      background-color: #4CAF50;
      color: white;
    }

    th, td {
      padding: 12px;
      text-align: left;
      border-bottom: 1px solid #ddd;
    }

    td:first-child, th:first-child {
      text-align: left;
    }

    tfoot {
      background-color: #f1f1f1;
      font-weight: bold;
      color: #333;
    }

    tfoot td {
      border-top: 2px solid #4CAF50;
    } 

	.jonas{
    position: relative;
    width:100%;
    height:150px;   /* aumentei a altura do cabeçalho */
    margin-bottom:20px;
}

.logo{
    position:absolute;
    left:0;
    top:0;
    width:200px;
}

.logo img{
    width:140px;    /* antes 220px */
    height:auto;
}

.empresa{
    position:absolute;
    right:0;
    top:18px;
    width:360px;
    text-align:right;
    font-size:15px;     /* antes 13px */
    line-height:24px;   /* antes 20px */
}
	</style>
	
	<div class="jonas">
    <div class="logo">
        <img src="'.__DIR__.'/img/'.$logo_empresa.'">
    </div>

    <div class="empresa">
        <strong>'.$nome_empresa.'</strong><br>
        Cell: (+258) '.$contactos.'<br>
        Endereço: '.$endereco_empresa.'<br>
        Email: '.$email_empresa.'<br>
        Maputo - Moçambique
    </div>

</div>
		
		<p>Relatorio da conta '.$resultConta['conta'].' de '.$inicio.' a '.$fim.'</p>
			'. $html .'
	
			
		
			
			
			           

		');
		
	
		

	//Renderizar o html
	$dompdf->render();

	//Exibibir a página
	$dompdf->stream(
		"report.pdf", 
		array(
			"Attachment" => false //Para realizar o download somente alterar para true
		)
	);
	
?>