<?php
	// include Database connection file 
	include("db_connection.php");

	// Design initial table header 
	$id_pac=$_GET['id_pac'];
	
	$query1 = "SELECT * FROM tabla1 WHERE id_pac LIKE '$id_pac'";

	if (!$result = mysqli_query($con, $query1)) {
        exit(mysqli_error($con));
    }
	$row1 = mysqli_fetch_assoc($result);
	
	$query = "SELECT * FROM historia WHERE id_pac LIKE '$id_pac' order by fecha DESC, id_histo DESC";

	if (!$result = mysqli_query($con, $query)) {
        exit(mysqli_error($con));
    }
	
	echo "<h3>Historia Clinica y de pagos de ".$row1['paciente']." ".$row1['nombre']."</h3><p>";
	$data = '<table class="table table-bordered table-striped" id="myTable">
						<tr>
							<th>Fecha</th>
							<th>Pza</th>
							<th>Prestacion</th>
							<th>Observac.</th>
							<th>Predet</th>
							<th>Profesional</th>
							<th>Borrar</th>
						</tr>';
    // if query results contains rows then featch those rows 
    if(mysqli_num_rows($result) > 0)
    {
    	$number = 1;
    	while($row = mysqli_fetch_assoc($result))
    	{
			$fecha=$row['fecha'];
					$trozos = explode ("-", $fecha);
					$vemos=array();
					$vemos['0']=$trozos['2'];
					$vemos['1']=$trozos['1'];
					$vemos['2']=$trozos['0'];
					$fecha = implode('/', $vemos);
	switch ($row['predet']) {
    case "extra":
        $predeterminado="EXTRACC";
        break;
    case "perno":
        $predeterminado="PERNO";
        break;
    case "circu":
        $predeterminado="CORONA";
        break;
	case "tc":
        $predeterminado="T. COND.";
        break;
	case "implante":
        $predeterminado="IMPLANT.";
        break;
	case "inc":
        $predeterminado="INCRUSTAC.";
        break;
    default:
        $predeterminado="------";
}

				$data .= '<tr>
				<td style="cursor:pointer;width:40px"><p onclick="GetUserDetails('.$row['id_histo'].')">'.$fecha.'</p></td>
				<td style="cursor:pointer;width:20px"><p onclick="GetUserDetails('.$row['id_histo'].')">'.$row['pieza'].'</p></td>
				<td style="cursor:pointer;max-width:100px"><p onclick="GetUserDetails('.$row['id_histo'].')">'.$row['tratamiento'].'</p></td>
				<td style="cursor:pointer;max-width:130px"><p onclick="GetUserDetails('.$row['id_histo'].')">'.$row['observaciones'].'</p></td>
				<td style="cursor:pointer;max-width:60px"><p onclick="GetUserDetails('.$row['id_histo'].')">'.$predeterminado.'</p></td>
				<td style="cursor:pointer;max-width:130px"><p onclick="GetUserDetails('.$row['id_histo'].')">'.$row['profesional'].'</p></td>
				<td style="max-width:40px">
					<button onclick="DeleteUser('.$row['id_histo'].')" class="btn btn-danger">Borrar</button>
				</td>
    		</tr>';
    		$number++;
    	}
    }
    else
    {
    	// records now found 
    	$data .= '<tr><td colspan="6">No hay datos!</td></tr>';
    }

    echo $data;

?>


