<?php
ini_set('max_execution_time', '0'); 

$tabla = array();

$fc_inicio = $_POST["fechainicio"];
$fc_termino =  $_POST["fechatermino"];
$idacepta = $_POST["idacepta"];
$departamento = $_POST["departamento"]; 

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


$url = 'http://200.24.13.60/~program1/power-time-api/public/listarestadoaprobacionaceptajuridico';
$ch = curl_init($url); //create a new cURL resource
$payload = json_encode(array('idacepta' => $idacepta, 'fecha_inicio' => $fc_inicio, 'fecha_termino' => $fc_termino, 'departamento' => $departamento));
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);


$estados = array("PENDIENTE", "EN PROCESO DE FIRMA", "FIRMADO POR TODOS", "RECHAZADO");

  foreach ($array[0] as $row) {

	  if(empty($row->pdfacepta)) { continue; }
      array_push($tabla, array(
          "pdfacepta" => $row->pdfacepta,
          "idpermiso" => $row->id, 
          "fechainicio" => $row->fecha_inicio,
          "fechatermino" => $row->fecha_termino, 
          "cantidad" => $row->cantidad,
          "tipo" => $row->tipo, 
          "rut" => $row->rut,
          "nombre" => $row->nombre ." ".$row->apellido, 
          "estado" => $estados[$row->idacepta],  
		  "revisado" => $row->revisado          		
      ));    	
  }

  echo json_encode($tabla);    
?>