<?php

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

$zona=$_GET["zona"];
$ras=$_GET["ras"];
$intentos=$_GET["intentos"];
$senszona = $_GET['senszona'];

$config = require ('config.php');
$conexion=$config['conexion'];

$conn = pg_connect($conexion);

$existe='N';
$sql="select * from zonas where zona='$zona' and ras = '$ras'";
$result = pg_query($conn,$sql);
if ($row = pg_fetch_array($result)) {
	$existe='S';
}


if ($existe=='N') {

	$sql="update zonas set seleccion='-'";
	$result = pg_query($conn,$sql);
	
	$sql="insert into zonas (ras,zona,font_baliza,mili_seg_togo,seleccion,x,y,w,h,x_roi,h_roi,y_roi,w_roi,x_textos,y_textos,x_baliza,y_baliza,x_roi2,y_roi2,w_roi2,h_roi2,x_roi3,y_roi3,h_roi3,w_roi3,x_roi4,y_roi4,h_roi4,w_roi4,senszona,display_ptl)
			values('$ras','$zona',1,$intentos,'S',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,$senszona,'RAS$ras"."POS$zona')";
	$result = pg_query($conn,$sql);
	
	$sql="insert into led values(0,0,0,0,'RAS$ras"."POS$zona')";
	$result = pg_query($conn,$sql);
	
	echo 'OK|OK';

} else {
	echo "OK|La Zona $zona ya existe.";

}

pg_close($conn);


?>