<?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=pg_connect($config['conexion']);
if (isset($_POST["zonsel"])) {
	$data1['zonsel']=$_POST["zonsel"];
	$zonsel=$_POST["zonsel"];
} else {
	$zonsel="-";
}

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

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


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,
		'senzonag'=> 		$row['senszona'],
		'transportista'=> $row['transportista'],

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

$data1['grid1'] = $grid1;


$conn=null;

}


//
//---------------------------------------
//

function carga_cbcontrolador($conn) {
	
	global $data1;
	
	$sql = "select controlador, ip from led_control";
	
	$choices = '';
	$values = '';
	$coma = false;
	$results = pg_query($conn,$sql);
	while ($row = pg_fetch_array($results)) {
		if ($coma) {
			$choices .= ',';
			$values .= ',';
		} else {
			$coma = true;
		}
		$choices .= $row['controlador'].' - '.$row['ip'];
		$values .= $row['controlador'];
	}
	
	$disponible = 1;
	$sql = "select max(controlador::integer)+1 as disponible from led_control";
	$results = pg_query($conn,$sql);
	while ($row = pg_fetch_array($results)) {
		$disponible = $row[0];
	}
	
	$data1['cbcontrolador_c'] = $choices;
	$data1['cbcontrolador_v'] = $values;
	$data1['controlador'] = $disponible;
	
	
	$conn=null;
	
}


function carga_transportistas($conn) {
	global $data1;

	$transportistas = '';
	
	$sql = "select distinct carrier from dhl_link_id where carrier is not null and carrier != ''";
	$result = pg_query($conn, $sql) or die('La consulta fallo: ' . pg_last_error());
	while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC)) {

		$transportista = $row['carrier'];
		if ($transportistas == '') {
			$transportistas = $transportista;
		} else{
			$transportistas .= ','.$transportista;
		}
		
	}

	pg_free_result($result);
	
	$data1['transportistas'] = $transportistas;
}


?>