<?php
require_once ('../librerias/dompdf/autoload.inc.php');
use Dompdf\Dompdf;

$data_string = array( 'user_name' => "CMVA", 'user_pin' => "rUWj3LR", "X-API-KEY" => "ec31e121e83d68f7217a22af8109007155c2518c" );
$data =  processCurlJsonrequestPOST("https://5.dec.cl/api/v1/auth/login",$data_string);
$parametro = json_decode($data);
$idsesion = $parametro->session_id;

function processCurlJsonrequestPOST($URL, $fieldString){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
	curl_setopt($ch, CURLOPT_URL, $URL);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
	curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fieldString));
	curl_setopt($ch, CURLOPT_POST, 1);
	$resulta = curl_exec($ch);
	if (curl_errno($ch)) {
		print curl_error($ch);
	} else {
		curl_close($ch);
	}
	return $resulta;
}	


$fecha_inicio = $_GET["txtFecha_inicio"];
$fecha_termino = $_GET["txtFecha_termino"];
$id_departamento = $_GET["txtDepartamento"];
$id_estado = $_GET["txtEstado"];

$url = 'http://200.24.13.60/~program1/power-time-api/public/permisoestadoacepta';
$ch = curl_init($url); //create a new cURL resource
$payload = json_encode(array('id_departamento' => $id_departamento, 'fecha_inicio' => $fecha_inicio, 'fecha_termino' => $fecha_termino, 'id_estado' => $id_estado));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
$result = curl_exec($ch); //execute the POST request
$array = json_decode($result);
		
$html = '
<style>
body{
	font-size:10px;
}
h3{
	font-size:20px;
	display:inline;
}
h4{
	font-size:14px;
	display:inline;	
}
.tbl-marcaje th {
  color: #000000;
  background-color: #CCCCCC;
  border:solid 1px #000000;
}
.tbl-marcaje td {
	border:solid 1px #000000;
}

</style>
<div>
	<img style="max-height:50px;" src="../img/002.png" />
</div> 

<center><h3>Informe Permisos Enviados a ACEPTA</h3></center>
<br /><br />
<h4>Listado de Permisos en Proceso de Firma Electr&oacute;nica</h4>
<br /><br />
<table class="tbl-marcaje" width="100%">
	<tr>
	  <th>FOLIO</th>	
	  <th>CODIGO ACEPTA</th>	
	  <th>&Aacute;REA</th>	
	  <th>CENTRO</th>					  
	  <th>FECHA INICIO</th>				
	  <th>FECHA TERMINO</th>	
	  <th>CANTIDAD DIAS</th>					  
	  <th>ESTADO DE PERMISO</th>					  
	  <th>TIPO DE PERMISO</th>				
	  <th>RUT</th>
	  <th>TRABAJADOR</th>		  
	</tr>
';


$estados = array("PENDIENTE", "EN PROCESO DE FIRMA", "FIRMADO POR TODOS", "RECHAZADO");
foreach ($array[0] as $row) {
  $table = "";
  
	  $table .= "<tr>";	
	  $table .= "<td>". $row->id ."</td>";	
	  $table .= "<td>". $row->pdfacepta ."</td>";	
	  $table .= "<td>". utf8_decode($row->sucursal) ."</td>";	
	  $table .= "<td>". utf8_decode($row->departamento) ."</td>";	
	  $table .= "<td>". date("d/m/Y", strtotime($row->fecha_inicio ))."</td>";		
	  $table .= "<td>". date("d/m/Y", strtotime($row->fecha_termino)) ."</td>";	
	  $table .= "<td>". $row->cantidad ."</td>";	 				
	  $table .= "<td>". $row->estado ."</td>";	
	  $table .= "<td>". utf8_decode($row->tipo) ."</td>";	
	  $table .= "<td>". $row->rut ."</td>";		  
	  $table .= "<td>". utf8_decode($row->nombre) ." ". utf8_decode($row->apellido) ."</td>";	
      $table .= "</tr>";

    $html .= $table;            
  
  
  
}

$html .= ' </table>  ';
$html .='
<br /><br />
<table class="tbl-marcaje" width="30%">
	<tr>
	  <th>CANTIDAD</th>		 		
	</tr>
	<tr>
	  <td style="text-align:center;">'.count($array[0]).'</td>		 		
	</tr>	
</table>  ';	

$pdf = new DOMPDF();
$pdf->set_paper("A4", "landscape");
$pdf->load_html(utf8_encode($html));
$pdf->render();
$pdf->stream("informe.pdf", array("Attachment" => false));
exit(0);

?>
