 <?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['t_id'] = $_GET['t_id'];
$_SESSION['t_ubic'] = $_GET['t_ubic'];
$_SESSION['t_top'] = $_GET['t_top'];
$_SESSION['t_left'] = $_GET['t_left'];
$_SESSION['t_width'] = $_GET['t_width'];
$_SESSION['t_height'] = $_GET['t_height'];
$_SESSION['tz_id'] = $_GET['tz_id'];
$_SESSION['suc_id'] = $_GET['suc_id'];
$_SESSION['opcion'] = $_GET['opcion'];

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

$jsonData = array();
$jsonTempData = array();
$indicador = "0";

if ($_SESSION['opcion'] != "5") {
    $sqlrest = "select 1 as indicador from " . $_GET['database'] . ".table_rest " .
        " where t_id = " . $_SESSION['t_id'] .
        " and tz_id =" . $_SESSION['tz_id'] .
        " and suc_id = " . $_SESSION['suc_id'] . "";

    $qur = $connection->query($sqlrest);

    while ($r = mysqli_fetch_assoc($qur)) {
        $indicador = $r['indicador'];
    }

    if ($indicador == "0") {
        //INSERTAR
        $sql = "insert into " . $_GET['database'] . ".table_rest (t_id, t_name, t_active, t_status, t_pers, t_ubic, t_top," .
            " t_left, t_width, t_height,tz_id, suc_id)" .
            " values (" . $_SESSION['t_id'] . ", " . $_SESSION['t_id'] . ", 1, 0" .
            ", 80, " . $_SESSION['t_ubic'] .
            ", " . $_SESSION['t_top'] . ", " . $_SESSION['t_left'] .
            ", " . $_SESSION['t_width'] .
            ", " . $_SESSION['t_height'] . ", " . $_SESSION['tz_id'] . "," . $_SESSION['suc_id'] . ")";

        $qur = $connection->query($sql);
    }
}

if ($_SESSION['opcion'] == "1") {
    //UPDATE
    $sql = "update " . $_GET['database'] . ".table_rest set t_active = 1 " .
        ", t_ubic = " . $_SESSION['t_ubic'] . ", t_top =" . $_SESSION['t_top'] .
        ", t_left = " . $_SESSION['t_left'] . ", t_width= " . $_SESSION['t_width'] .
        ", t_height = " . $_SESSION['t_height'] .
        " where t_id = " . $_SESSION['t_id'] .
        " and tz_id =" . $_SESSION['tz_id'] .
        " and suc_id = " . $_SESSION['suc_id'] . "";
}

if ($_SESSION['opcion'] == "2") {
    //UPDATE
    $sql = "update " . $_GET['database'] . ".table_rest set t_active = 1" .
        ", t_ubic = " . $_SESSION['t_ubic'] .
        " where t_id = " . $_SESSION['t_id'] .
        " and tz_id =" . $_SESSION['tz_id'] .
        " and suc_id = " . $_SESSION['suc_id'] . "";
}

if ($_SESSION['opcion'] == "3") {
    //UPDATE
    $sql = "update " . $_GET['database'] . ".table_rest set t_status = 2" .
        " where t_id = " . $_SESSION['t_id'] .
        " and tz_id =" . $_SESSION['tz_id'] .
        " and suc_id = " . $_SESSION['suc_id'] . "";
}

if ($_SESSION['opcion'] == "4") {
    //UPDATE
    $sql = "update " . $_GET['database'] . ".table_rest set t_status = 0" .
        " where t_id = " . $_SESSION['t_id'] .
        " and tz_id =" . $_SESSION['tz_id'] .
        " and suc_id = " . $_SESSION['suc_id'] . "";
}

if ($_SESSION['opcion'] == "5") {
    //DELETE
    $sql = "DELETE FROM " . $_GET['database'] . ".table_rest" .
        " where t_id not in (" . $_SESSION['t_id'] .
        ") and tz_id =" . $_SESSION['tz_id'] .
        " and suc_id = " . $_SESSION['suc_id'] . "";
}

$qur = $connection->query($sql);

if ($_SESSION['codigo'] == "99") {

    $json->datos = "false";

} else {
    $json->datos = "true";

}

print_r(json_encode($json));

@mysqli_close($connection);
?>