<?php	require_once('../preheader1.php'); // <-- this include file MUST go first before any HTML/output
$user=$_SESSION['MM_Username'];
if(!isset($user)) header("Location:" . "../../index.php?deslogeo");
require_once('../../Connections/odonto_connection1.php'); 

$query_rs_todos = "SELECT negocio,categ FROM ingreso WHERE user LIKE '$user'";
$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 logeado FROM mesas WHERE negocio = '$negocio' AND logeado = 1";
$rs = mysqli_query($odonto_connection,$q) or die(mysqli_error());
$num_row_logeados = mysqli_num_rows($rs);

if(isset($_GET['id_avi'])){$id_avi=$_GET['id_avi'];
	$query_rs_todos = "UPDATE avisos SET avisado = 1 WHERE id_avi = $id_avi";
	$rs_ingreso2 = mysqli_query($odonto_connection,$query_rs_todos) or die(mysqli_error());
}

	#the code for the class
	include ('../ajaxCRUD.class.php'); // <-- this include file MUST go first before any HTML/output

    #this one line of code is how you implement the class
    ########################################################
    ##

    $tabla1 = new ajaxCRUD("MESAS", "mesas", "id_mesa", "../");

    ##
    ########################################################

    ## all that follows is setup configuration for your fields....
    ## full API reference material for all functions can be found here - http://ajaxcrud.com/api/
    ## note: many functions below are commented out (with //). note which ones are and which are not

    #i can define a relationship to another table
    #the 1st field is the fk in the table, the 2nd is the second table, the 3rd is the pk in the second table, the 4th is field i want to retrieve as the dropdown value
    #http://ajaxcrud.com/api/index.php?id=defineRelationship
    //$tabla1->defineRelationship("fkID", "tabla1Relationship", "pkID", "fldName", "fldSort DESC"); //use your own table - this table (tabla1Relationship) not included in the installation script

//$tabla1->defineRelationship("usuario","ingreso","user","doc");

//$tabla1->addAjaxFilterBox('celular',11);

    #i don't want to visually show the primary key in the table
    
	//$tabla1->displayAddFormTop();
	
	//$tabla1->showCSVExportOption();
	
	$tabla1->omitPrimaryKey();

    #the table fields have prefixes; i want to give the heading titles something more meaningful

	$tabla1->displayAs("codigo", "CODIGO");
	$tabla1->displayAs("mesa", "MESA");
	$tabla1->displayAs("descripcion", "PEDIDOS");
	$tabla1->displayAs("mozo", "ZONA");
	$tabla1->displayAs("mensaje", "MENSAJE A MESA");
	$tabla1->displayAs("ver", "MSJ");

	$tabla1->setTextboxWidth('mensaje', 80);


	#set the textarea height of the longer field (for editing/adding)
    #http://ajaxcrud.com/api/index.php?id=setTextareaHeight
    //$tabla1->setTextareaHeight('fldLongField', 150);

   //$tabla1->omitField("codigo");
   $tabla1->omitField("hash");
   $tabla1->omitField("logeado");
   $tabla1->omitField("sucursal");
   $tabla1->omitField("negocio");
   //$tabla1->omitField("mozo");
   $tabla1->omitField("nombre");
   $tabla1->omitField("ocup");
   $tabla1->omitField("id_delivery");
   $tabla1->disallowEdit('codigo'); 
   //$tabla1->disallowEdit('descripcion'); 
   $tabla1->disallowEdit('mensaje'); 
   $tabla1->disallowEdit('ver'); 
   $tabla1->disallowEdit('mesa'); 
   $tabla1->disallowEdit('descripcion');
	//$tabla1->omitFieldCompletely("mat");
 	//$tabla1->omitFieldCompletely('notas');
    #i could omit a field from being on the add form if I wanted
   
$tabla1->emptyTableMessage = "";
																					//$tabla1->turnOffAjaxADD();

//$tabla1->orderFields("paciente,nombre,dni,telefono,celular,obra,email,ficha,usuario");



    #i could set a field to accept file uploads (the filename is stored) if wanted
    //$tabla1->setFileUpload("fldField2", "uploads/");

//$tabla1->addValueOnInsert("hora", "NOW()");
//$tabla1->addValueOnInsert("negocio", $negocio);

