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

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

$tabla = array();

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

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/permisoestadoaceptasinc';
$ch = curl_init($url); //create a new cURL resource
$payload = json_encode(array('id_estado' => 2, 'fecha_inicio' => $fc_inicio, 'fecha_termino' => $fc_termino));
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) {

    $documento = $row->pdfacepta;
    $data_string2 = array( 
    'institution' => "CMVA", 
    'code' => $documento, 
    'session_id' => $idsesion, 
    "X-API-KEY" => "ec31e121e83d68f7217a22af8109007155c2518c" 
    );
    $data2 =  processCurlJsonrequestPOST("https://5.dec.cl/api/v1/documents",$data_string2);
    $parametro2 = json_decode($data2);
    if($parametro2  != null){
      foreach ($parametro2->result as $value){

            array_push($tabla, array(
                "pdfacepta" => $row->pdfacepta,
                "idpermiso" => $row->id, 
                "fechainicio" => $row->fecha_inicio,
                "fechatermino" => $row->fecha_termino, 
                "rut" => $row->rut,
                "nombre" => $row->nombre ." ".$row->apellido, 
                "estado" => $estados[$value->state],            		
            )); 

	
            $url2 = 'http://200.24.13.60/~program1/power-time-api/public/modificarestadopermisoacepta';
            $ch2 = curl_init($url2); //create a new cURL resource
            $payload2 = json_encode(array('idacepta' => $value->state, 'id' => $row->id));
            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);		


            if($value->state == 2){

                $url = 'http://200.24.13.60/~program1/power-time-api/public/modificarestadopermiso';
                $ch = curl_init($url); //create a new cURL resource
                $payload = json_encode(array('id_estado' => 5, 'id' => $row->id));
                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);				
            
            }

            if($value->state == 3){

              $url = 'http://200.24.13.60/~program1/power-time-api/public/modificarestadopermiso';
              $ch = curl_init($url); //create a new cURL resource
              $payload = json_encode(array('id_estado' => 3, 'id' => $row->id));
              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);				
          
          }            
        
        }   
    
    
    }	
  }

  echo json_encode($tabla);    
?>