<?php	
session_start();
if(isset($_SESSION['MM_Username'])) $user=$_SESSION['MM_Username'];
else {
	if(isset($_GET[negocio])){
		$sacar_menu = 1;
		$negocio = $_GET[negocio];
		require_once('../../Connections/odonto_connection1.php'); 
		$query_rs_todos = "SELECT user FROM ingreso WHERE negocio LIKE '$negocio' LIMIT 1";
		$rs_ingreso0 = mysqli_query($odonto_connection,$query_rs_todos) or die(mysqli_error());
		$row_rs_ingreso0 = mysqli_fetch_assoc($rs_ingreso0);
		$user=$row_rs_ingreso0[user];
	}
}

if(!isset($user)) {
	header("Location:" . "../../index.php?deslogeo");
	die();
}

if(isset($_POST[mozo_submit])) $mozo = $_POST[mozo];
if(isset($_GET[mozo])) $mozo = $_GET[mozo];

require_once('../../Connections/odonto_connection1.php'); 

$query_rs_todos = "SELECT categ,negocio FROM ingreso WHERE user LIKE '$user' LIMIT 1";
$rs_ingreso0 = mysqli_query($odonto_connection,$query_rs_todos) or die(mysqli_error());
$row_rs_ingreso0 = mysqli_fetch_assoc($rs_ingreso0);
$categ=$row_rs_ingreso0['categ'];
$negocio=$row_rs_ingreso0['negocio'];

$q = "SELECT id_datos,cliente_habilitado FROM datos WHERE negocio LIKE '$negocio' LIMIT 1";
$rs = mysqli_query($odonto_connection,$q) or die(mysqli_error());
$row = mysqli_fetch_assoc($rs);
$id_datos = $row['id_datos'];
$cliente_habilitado = $row['cliente_habilitado'];

if(!$cliente_habilitado){
	echo "<h1>Disculpas...Cliente inhabilitado por falta de pago...<h1>)-:</h1></h1>";
	die();
}

if(isset($_GET['confirmated']))
{
	$existe = 0;
	$id_delivery = $_GET['confirmated'];//es el id_delivery
	
	$q = "SELECT confiable FROM delivery WHERE id_delivery LIKE '$id_delivery' LIMIT 1";
	$rs = mysqli_query($odonto_connection,$q) or die(mysqli_error());
	$row = mysqli_fetch_assoc($rs);
	$confiable = $row['confiable'];
	if($confiable) {
		$confiable_decod = json_decode($confiable);
		foreach($confiable_decod as $clave => $valor){
			if($valor == $id_datos) $existe = 1;			
		}
	}
	else $confiable_decod = array();
	
	if(!$existe){
		
		$confiable_decod[] = $id_datos;
		$json=json_encode($confiable_decod);
	
		$sql = "UPDATE delivery SET confiable = '$json' WHERE id_delivery = '$id_delivery'";
		$rs = mysqli_query($odonto_connection, $sql) or die(mysqli_error());
	}
}

if(isset($_GET['no_confiable']))
{
	$existe = 0;
	$id_delivery = $_GET['confirmated'];//es el id_delivery
	
	$q = "SELECT confiable FROM delivery WHERE id_delivery LIKE '$id_delivery' LIMIT 1";
	$rs = mysqli_query($odonto_connection,$q) or die(mysqli_error());
	$row = mysqli_fetch_assoc($rs);
	$confiable = $row['confiable'];
		$confiable_decod = json_decode($confiable);
		foreach($confiable_decod as $clave => $valor){
			if($valor == $id_datos) unset($confiable_decod[$clave]);
		}

		$json=json_encode($confiable_decod);
		$sql = "UPDATE delivery SET confiable = '$json' WHERE id_delivery = '$id_delivery'";
		$rs = mysqli_query($odonto_connection, $sql) or die(mysqli_error());

}

if(isset($_GET['id_avi']))
{
	$id_avi=$_GET['id_avi'];
	$codigo=$_GET['codigo'];

	$q = "UPDATE avisos SET avisado = 1,hora_avisado = CURRENT_TIMESTAMP WHERE id_avi = $id_avi";
	$rs = mysqli_query($odonto_connection,$q) or die(mysqli_error());
	
	$query_rs_todos = "DELETE FROM pedidos WHERE mesa = '$codigo' ";
	$rs_ingreso0 = mysqli_query($odonto_connection,$query_rs_todos) or die(mysqli_error());
		
	$q = "UPDATE mesas SET ocup = '0',descripcion = '' WHERE codigo LIKE '$codigo'";
	$rs= mysqli_query($odonto_connection,$q) or die("Error en $q");
	
}