//$tabla1->setInitialAddFieldValue ("nacionalidad", "Argentina");

    #i can have a field automatically populate with a certain value (eg the current timestamp)
    //$tabla1->addValueOnInsert("fldField1", "NOW()");
	$tabla1-> addWhereClause("WHERE (negocio = '$negocio' AND mesa != '999')");
//	$tabla1->turnOffAjaxEditing();

$tabla1->addButtonToRow("Cerrar", "auxi_limpiar.php" , "codigo");
$tabla1->addButtonToRow("Mensaje", "negocio_2_mesas.php" , "codigo");
$tabla1->addButtonToRow("Modif", "negocio_8.php" , "codigo");


	
    #i can use a where field to better-filter my table
    //$tabla1->addWhereClause("WHERE (fldField1 = 'test')");
    #i can order my table by whatever i want
 
 $tabla1->addOrderBy("ORDER BY mesa ASC");
 
    #i can set certain fields to only allow certain values
    #http://ajaxcrud.com/api/index.php?id=defineAllowableValues
    
	$q = "SELECT zona FROM zonas WHERE negocio = '$negocio' ";
	$rs = mysqli_query($odonto_connection,$q) or die(mysqli_error());
	$num_row = mysqli_num_rows($rs);
	$allowableValues = array();
	for ($i = 0; $i < $num_row; $i++ ){
		$allowableValues[$i] = $i + 1;
	}
    $tabla1->defineAllowableValues("mozo", $allowableValues);
	
	$tabla1->defineCheckbox("ver", "si", "no");
    //set field fldCheckbox to be a checkbox
    //$tabla1->defineCheckbox("fldCheckbox");

    #i can disallow deleting of rows from the table
    #http://ajaxcrud.com/api/index.php?id=disallowDelete
    $tabla1->disallowDelete();
    
$tabla1->hover_color = "#E9E8DC";
    $tabla1->setLimit(110);

//$tabla1->addMessage = "Paciente agregado correctamente";

$numRows = $tabla1->getNumRows();
$tabla1->disallowAdd();
//$tabla1->displayAddFormTop();

    #i can disallow adding rows to the table
    #http://ajaxcrud.com/api/index.php?id=disallowAdd
    //if($clinica=='clinica_28'){echo $tabla1->disallowAdd();}

    #i can add a button that performs some action deleting of rows for the entire table
    #http://ajaxcrud.com/api/index.php?id=addButtonToRow
  //  $tabla1->addButtonToRow("OK", "negocio_0.php", "$id_avi");

//$tabla1->formatFieldWithFunctionAdvanced('hora', 'formatfecha_fin');
//$tabla1->formatFieldWithFunctionAdvanced('aviso', 'formatfecha_fin');
//$tabla1->formatFieldWithFunctionAdvanced('descrip', 'formatfecha_fin');


function formatfecha_fin($data,$id)	
								{
$trozos = explode ("-", $data);
									$vemos=array();
									$vemos['0']=$trozos['2'];
									$vemos['1']=$trozos['1'];
									$vemos['2']=$trozos['0'];
									$data_c = implode('-', $vemos);

$carInfo = qr("SELECT * FROM avisos WHERE id_avi = $id");

$avisado = $carInfo['avisado'];
$hora = $carInfo['hora'];
$timer=date("Y-m-d H:i:s");

$horaInicio = new DateTime($hora);
$horaTermino = new DateTime($timer);
$interval = $horaInicio->diff($horaTermino);
//echo $interval->format('%i %s');
if($interval->format('%i') < '1' && $interval->format('%h') == '0' && $interval->format('%d') == '0' && $avisado != '1') {
	
//if($data < $fecha_estipulada2 && $status=="<font style='color:red'>PENDIENTE</font>")

	return "<span style='color:#75b257ff' class='parpadea text2'>$data</span>";
}

if($interval->format('%i') >= '1' && $interval->format('%i') < '2' && $interval->format('%h') == '0' && $interval->format('%d') == '0' && $avisado != '1') {
	
//if($data < $fecha_estipulada2 && $status=="<font style='color:red'>PENDIENTE</font>")

	return "<span style='color:#dbb326ff' class='parpadea text2'>$data</span>";
}

if($interval->format('%i') > '2'  && $interval->format('%h') == '0' && $interval->format('%d') == '0' && $avisado != '1' ) {
	
//if($data < $fecha_estipulada2 && $status=="<font style='color:red'>PENDIENTE</font>")

	return "<span style='color:#d84a2cff' class='parpadea text2'>$data</span>";
}

}

	$tabla1->formatFieldWithFunctionAdvanced('mesa', 'makeBlue');
	$tabla1->formatFieldWithFunctionAdvanced('descripcion', 'makeBlue');
	$tabla1->formatFieldWithFunctionAdvanced('mozo', 'makeBlue');
	$tabla1->formatFieldWithFunctionAdvanced('mensaje', 'makeBlue');
	$tabla1->formatFieldWithFunctionAdvanced('ver', 'makeBlue');
	$tabla1->formatFieldWithFunctionAdvanced('logeado', 'makeBlue');

	$tabla1->deleteText = "Borrar";
	$tabla1->addText= "AGREGAR";
	$tabla1->setCSSFile('greysuit.css');

	//$tabla1->addButtonText = "Register Me for this Event";

	#my self-defined functions used for formatFieldWithFunction
	function makeBold($val){
		if ($val == "") return "-----";
		return "<font style='font-size:10px'><b>$val</b></font>";
	}

	function makeBlue($val,$id){
		$carInfo = qr("SELECT logeado FROM mesas WHERE id_mesa = $id");
		$logeado = $carInfo['logeado'];
		if($logeado == 1)	return "<span style='color:#C7FF33;font-size:0.8rem'>$val</span>";
		else return "<span style='color:#fff;font-size:1.3rem'>$val</span>";
	}
	function myCallBackFunction($array){
		echo "THE ADD ROW CALLBACK FUNCTION WAS implemented";
		print_r($array);
	}
		
