<?php if(!isset($_SESSION)){session_start();} ////// EMPIEZA CODIGO PARA VERIFICAR LOGEO
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False;
  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { $isValid = true; } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { $isValid = true; } 
    if (($strUsers == "") && true) { $isValid = true;  }  } 
  return $isValid; }
$MM_restrictGoTo = "index.php?banned";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;} ////// TERMINA CODIGO PARA VERIFICAR LOGEO

/////// CONEXIÓN A LA BASE DE DATOS /////////
$host = 'localhost';
$basededatos = 'elindelc_dental';
$usuario = 'elindelc_dental0';
$contraseña = 'dental001';

$conexion = new mysqli($host, $usuario,$contraseña, $basededatos);
if ($conexion -> connect_errno)
{
	die("Fallo la conexion:(".$conexion -> mysqli_connect_errno().")".$conexion-> mysqli_connect_error());
}

//////////////// VALORES INICIALES ///////////////////////

$user=$_SESSION['MM_Username'];
$query = "SELECT clinica FROM `ingreso` WHERE `user` like '$user'";
$buscarclinica=$conexion->query($query);
$filaclinica= $buscarclinica->fetch_assoc();

$clinica=$filaclinica['clinica'];


$tabla="";
$query="SELECT * FROM tabla1 WHERE clinica LIKE '$clinica' LIMIT 0";
	
///////// LO QUE OCURRE AL TECLEAR SOBRE EL INPUT DE BUSQUEDA ////////////
if(isset($_POST['alumnos']) && strlen($_POST['alumnos'])>2)
{
if ($_POST['alumnos'] != "" && $_POST['alumnos'] != " " && $_POST['alumnos'] != NULL)
{
	$id_user = isset($_POST['id_user']) ? $_POST['id_user'] : null;
	if($id_user != null) $buscausuario="AND
		num_usuario LIKE ".$id_user;
		else{$buscausuario='';}
	$q=$conexion->real_escape_string($_POST['alumnos']);
	$query="SELECT * FROM tabla1 WHERE  
		(paciente LIKE '%".$q."%' OR
		nombre LIKE '%".$q."%' OR
		dni LIKE '%".$q."%' OR
		celular LIKE '%".$q."%' OR
		obra LIKE '%".$q."%' OR
		email LIKE '%".$q."%'
		) AND
		clinica LIKE '".$clinica."'".$buscausuario;
}
if(stristr($_POST['alumnos'], " "))
{
	$q=$conexion->real_escape_string($_POST['alumnos']);
	$porciones = explode(" ", $q);
	$query="SELECT * FROM tabla1 WHERE  
		(paciente LIKE '%".$porciones[0]."%' AND 
		nombre LIKE '%".$porciones[1]."%' AND 
		nombre LIKE '%".$porciones[2]."%' OR
		paciente LIKE '%".$porciones[0]."%' AND 
		paciente LIKE '%".$porciones[1]."%' AND 
		nombre LIKE '%".$porciones[2]."%'
		) AND
		clinica LIKE '".$clinica."' ";
}
}
else
{
	$query="SELECT * FROM tabla1 WHERE clinica LIKE '$clinica' ORDER BY paciente DESC limit 0";
}

$buscarAlumnos=$conexion->query($query);

if ($buscarAlumnos->num_rows > 0)
{
	$tabla.= 
	'<table class="table table display table-bordered table-responsive table-hover" style="background-color:#AAE4ED;color:#000">
		';

	while($filaAlumnos= $buscarAlumnos->fetch_assoc())
	{
		$tabla.=
		'<tr>
			<td><a href="pac.php?cual=1">'.$filaAlumnos['paciente'].'</ta></td>
			<td><a href="pac.php?cual=1">'.$filaAlumnos['nombre'].'</ta></td>
			<td>'.$filaAlumnos['dni'].'</td>
			<td>'.$filaAlumnos['celular'].'</td>
			<td>'.$filaAlumnos['obra'].'</td>
			<td>'.$filaAlumnos['email'].'</td>
			<td>'.$filaAlumnos['usuario'].'</td>
			<td><a href="pacientes.php" class="btn btn-info" role="button">TURNO</a></td>
			<td><a href="pacientes.php" class="btn btn-warning" role="button">SMS</a></td>
		</tr>
		';
	}

	$tabla.='</table>';
} else
	{
		$tabla="<h6 style='color:#365899'>********</h6>";
	}

echo $tabla;
?>