?>
<!DOCTYPE HTML>
<html>
<head>
<!--<meta http-equiv="refresh" content="7" charset="UTF-8"> -->
<meta charset="UTF-8">
<title>PonderZone</title>
<link rel="icon" href="../../pondercode.jpg">

<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0" />

<?php require_once('../../inc/bootstrap.php');?>

<link href='https://fonts.googleapis.com/css?family=Handlee' rel='stylesheet'>

<style>
body{
	background-color:#0D1749;
	background-image:none;
}

</style>

</head>
<body onload="setInterval('showUser()',2000)" />

<?php if(!$sacar_menu) require_once('menu.php');?>
<div class=row>
	<div class="col text-white text-center"><?php if($mozo) echo "<h3>Zona: ".$mozo."</h3>"; ?></div>
</div>	
<div class=row>
<div class=col style='position:absolute;top:80px;left:70px;width:500px;heigth:1000px' class="popup" id="txtHint"></div>
</div>  
</body>
</html>


<script type="text/javascript">

function myfuncionListo(id_avi){
	var id_avi = id_avi;
	$.post("getuser_limpia.php",{id_avi:id_avi});
}

function showUser(str) {
		<?php 
		require_once('../../Connections/odonto_connection1.php'); 

		$query_rs_todos = "SELECT id_avi FROM avisos WHERE negocio like '$negocio'";
		$rs_ingreso = mysqli_query($odonto_connection,$query_rs_todos) or die(mysqli_error());
		$row_rs_ingreso = mysqli_fetch_assoc($rs_ingreso);
		$row_cnt_orig = mysqli_num_rows($rs_ingreso);


		$query_rs_todos = "SELECT sonido FROM datos WHERE negocio like '$negocio'";
		$rs_ingreso = mysqli_query($odonto_connection,$query_rs_todos) or die(mysqli_error());
		$row_rs_ingreso = mysqli_fetch_assoc($rs_ingreso);
		$sonido=$row_rs_ingreso['sonido'];

		?>	
			var negocio = <?php echo "'".$negocio."'";?>;
			var orig = <?php echo "'".$row_cnt_orig."'";?>;
			var categ = <?php echo "'".$categ."'";?>;
			var sonido = <?php echo "'".$sonido."'";?>;
			var mozo = <?php echo "'".$mozo."'";?>;

			if (str == "") {
				document.getElementById("txtHint").innerHTML = "";
				return;
			} else { 
				if (window.XMLHttpRequest) {
					// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				} else {
					// code for IE6, IE5
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				xmlhttp.onreadystatechange = function() {
					if (this.readyState == 4 && this.status == 200) {
						document.getElementById("txtHint").innerHTML = this.responseText;
						var vemos = this.responseText;
						var nin = vemos.indexOf("fas");
						//alert(nin);
						//if(nin == 144){startFlashTitle("Nuevo Pedido");}
						//startFlashTitle("Nuevo Pedido");
				}
				};
				xmlhttp.open("GET","./getuser.php?q="+negocio+"&r="+orig+"&c="+categ+"&s="+sonido+"&mozo="+mozo,true);
				xmlhttp.send();
			
			}
}


  (function () {
   var titleOriginal = document.title;   
   var intervalBlinkTitle = 0;
 
   // Inicia el parpadeo del título la página
   window.startFlashTitle = function (newTitle) {       
       if(intervalBlinkTitle == 0){
        document.title = 
         (document.title == titleOriginal) ? newTitle : titleOriginal;           
 
        intervalBlinkTitle = setInterval(function (){
         document.title = 
          (document.title == titleOriginal) ? newTitle : titleOriginal;           
        }, 800);
    } 
   };
 
   // Para el parpadeo del título de la página   
   // Restablece el título
   window.stopFlashTitle = function () {       
       clearInterval(intervalBlinkTitle);
       intervalBlinkTitle = 0;
       document.title = titleOriginal;
   };
  }());
  
</script>
 
<style> 

.parpadea {
  animation-name: parpadeo;
  animation-duration: 1s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;

  -webkit-animation-name:parpadeo;
  -webkit-animation-duration: 1s;
  -webkit-animation-timing-function: linear;
  -webkit-animation-iteration-count: infinite;
}

@-moz-keyframes parpadeo{  
  0% { opacity: 1.0; }
  50% { opacity: 0.0; }
  100% { opacity: 1.0; }
}

@-webkit-keyframes parpadeo {  
  0% { opacity: 1.0; }
  50% { opacity: 0.0; }
   100% { opacity: 1.0; }
}

@keyframes parpadeo {  
  0% { opacity: 1.0; }
   50% { opacity: 0.0; }
  100% { opacity: 1.0; }
}


</style>