 <?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['det'] = $_GET['det'];
$_SESSION['opcion'] = $_GET['opcion'];

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

//INSERTAR

if ($_SESSION['opcion'] == 1) {
    $sql = "INSERT INTO " . $_GET['database'] . ".familia_mensajes(f_mensage, f_id)
		SELECT '" . $_SESSION['det'] . "','" . $_GET['f_id'] . "'
		FROM dual
		WHERE NOT EXISTS (SELECT f_mensage FROM " . $_GET['database'] . ".familia_mensajes WHERE f_mensage = '" . $_SESSION['det'] . "' LIMIT 1)";
    $qur = $connection->query($sql);

}

// CONSULTA
if ($_SESSION['opcion'] == 2) {
    $sql = "SELECT * FROM " . $_GET['database'] . ".familia_mensajes WHERE f_id='" . $_GET['f_id'] . "'";
    $qur = $connection->query($sql);

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

        $jsonTempData = array();
        $jsonTempData['f_mensage'] = $r['f_mensage'];
        $jsonData[] = $jsonTempData;

    }

}

$json->detalle_comanda = $jsonData;

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

    $json->datos = "false";

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

}

print_r(json_encode($json));

@mysqli_close($connection);
?>
