<?php
require('../librerias/PHPExcel/Classes/PHPExcel.php');

$filename = $_FILES['file']['name'];
$archivo = $_FILES['file']['tmp_name'];

$inputFileType = PHPExcel_IOFactory::identify($archivo);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($archivo);
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();

for ($row = 1; $row <= $highestRow; $row++){ 

	$Fecha_excel = $sheet->getCell('B'.$row);
	$Conversion = $Fecha_excel->getValue();
	if(PHPExcel_Shared_Date::isDateTime($Fecha_excel)){
	  $Conversion = date($format = "Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($Conversion));
	}
	
	$Fecha_excel2 = $sheet->getCell('C'.$row);
	$Conversion2 = $Fecha_excel2->getValue();
	if(PHPExcel_Shared_Date::isDateTime($Fecha_excel2)){
	  $Conversion2 = date($format = "Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($Conversion2));
	}	
	
	$Fecha_excel3 = $sheet->getCell('D'.$row);
	$Conversion3 = $Fecha_excel3->getValue();
	if(PHPExcel_Shared_Date::isDateTime($Fecha_excel3)){
	  $Conversion3 = date($format = "H:i:s", PHPExcel_Shared_Date::ExcelToPHP($Conversion3));
	}	

	$Fecha_excel4 = $sheet->getCell('E'.$row);
	$Conversion4 = $Fecha_excel4->getValue();
	if(PHPExcel_Shared_Date::isDateTime($Fecha_excel4)){
	  $Conversion4 = date($format = "H:i:s", PHPExcel_Shared_Date::ExcelToPHP($Conversion4));
	}

	$ch = curl_init('http://200.24.13.60/~program1/power-time-api/public/obtenerpersonarut'); //create a new cURL resource
	$payload = json_encode(array('rut' => $sheet->getCell("A".$row)->getValue()));
	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;
	$rut = $array[0]->rut;	
	$nombre = $array[0]->nombre;		

	$vetor[] = array(
		'rut' => $sheet->getCell("A".$row)->getValue(),
		'nombre' => $nombre,	
		'id' => $id,			
		'fecha_inicio' => $Conversion,		
		'fecha_termino' => $Conversion2,	
		'hora_inicio' => $Conversion3, 		
		'hora_termino' => $Conversion4,			
	); 		
	
}
echo json_encode($vetor);


?>