<?php

session_start();
////
/// Camptura de inventario
///

//$file = fopen("log.txt", "w");   /// BORRAR LOG
//fwrite($file, "Error $error". PHP_EOL);
//fclose($file);


header("Content-Type: roi/json");

$ras=$_GET["ras"];

//------------------------------------------------ Prepara la primera pantalla -----------------------------------------------------------------------------------------
$view='jsonfiles/roi.json';

$data1=array();
$output = array();

$config = require ('config.php');

$data1['url']= $config['URL'];
$data1['ras']= $ras;
$data1['extjs']='jscss/roi.js?'.trim(date_timestamp_get(date_create()));
$conn=obtener_conexion($ras);


function obtener_conexion($rampa){
	$conn_string = "host=localhost port=5434 dbname=VISUAL_CHECK user=VISUAL_CHECK password=Tresco_29";
	$db = pg_connect($conn_string);
	return $db;
}
if (isset($_POST["zonsel"])) {
	$data1['zonsel']=$_POST["zonsel"];
	$zonsel=$_POST["zonsel"];
} else {
	$zonsel="-";
}

cargadatos($conn,$ras,$zonsel);

$output = array('data' => $data1, 'view' => $view, 'screen' => 'fmt01');

utf8_encode_deep($output);
echo  json_encode($output);
pg_close($conn);


//
//------------------------ Carga ultimas peticiones
//

function cargadatos($conn,$ras,$zonsel) {


global $data1;
$grid1 = array();



// Sensibilidad
$sens=0;
$zlanza="";
$sql="select * from parametrizacion";
$results = pg_query($conn,$sql);
while ($row = pg_fetch_array($results)) {
	$existe='S';
	$sens=$row['umbral'];
}
$data1['sens']=$sens;
$data1['zlanza']=$zlanza;


if ($zonsel=='-') {
	$sql="select * from zonas where ras='$ras' order by zona";
} else {
	$sql="select * from zonas where ras='$ras' and zona >= '$zonsel' order by zona";
}


$results = pg_query($conn,$sql);
while ($row = pg_fetch_array($results)) {

	$vroi='1';
	if ($row['x']==="") {
		$vroi='0';
	}
	$iexclu='0';
	$icamino='0';
	array_push($grid1, array(
		'zona2' => $row['zona'],
		'a1' => $row['x'],
		'bcde' => $row['x_roi'].','.$row['x_roi2'].','.$row['x_roi3'].','.$row['x_roi4'],
		'f1' => $row['x_baliza'],
		'iexclu' => $iexclu,
		'icamino' => $icamino,

		
		'intentos2' => $row['mili_seg_togo']));
}

$data1['grid1'] = $grid1;


$conn=null;

}

function utf8_encode_deep(&$input) {

	if (is_string($input)) {
		$input = utf8_encode($input);
	} else if (is_array($input)) {
		foreach ($input as &$value) {
			utf8_encode_deep($value);
		}

		unset($value);
	} else if (is_object($input)) {
		$vars = array_keys(get_object_vars($input));

		foreach ($vars as $var) {
			utf8_encode_deep($input->$var);
		}
	}
}

?>