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

$departamento = "TODAS";
if($_POST["txtDepartamento"] > 0){
	$ch3 = curl_init('http://200.24.13.60/~program1/power-time-api/public/obtenerdepartamento'); //create a new cURL resource
	$payload3 = json_encode(array('id' => $_POST["txtDepartamento"]));
	curl_setopt($ch3, CURLOPT_POSTFIELDS, $payload3);//attach encoded JSON string to the POST fields
	curl_setopt($ch3, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
	curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
	$result3 = curl_exec($ch3); //execute the POST request
	$array3 = json_decode($result3);

	 
	$departamento = $array3[0]->descripcion;			

	curl_close($ch3); 	
}

$sucursal = "TODAS";
if($_POST["txtSucursal"] > 0){
	$ch3 = curl_init('http://200.24.13.60/~program1/power-time-api/public/obtenersucursal'); //create a new cURL resource
	$payload3 = json_encode(array('id' => $_POST["txtSucursal"]));
	curl_setopt($ch3, CURLOPT_POSTFIELDS, $payload3);//attach encoded JSON string to the POST fields
	curl_setopt($ch3, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
	curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
	$result3 = curl_exec($ch3); //execute the POST request
	$array3 = json_decode($result3);

	 
	$sucursal = $array3[0]->descripcion;			

	curl_close($ch3); 	
}

$ch4 = curl_init('http://200.24.13.60/~program1/power-time-api/public/permisoestadodeptosucursal'); //create a new cURL resource
$payload4 = json_encode(array(
'fecha_inicio' => $_POST["txtFecha_inicio"],
'fecha_termino' => $_POST["txtFecha_termino"],
'id_departamento' => $_POST["txtDepartamento"],
'id_sucursal' => $_POST["txtSucursal"],
'id_estado' =>2
));
curl_setopt($ch4, CURLOPT_POSTFIELDS, $payload4);//attach encoded JSON string to the POST fields
curl_setopt($ch4, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
curl_setopt($ch4, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
$result4 = curl_exec($ch4); //execute the POST request
$array4 = json_decode($result4);

$permisoHTML = "";				
foreach ($array4[0] as $row) {
	$permisoHTML .= "<tr>";			
	$permisoHTML .= "<td>". $row->fecha_inicio ."</td>";	
	$permisoHTML .= "<td>". $row->fecha_termino ."</td>";	
	$permisoHTML .= "<td>". $row->total ."</td>";	
	$permisoHTML .= "<td>".STRTOUPPER($row->tipo) ."</td>";	
	$permisoHTML .= "<td>".STRTOUPPER($row->estado) ."</td>";		
	$permisoHTML .= "<td>". $row->rut ."</td>";	
	$permisoHTML .= "<td>". STRTOUPPER($row->nombre . " ". $row->apellido) ."</td>";	
	$permisoHTML .= "<td>".STRTOUPPER($row->sucursal) ."</td>";	
	$permisoHTML .= "<td>".STRTOUPPER($row->departamento) ."</td>";			
	$permisoHTML .= "</tr>";		
}
 		 

curl_close($ch4); 	
			
/***************************************************/
				
$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> 
<table width="100%">
	<tr>
		<td width="70%"><b>COMPA&Ntilde;IA :</b> CORP MUNICIPAL PARA EL DESARROLLO SOCIAL</td>	
	    <td><b>PERIODO DESDE :</b> '. $_POST["txtFecha_inicio"] .'</td>			
	</tr>
	<tr>		
		<td width="70%"><b>RUT :</b> 70.983.600-5</td>	
	<td><b>PERIODO HASTA :</b> '. $_POST["txtFecha_termino"] .'</td>			
	</tr>	
</table><br /> 

		
<center><h3>Reporte Permisos</h3></center>
<br />
<table class="tbl-marcaje" width="100%">
	<tr>
		<th><b>SUCURSAL </b></th>
		<th><b>DEPARTAMENTO </b></th>			
	</tr>
	<tr>
		<td>'.$sucursal.'</td>		
		<td>'.$departamento.'</td>		
	</tr>			
</table>  
<br /> 	
<h4>Listado de Trabajadores</h4>
<br /><br />
<table class="tbl-marcaje" width="100%">
	<tr>
		<th><b>FECHA INICIO </b></th>
		<th><b>FECHA TERMINO </b></th>	
		<th><b>DIAS </b></th>			
		<th><b>TIPO DE PERMISO </b></th>
		<th><b>ESTADO </b></th>	
		<th><b>RUT </b></th>
		<th><b>TRABAJADOR </b></th>	
		<th><b>SUCURSAL </b></th>
		<th><b>DEPARTAMENTO </b></th>			
	</tr>
	'.$permisoHTML.'		
		
</table>  


';

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

?>
