<?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 pasajepaciente1 where id='".$id."'");
		// $count=mysqli_num_rows($query);
		// if ($count==0){
			if ($delete1=mysqli_query($con,"DELETE FROM pasajepaciente 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 Referido</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 = "pasajepaciente";
		 $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">
			  <table class="table-bordered">
			  	<input id="entradafilter" type="text" class="form-control" placeholder="BUSQUEDA DE REGISTRO DE SOLICITUD DEL PACIENTE"></strong>
			  	<thead>
				<tr  class="info">
					<th>N°</th>
					<th>F. Reg.</th>	
					<th>Cod. Ref.</th>
					<th>DNI</th>
					<th class='text-left'>Apellidos y Nombres</th>			
					<th>Lugar</th>
					<th class='text-left'>Servicio Origen</th>
					<!--<th>Servicio Destino</th>-->
					<th>Medico</th>
					<th>F. Cita</th> 
					<th>Registrado</th>
					<th>Aprobado</th>
					<th>Emitido</th>
					<th>Reimprimir</th>
					
				</tr>
				</thead>
				<tbody class="contenidobusqueda">
				<?php
				while ($row=mysqli_fetch_array($query)){
						$id=$row['id'];
						$fechareg= date('d/m/Y', strtotime($row['fechareg']));
						$nrorden=$row['nrorden'];
						$dni=$row['dni'];
						$apenom=$row['apenom'];
						$edad=$row['edad'];
						$telefono=$row['telefono'];
						$autogenerado=$row['autogenerado'];
						$lugarorigen=$row['lugarorigen'];
						$lugardestino=$row['lugardestino'];
						$hospital=$row['hospital'];
						$departamento=$row['departamento'];
						$servicio=$row['servicio'];
						$coddiagnostico=$row['coddiagnostico'];

						$fechaviaje= date('d/m/Y', strtotime($row['fechaviaje']));

						
						
						$nommedico=$row['nommedico'];
						$descripdiagnostico=$row['descripdiagnostico'];
						$dniaco=$row['dniaco'];
						$apenomaco=$row['apenomaco'];
						$edadaco=$row['edadaco'];
						$estado=$row['estado'];		
						$estado2=$row['estado2'];		
						$estado3=$row['estado3'];						
						
					?>
					<tr>
						<td><?php echo $item?></td>
						<td style="font-weight: bold;background: #B3F0AB"><?php echo $fechareg;?></td>
						<td style="font-weight: bold;"><?php echo $nrorden; ?></td>
						<td><?php echo $dni; ?></td>
						<td ><?php echo $apenom; ?></td>
						
						<td><?php echo $lugardestino?></td>
						<td ><?php echo $departamento?></td>
						<!--<td style="background-color: #F2F2F2;"><?php echo $servicio?></td>-->
						<td><?php echo $nommedico?></td>
						<td style="font-weight: bold;background-color: #B3F0AB;"><?php echo $fechaviaje; ?></td>
						<td style="background-color: #B0B1F5;"><?php echo $estado;?></td>
						<td style="background-color: #89B3F0;"><?php echo $estado2?></td>
						<td style="background-color: #6DA5F7;"><?php echo $estado3?></td>
						<td class='text-right'>
						<a href="#" class='btn btn-default' title='Ver Registro' data-nombre='<?php echo $nrorden;?>' data-descripcion='<?php echo $fechareg?>' data-dni='<?php echo $dni?>' data-apenom='<?php echo $apenom?>'  data-edad='<?php echo $edad?>' data-telefono='<?php echo $telefono?>' data-autogenerado='<?php echo $autogenerado?>' data-lugarorigen='<?php echo $lugarorigen?>' data-lugardestino='<?php echo $lugardestino?>'  data-hospital='<?php echo $hospital?>' data-departamento='<?php echo $departamento?>' data-servicio='<?php echo $servicio?>' data-coddiagnostico='<?php echo $coddiagnostico?>' data-descripdiagnostico='<?php echo $descripdiagnostico?>' data-nommedico='<?php echo $nommedico?>' data-dniaco='<?php echo $dniaco?>'  data-apenomaco='<?php echo $apenomaco?>' data-edadaco='<?php echo $edadaco?>'  data-id='<?php echo $id;?>' data-toggle="modal" data-target="#myModal2">Reimprimir</a> 
					</td>

					</tr>
					<?php
					$item=$item+1;
				}
				?>
				<tr>
					<td colspan=4><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>  