<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

error_reporting(E_ERROR | E_PARSE);
$_SESSION['database'] = $_GET['database'];
$_SESSION['e_id_ant'] = $_GET['e_id_ant'];
$dep = $_SESSION['database'] . ".estaciones";

include 'conn.php';
include 'funciones.php';

$jsonData = array();
$jsonTempData = array();

/////////INICIO estaciones
/////////
/////////

$jsonData3 = array();
$jsonTempData3 = array();

$estacionsql = "SELECT e_activo, uso_tablet FROM " . $dep . " where suc_id = " . $_GET['suc_id'] . " and e_id = " . $_GET['e_id'] . "";

//print_r($estacionsql);
$qur = $connection->query($estacionsql);

$json->datos = "true";

$jsonTempData3 = array();
$uso = 0;
$activo = 0;

while ($r = mysqli_fetch_assoc($qur)) {

    $uso = $r['uso_tablet'];
    $activo = $r['e_activo'];
}

if ($uso == 0 && $activo == 1) {
    $estacionsql = "update " . $dep . " set uso_tablet = 1 where suc_id = " . $_GET['suc_id'] . " and e_id = " . $_GET['e_id'] . "";
    $qur = $connection->query($estacionsql);

    $estacionsql = "update " . $dep . " set uso_tablet = 0 where suc_id = " . $_GET['suc_id'] . " and e_id = " . $_GET['e_id_ant'] . "";
    $qur = $connection->query($estacionsql);

}

$jsonTempData3['uso'] = $uso;
$jsonTempData3['activo'] = $activo;
$jsonData3[] = $jsonTempData3;
$json->estacionuso = $jsonData3;

/////////
///////// FIN estaciones
/////////
/////////

print_r(json_encode($json));

$qur=null;
@mysqli_close($connection);
?>