 <?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'] = $_POST['database'];
$_SESSION['json_mesa'] = $_POST['json_mesa'];
$_SESSION['imgfondo'] = $_POST['imgfondo'];
$_SESSION['sala'] = $_POST['sala'];
$_SESSION['suc_id'] = $_POST['suc_id'];
$existe = 0;

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

$sql = "delete from " . $_SESSION['database'] . ".json_mesas where sala=" . $_SESSION['sala'] . " and suc_id =" . $_SESSION['suc_id'] . "";

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

$sql = "insert into " . $_SESSION['database'] . ".json_mesas (imgfondo, json_mesa, sala, suc_id) values('" . $_SESSION['imgfondo'] . "', '" . $_SESSION['json_mesa'] . "', " . $_SESSION['sala'] . ", " . $_SESSION['suc_id'] . ")";

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

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

    $json->datos = "false";

} else {

    $json->datos = "true";

    $sala = $_SESSION['sala'] + 1;

    $sql = "selct 1 as existe from " . $_SESSION['database'] . ".table_rest_zone where tz_id =" . $sala . " and suc_id =" . $_SESSION['suc_id'];

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

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

        $existe = $r['existe'];
    }

    if ($existe == "0") {

        $descri_zona = 'Sala ' . $sala;

        $sql = "insert into " . $_SESSION['database'] . ".table_rest_zone values (" . $sala . ",'.$descri_zona.',0,'',1," . $_SESSION['suc_id'] . ",0,0)";

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

    }

}

print_r(json_encode($json));

@mysqli_close($connection);
?>
