<?php session_start(); ?>
<html class="no-js" lang="es">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>Gestión de pacientes</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="../static/style.css" type="text/css" media="screen" />
    </head>
    <body>
    <?php include 'navbar.php' ?>
        <div style="text-align: right; margin-top: 50px;">
            <a href="forms/patient_create_form.php" class="create_btn" >Crear</a>
        </div>
        <table>
            <thead>
                <tr>
                    <th>Nombre</th>
                    <th>Apellido</th>
                    <th>Fecha de nacimiento</th>
                    <th>Documento identificativo</th>
                    <th>Tipo de documento</th>
                    <th>Dirección</th>
                    <th>Provincia</th>
                    <th>Localidad</th>
                    <th>Pais</th>
                    <th>Citas</th>
                    <th>Informes</th>
                    <th colspan="2">Acciones</th>
                </tr>
            </thead>
            <?php
            include 'database.php';

            $pdo = connectDatabase();

            if (isset($_GET["search"])) {
                $list = findPatient($pdo, $_GET["search"]);
            } else {
                $list = listPatients($pdo);
            }

            foreach($list as $row) :
            ?>
                <tr>
                    <td><?php echo $row[1]; ?></td>
                    <td><?php echo $row[2]; ?></td>
                    <td><?php echo $row[3]; ?></td>
                    <td><?php echo $row[4]; ?></td>
                    <td><?php echo $row[5]; ?></td>
                    <td><?php echo $row[6]; ?></td>
                    <td><?php echo $row[7]; ?></td>
                    <td><?php echo $row[8]; ?></td>
                    <td><?php echo $row[9]; ?></td>
                    <td><?php echo $row[10]; ?></td>
                    <td><?php echo $row[11]; ?></td>
                    <td>
                        <a href="forms/patient_edit_form.php?edit=<?php echo $row[0]; ?>" class="edit_btn">Editar</a>
                    </td>
                    <td>
                        <a href="patient_management.php?delete=<?php echo $row[0]; ?>" class="del_btn">Borrar</a>
                    </td>
                </tr>
            <?php endforeach ?>
            <?php closeDatabase($pdo); ?>
        </table>
        <div>
            <form method="post" action="patient_management.php">
                <div class="search-group">
                    <input type="text" name="search_box" value="">
                </div>
                <div class="input-group">
                    <button class="btn" type="submit" name="search" >Buscar</button>
                </div>
            </form>
        </div>
        <?php if (isset($_GET["search"])):  ?>
        <div style="text-align: right; margin-top: 40px;">
            <a href="patient.php" class="back_btn">Atrás</a>
        </div>
        <?php endif ?>
    </body>
</html>
