<?php
include("header.php");
$sql = "SELECT * FROM `pacientes` WHERE pac_activo = 1 order by pac_nombre";
if (!empty($_POST)) {
    $nompac = $_POST['nompac'];
    $sql = "SELECT * FROM `pacientes` WHERE pac_activo = 1 and `pac_nombre` LIKE '%".$nompac."%'";
}
$resultado = mysqli_query($mysqli, $sql);
?>

<div class="content-wrapper">
    <!-- START PAGE CONTENT-->
    <div class="page-heading">
        <h1 class="page-title">Pacientes: <?if (!empty($_POST)){ echo 'Busqueda por: <b>' . $nompac . '</b>';}?></h1>
        <ol class="breadcrumb">
            <li class="breadcrumb-item">
                <button onclick="location.href='npacientes.php'" type="button" class="btn btn-primary">Nuevo</button>
            </li>

        </ol>
    </div>
    <div class="page-content fade-in-up">
        <div class="ibox">
            <div class="ibox-head">
                <div class="ibox-title">Listado</div>
            </div>
            <div class="ibox-body">
                <table class="table table-striped table-bordered table-hover display dataTable_width_auto" id="dataTablePacientes" cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th> NOMBRE </th>
                            <th> FECHA DE NACIMIENTO </th>
                            <th> TELEFONO </th>
                            <th> CELULAR </th>
                            <th> EMAIL </th>
                            <th> CITA </th>
                            <th></th>

                        </tr>
                    </thead>
                    <tbody>
                        <?php $NUM = 0;
                        while ($row = mysqli_fetch_array($resultado)) {
                            $NUM = $NUM + 1 ?>
                            <tr>
                                <?
                                $sqlexp = "SELECT * FROM `expediente`,pacientes,tiponota WHERE `exp_pac_id`=pac_id and tip_id=exp_tip_id and exp_pac_id = " . $row['pac_id'] . " order by `exp_id` desc limit 1";
                                $resultexp = $mysqli->query($sqlexp);
                                $rowexp = $resultexp->fetch_assoc();
                                $sqlage = "SELECT * FROM `agenda` where `age_pac_id`= " . $row['pac_id'] . " and `age_fecha` >= now() ORDER BY `age_id`  DESC limit 1";
                                $resultage = $mysqli->query($sqlage);
                                $rowage = $resultage->fetch_assoc();
                                $date1='';
                                $date2='';
                                if ($rowage['age_fecha'] != '') {
                                    $date1 = date_create($rowage['age_fecha']);
                                    $date1 = date_format($date1, "d/m/Y");
                                }
                                else
                                $date1='No hay próxima cita.';
                                if ($rowexp['exp_fecha'] != '') {
                                    $date2 = date_create($rowexp['exp_fecha']);
                                    $date2 = date_format($date2, "d/m/Y");
                                }
                                else
                                $date2='No hay cita previa.';
                                $date3 = date_create($row['pac_nacimiento']);
                                $date3 = date_format($date3, "d/m/Y");

                                ?>
                                <td><?php echo '<a href="notaexpediente.php?id=' . $rowexp['exp_id'] . '&tip_id=' . $row['tip_id'] . 'tabindex="-1" role="button" aria-disabled="true">' . $row['pac_nombre'] . '</a>'; ?></td>
                                <td><?php echo $date3; ?></td>
                                <td><?php echo $row['pac_telefono']; ?></td>
                                <td><?php echo $row['pac_celular']; ?></td>
                                <td><?php echo $row['pac_email']; ?></td>

                                <td> <?php echo 'Próxima CITA: <br><b>' . $date1 . '</b><br>Cita Anterior: <br><b>' . $date2 . '</b>'; ?></td>
                                <td>
                                    <div class=" col-12 ">
                                        <CENTER>
                                            <a href="npacientes.php?id=<?php echo $row['pac_id']; ?>&edt=1" class="btn btn-success" tabindex="-1" role="button" aria-disabled="true">MODIFICAR&nbsp;&nbsp;&nbsp;&nbsp;</a>
                                            <a href="tiponota.php?id=<?php echo $row['pac_id']; ?>" class="btn btn-success" tabindex="-1" role="button" aria-disabled="true">NUEVA NOTA</a>
                                            <a href="rexpediente.php?id=<?php echo $row['pac_id']; ?>" class="btn btn-success" tabindex="-1" role="button" aria-disabled="true">EXPEDIENTE&nbsp;&nbsp;</a>
                                            <a href="estudios.php?id=<?php echo $row['pac_id']; ?>" class="btn btn-success" tabindex="-1" role="button" aria-disabled="true">ESTUDIOS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
                                            <a href="nagenda.php?id=<?php echo $row['pac_id']; ?>" class="btn btn-success" tabindex="-1" role="button" aria-disabled="true">AGENDAR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
                                            <a href="nventa.php?id=<?php echo $row['pac_id']; ?>" class="btn btn-success" tabindex="-1" role="button" aria-disabled="true">VENTAS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
                                            <a href="#" data-href="npacientes.php?id=<?php echo $row['pac_id'] . '&borrar=1'; ?>" data-toggle="modal" data-target="#confirm-delete" class="btn btn-danger" tabindex="-1" role="button" aria-disabled="true">ELIMINAR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
                                        </CENTER>
                                    </div>
                                </td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php
        include('footer.php');
        ?>