<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/helpers.php';
require_user();

$hoy = date('Y-m-d');
$from = $_GET['from'] ?? $hoy;
$to = $_GET['to'] ?? $hoy;

$where = ' WHERE f.patient_id = ? ';
$params = [(int)current_patient_id()];
$types = 'i';
if (!empty($from)) { $where .= ' AND f.followup_date >= ? '; $params[] = $from; $types .= 's'; }
if (!empty($to)) { $where .= ' AND f.followup_date <= ? '; $params[] = $to; $types .= 's'; }

$sql = "SELECT f.*, u.name user_name, pa.name patient_name, p.name procedure_name, m.name medication_name
        FROM medical_followups f
        INNER JOIN users u ON u.id = f.user_id
        INNER JOIN patients pa ON pa.id = f.patient_id
        LEFT JOIN procedures_catalog p ON p.id = f.procedure_id
        LEFT JOIN medications_catalog m ON m.id = f.medication_id
        $where
        ORDER BY f.followup_date DESC, f.followup_time DESC, f.id DESC";
$stmt = $mysqli->prepare($sql);
if (!$stmt) {
    die('Error al preparar la consulta de seguimiento: ' . $mysqli->error);
}
$stmt->bind_param($types, ...$params);
$stmt->execute();
$rows = $stmt->get_result();
include __DIR__ . '/../header.php';
?>
<div class="card card-pink p-4">
  <div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
    <div>
      <h4 class="m-0" style="color:#d63384;">Seguimientos cargados</h4>
      <small class="text-muted">Paciente: <strong><?= e(current_patient_name()) ?></strong></small>
    </div>
    <a class="btn btn-pink" href="create.php">Nuevo Item</a>
  </div>
  <?php if (!empty($_GET['ok'])): ?><div class="alert alert-success">Item guardado correctamente.</div><?php endif; ?>
  <form method="get" class="row g-2 mb-3">
    <div class="col-md-4"><input type="date" name="from" class="form-control" value="<?= e($from) ?>"></div>
    <div class="col-md-4"><input type="date" name="to" class="form-control" value="<?= e($to) ?>"></div>
    <div class="col-md-4 d-flex gap-2"><button class="btn btn-outline-danger w-100">Filtrar</button><a class="btn btn-outline-secondary" href="index.php">Limpiar</a></div>
  </form>
  <div class="table-responsive">
    <table class="table table-hover align-middle">
      <thead>
        <tr>
          <th>Fecha</th><th>Hora</th><th>Usuario</th><th>Procedimiento</th><th>Medicamento</th><th>PA</th><th>Sat. O₂</th><th>Observación</th>
        </tr>
      </thead>
      <tbody>
        <?php while($r = $rows->fetch_assoc()): ?>
          <tr>
            <td><?= date('d/m/Y', strtotime($r['followup_date'])) ?></td>
            <td><?= substr($r['followup_time'],0,5) ?></td>
            <td><span class="badge badge-pink"><?= e($r['user_name']) ?></span></td>
            <td><?= e($r['procedure_name'] ?? '') ?></td>
            <td><?= e($r['medication_name'] ?? '') ?></td>
            <td><?= e($r['blood_pressure']) ?></td>
            <td><?= e($r['oxygen_saturation']) ?></td>
            <td style="min-width:220px;"><?= nl2br(e($r['observation'])) ?></td>
          </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
  </div>
</div>
<?php include __DIR__ . '/../footer.php'; ?>
