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

$url = 'http://200.24.13.60/~program1/power-time-api/public/obtenerjustificacionpersonafecha';
$ch = curl_init($url); //create a new cURL resource
$payload = json_encode(array('id_persona' => $_POST["txtId"], 'fecha' => $_POST["txtFecha"]));
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);

$id = $array[0]->id;
$fecha = $array[0]->fecha;
$hora_inicio = $array[0]->hora_inicio;
$hora_termino = $array[0]->hora_termino;	
$id_tipo_justificacion = $array[0]->id_tipo_justificacion;		
$id_tipo_marcaje = $array[0]->id_tipo_marcaje;
$id_persona = $array[0]->id_persona;	
$id_autoriza = $array[0]->id_autoriza;
$observacion = $array[0]->observacion;	
	
curl_close($ch); 

/***************************************************************************/
$url2 = 'http://200.24.13.60/~program1/power-time-api/public/obtenerpersona';
$ch2 = curl_init($url2); //create a new cURL resource
$payload2 = json_encode(array('id' => $id_persona));
curl_setopt($ch2, CURLOPT_POSTFIELDS, $payload2);//attach encoded JSON string to the POST fields
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
$result2 = curl_exec($ch2); //execute the POST request
$array2 = json_decode($result2);

$nombrePersona = $array2[0]->nombre ." ". $array2[0]->apellido;
$rut = $array2[0]->rut;
curl_close($ch2);

/***************************************************************************/	

$url2 = 'http://200.24.13.60/~program1/power-time-api/public/obtenerpersona';
$ch2 = curl_init($url2); //create a new cURL resource
$payload2 = json_encode(array('id' => $id_autoriza));
curl_setopt($ch2, CURLOPT_POSTFIELDS, $payload2);//attach encoded JSON string to the POST fields
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
$result2 = curl_exec($ch2); //execute the POST request
$array2 = json_decode($result2);

$nombreAutoriza = $array2[0]->nombre ." ". $array2[0]->apellido;
$rutAutoriza = $array2[0]->rut;
curl_close($ch2);

/***************************************************************************/	
$ch3 = curl_init('http://200.24.13.60/~program1/power-time-api/public/obtenertipojustificacion'); //create a new cURL resource
$payload3 = json_encode(array('id' => $id_tipo_justificacion));
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);

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

curl_close($ch3);	

/***************************************************************************/	
 
$ch4 = curl_init('http://200.24.13.60/~program1/power-time-api/public/obtenercargoasignado'); //create a new cURL resource
$payload4 = json_encode(array('id_persona' => $id_autoriza));
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);

$cargoAutoriza = $array4[0]->descripcion;			

curl_close($ch4); 	



/***************************************************************************/	

$ch4 = curl_init('http://200.24.13.60/~program1/power-time-api/public/obtenercargoasignado'); //create a new cURL resource
$payload4 = json_encode(array('id_persona' => $id_persona));
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);

$cargo = $array4[0]->descripcion;			

curl_close($ch4); 	

/***********************************************************************/

$ch5 = curl_init('http://200.24.13.60/~program1/power-time-api/public/obtenerdepartamentoprimarioasignado'); //create a new cURL resource
$payload5 = json_encode(array('id_persona' => $id_persona));
curl_setopt($ch5, CURLOPT_POSTFIELDS, $payload5);//attach encoded JSON string to the POST fields
curl_setopt($ch5, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
curl_setopt($ch5, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
$result5 = curl_exec($ch5); //execute the POST request
$array5 = json_decode($result5);

$sucursal = $array5[0][0]->sucursal;	
$departamento = $array5[0][0]->departamento;	

curl_close($ch5); 


 
$html = '
<style>
body{
	font-size:10px;
}
.tbl-trabajador table{
	border:solid 1px #000000;	
}
.tbl-trabajador tr td{
	border:solid 1px #000000;	
}
.hora-permiso table{
	border:solid 1px #000000;	
}
.hora-permiso tr td{
	border:solid 1px #000000;	
}
</style>

    <div>
		<img style="max-height:50px;" src="../img/002.png" />
	</div>
	<div>    
	    <div><center><h4 style="display:inline; font-size:16px;">COMPROBANTE DE JUSTIFICACION<BR /> '.strtoupper($tipojustificacion).'</h4></center></div>
		<br />
		
		<table style="width:100%;">
			<tr>
				<td style="width:15%;"><b>NOMBRE :</b></td>				
				<td> SR(A): '.$nombrePersona.'</td>			
			</tr>	
			<tr>
				<td style="width:15%;"><b>RUT :</b></td>				
				<td>'.$rut.'</td>			
			</tr>				
			<tr>
				<td style="width:15%;"><b>CARGO :</b></td>				
				<td> '.$cargo.'</td>			
			</tr>			
		</table>
		<table style="width:100%;">
			<tr>
				<td style="width:15%;"><b>COMPA&Ntilde;IA :</b> </td>	
				<td>CORP MUNICIPAL PARA EL DESARROLLO SOCIAL 
				</tr>				
			<tr>
				<td style="width:15%;"><b>RUT :</b></td>				
				<td>70.983.600-5</td>
			</tr>			
		</table>			
		<br />
		<table style="width:100%;" class="tbl-trabajador">				
			<tr>
				<td><center>'.$sucursal.'</center></td>	
				<td><center>'.$departamento.'</center></td>					
			</tr>
			<tr>
				<td><center><b>SUCURSAL </b></center></td>	
				<td><center><b>DEPARTAMENTO </b></center></td>				
			</tr>				

			<tr>
				<td colspan="2"><center>'.date("d-m-Y", strtotime($fecha)).'</center></td>		
			</tr>
			<tr>
				<td colspan="2"><center><b>FECHA </b></center></td>			
			</tr>	
			<tr>
				<td colspan="2" style="height:75px;"><center>'.$observacion.'</center></td>			
			</tr>
			<tr>
				<td colspan="2"><center><b>MOTIVO JUSTIFICACION </b></center></td>			
			</tr>			
		</table>
		<br />	<br />	
		<table style="width:100%;">				
			<tr>
				<td  style="width:12%;"><b>AUTORIZA :</b> </td>
				<td>'.$nombreAutoriza.'</td>				
			</tr>
			<tr>
				<td  style="width:12%;"><b>RUT :</b></td>
				<td> '.$rutAutoriza.'</td>				
			</tr>
			<tr>
				<td  style="width:12%;"><b>CARGO :</b> </td>
				<td>'.$cargoAutoriza.'</td>				
			</tr>				
		</table>					
	</div>
';

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