<?php
	include('is_logged.php');//Archivo verifica que el usario que intenta acceder a la URL esta logueado
	/* Connect To Database*/
	require_once ("../config/db.php");//Contiene las variables de configuracion para conectar a la base de datos
	require_once ("../config/conexion.php");//Contiene funcion que conecta a la base de datos
	
	$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
	if (isset($_GET['id'])){
		$id=intval($_GET['id']);
		// $query=mysqli_query($con, "select * from controlpaciente1 where id='".$id."'");
		// $count=mysqli_num_rows($query);
		// if ($count==0){
			if ($delete1=mysqli_query($con,"DELETE FROM controlpaciente WHERE id='".$id."'")){
			?>
			<div class="alert alert-success alert-dismissible" role="alert">
			  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			  <strong>Paciente </strong> eliminado.
			</div>
			<?php 
		}else {
			?>
			<div class="alert alert-danger alert-dismissible" role="alert">
			  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			  <strong>Error!</strong> Lo siento algo ha salido mal intenta nuevamente.
			</div>
			<?php
			
		}			
		} 
		
	if($action == 'ajax'){
		// escaping, additionally removing everything that could be (html/javascript-) code
         $q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
		 $aColumns = array('dni','apenom');//Columnas de busqueda
		 $sTable = "controlpaciente";
		 $sWhere = "";
		if ( $_GET['q'] != "" )
		{
			$sWhere = "WHERE (";
			for ( $i=0 ; $i<count($aColumns) ; $i++ )
			{
				$sWhere .= $aColumns[$i]." LIKE '%".$q."%' OR ";
			}
			$sWhere = substr_replace( $sWhere, "", -3 );
			$sWhere .= ')';
		}
		$sWhere.=" order by id DESC ";
		include 'pagination.php'; //include pagination file
		//pagination variables
		$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
		$per_page = 100; //how much records you want to show
		$adjacents  = 4; //gap between pages after number of adjacents
		$offset = ($page - 1) * $per_page;
		//Count the total number of row in your table*/
		$count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM $sTable  $sWhere  ");
		$row= mysqli_fetch_array($count_query);
		$numrows = $row['numrows'];
		$total_pages = ceil($numrows/$per_page);
		$reload = './clientes.php';
		//main query to fetch the data
		$sql="SELECT * FROM  $sTable  $sWhere   LIMIT $offset,$per_page";
		$query = mysqli_query($con, $sql);
		$item=1;
		//loop through fetched data
		if ($numrows>0){
			
			?>
			<div class="table-responsive container">
			  <table class="table" border="1">
			  	
			  	<input id="entradafilter" type="text" class="form-control" placeholder="BUSQUEDA DE REGISTRO DE SOLICITUD DEL PACIENTE"></strong>
			  	<thead  class="thead-dark">
				<tr  class="info">
					<th>N° REG</th>
					<th>Fecha Registro</th>	
					<th>DNI</th>
					<th class='text-left'>Apellidos y Nombres</th>			
					<th>Edad</th>
					<th class='text-left'>Pulso</th>
					<th>temperatura</th>
					<th>peso</th>
					<th>talla</th>
					<th>Presion Art.</th>
					<th>Saturación</th>
					<!--<th>Diagnostico</th>-->
					
					<th style = "width:8%;"> Acciones </th>
					
				</tr>
				
				</thead>
				<tbody class="contenidobusqueda">	
				<?php
				while ($row=mysqli_fetch_array($query)){
						$id=$row['id'];
						$fechareg= date('d/m/Y', strtotime($row['fechareg']));
						$dni=$row['dni'];
						$apenom=$row['apenom'];
						$edad=$row['edad'];
						$telefono=$row['telefono'];
						$pulso=$row['pulso'];
						$temperatura=$row['temperatura'];
						$peso=$row['peso'];
						$talla=$row['talla'];
						$presionart=$row['presionart'];		
						$saturacion=$row['saturacion'];					
						
					?>
					<tr>
						<td><?php echo $item?></td>						
						
						<td style="font-weight: bold;"><?php echo $fechareg;?></td>
						<td><?php echo $dni; ?></td>
						<td ><?php echo $apenom; ?></td>
						<td><?php echo $edad?></td>
						<td><?php echo $pulso?></td>
						<td><?php echo $temperatura?></td>
						<td><?php echo $peso?></td>
						<td><?php echo $talla?></td>
						<td style="background-color: #BDBDBD;"><?php echo $presionart?></td>
						<td><?php echo $saturacion?></td>



					<td class='text-right'>
						<a href="#" class='btn btn-default' title='Ver Registro' data-dni='<?php echo $dni;?>' data-fechareg='<?php echo $fechareg?>' data-dni='<?php echo $dni?>' data-apenom='<?php echo $apenom?>'  data-edad='<?php echo $edad?>' data-telefono='<?php echo $telefono?>' data-pulso='<?php echo $pulso?>' data-temperatura='<?php echo $temperatura?>' data-peso='<?php echo $peso?>'  data-talla='<?php echo $talla?>' data-presionart='<?php echo $presionart?>' data-saturacion='<?php echo $saturacion?>'   data-id='<?php echo $id;?>' data-toggle="modal" data-target="#myModal2">v</a> 
						<a href="#" class='btn btn-danger' title='Borrar Registro' onclick="eliminar('<?php echo $id; ?>')">x</a>
					</td>

					

					</tr>
					<?php
					$item=$item+1;
				}
				?>
				<tr>
					<td colspan=12><span class="pull-right">
					<?php
					 echo paginate($reload, $page, $total_pages, $adjacents);
					?></span></td>
				</tr>
				</tbody>
			  </table>
			</div>
			<?php
		}
	}
?>

<script>
$(document).ready(function () {
   $('#entradafilter').keyup(function () {
      var rex = new RegExp($(this).val(), 'i');
        $('.contenidobusqueda tr').hide();
        $('.contenidobusqueda tr').filter(function () {
            return rex.test($(this).text());
        }).show();

        })

});
</script>  