$tabla2 = new ajaxCRUD("DATOS", "datos", "id_datos", "../");
	$tabla2->omitPrimaryKey();
	$tabla2->displayAs("sonido", "Sonido");
	$tabla2->displayAs("concarta", "Muestra Carta");
	$tabla2->displayAs("conarmapedido", "Muestra Arma Pedido");
	//$tabla2->omitField("cod_pre");
	$tabla2->omitField("negocio");
	//$tabla2->disableTableHeaders();
	//$tabla2->emptyTableMessage = "nada para mostrar";
	$tabla2-> addWhereClause("WHERE (negocio = '$negocio')");
	//$tabla2->turnOffAjaxEditing();
	//$tabla2->addButtonToRow("OK", "index_precios.php?veamos", "id_pre");
	//$tabla2->addOrderBy("ORDER BY id_pre ASC");
	$tabla2->disallowDelete();
	$tabla2->setLimit(2);
	$tabla2->disallowAdd();
	//$tabla2->disallowEdit('menu'); 
	//$tabla2->disallowEdit('precio'); 
	$tabla2->setCSSFile('greysuit.css');
	$tabla2->addText= "AGREGAR";	

?>
<!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');?>
	
</head>
<body onload="setInterval('showUser()',2000)" style="background-color:#0D1749"/>
	
	<?php	 require_once('menu.php');?>
	<br>
	<?php	 require_once('botones.php');?>
	
	<div class="container-fluid text-center">
		<div class="row">
			<div class="col">
				<h3><a class="btn btn-outline-light" href="negocio_zonas.php">Habilitacion Zonas</a></h3>
			</div>
			<div class="col">
				<?php echo "<h3>".$num_row_logeados." mesas logueadas</h3>";?>
			</div>
			<div class="col">
				<h3><a class="btn btn-outline-light" href="auxi_limpiar.php?negocio=<?php echo $negocio;?>">Cerrar Todas</a></h3>
			</div>
		</div>
		<div class="row">
			<div class="col">
			<?php $tabla1->showTable();?>
			</div>
		</div>
	</div>

<style>
.text {
  font-size:18px;
  font-family:helvetica;
  font-weight:bold;
  color:#71d90b;
  text-transform:uppercase;
}
.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>

<div id=txtHint></div>
</body>
</html>


<script type="text/javascript">

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

$query_rs_todos = "SELECT id_mesa FROM `mesas` WHERE `negocio` like '$negocio' AND `logeado` = 1";
$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);

?>	
	var negocio = "<?php echo $negocio;?>";
	var orig = "<?php echo $row_cnt_orig;?>";

	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;
if(vemos.indexOf("aqui") > -1){myFunction();}
		}
        };
        //xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.open("GET","./getuser_mesas.php?negocio="+negocio+"&cantidad_original="+orig,true);
        xmlhttp.send();

		
    }
}

function myFunction() {
  window.open("negocio_2.php" , '_parent');
}

</script>
 