File "diagnostico.php"

Full Path: C:/wamp64/www/casos_medicos/diagnostico.php
File size: 5.89 KB
MIME-type: text/x-php
Charset: utf-8

<?php
include("./conexion.php");

$cedula = $_GET['cedula'] ?? '';

$sqlDiag = "SELECT dp.*, d.descripcion AS diagnostico_descripcion
            FROM diagnostico_paciente dp
            LEFT JOIN diagnostico d ON dp.codigo_diagnostico = d.id
            WHERE dp.cedula = '$cedula'
            ORDER BY dp.fecha DESC";

$resDiag = $conn->query($sqlDiag);
?>

<style>
.main-row {
  cursor: pointer;
  transition: background-color 0.2s;
}
.main-row:hover {
  background-color: #e9f5ff;
}
.detail-cell {
  background-color: #f1f5f9;
  padding: 15px;
  border-radius: 8px;
  box-shadow: inset 0 0 5px rgba(0,0,0,0.05);
  font-size: 0.9rem;
}
.detail-section {
  margin-bottom: 10px;
}
.detail-title {
  font-weight: bold;
  color: #0d6efd;
  margin-bottom: 5px;
  border-bottom: 1px solid #dee2e6;
  padding-bottom: 3px;
}
.toggle-icon {
  display: inline-block;
  width: 20px;
  font-weight: bold;
  margin-right: 8px;
  transition: transform 0.2s;
}
.toggle-icon.rotate {
  transform: rotate(45deg);
}
</style>

<div class="container mt-3">
  <div class="d-flex justify-content-between align-items-center mb-3">
    <h6 class="m-0">Diagnósticos registrados</h6>
    <button class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#modalDiagnostico">
      ➕ Agregar diagnóstico
    </button>
  </div>

  <?php if ($resDiag && $resDiag->num_rows > 0): ?>
  <div class="table-responsive">
    <table class="table table-striped table-bordered table-sm align-middle">
      <thead class="table-success">
        <tr>
         
          <th>Fecha</th>
          <th>Diagnóstico</th>
          <th>Origen</th>
          <th>Entidad</th>
          <th>Tipo de Evento</th>
        </tr>
      </thead>
      <tbody>
        <?php $counter = 0; ?>
        <?php while($d = $resDiag->fetch_assoc()): $counter++; ?>
        <tr class="main-row" data-bs-toggle="collapse" data-bs-target="#detail<?=$counter?>" aria-expanded="false">
         
          <td><?=htmlspecialchars($d['fecha'])?></td>
          <td><?=htmlspecialchars($d['diagnostico_descripcion'] ?? $d['codigo_diagnostico'])?></td>
          <td><?=htmlspecialchars($d['origen'])?></td>
          <td><?=htmlspecialchars($d['entidad'])?></td>
          <td><?=htmlspecialchars($d['tipo_evento'])?></td>
        </tr>
        <?php endwhile; ?>
      </tbody>
    </table>
  </div>
  <?php else: ?>
    <div class="alert alert-warning p-2">No hay diagnósticos registrados para este paciente.</div>
  <?php endif; ?>
</div>

<!-- MODAL NUEVO DIAGNÓSTICO -->
<div class="modal fade" id="modalDiagnostico" tabindex="-1">
  <div class="modal-dialog modal-lg">
    <form class="modal-content" id="formDiagnostico">
      <div class="modal-header bg-success text-white">
        <h5 class="modal-title">Agregar Diagnóstico</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <div class="modal-body">
        <input type="hidden" name="cedula" value="<?=$cedula?>">
        <div class="row g-3">

          <div class="col-md-4">
            <label class="form-label">Fecha</label>
            <input type="datetime-local" name="fecha" class="form-control form-control-sm" required>
          </div>

          <div class="col-md-8">
            <label class="form-label">Diagnóstico</label>
            <select name="codigo_diagnostico" class="form-control form-control-sm" required>
              <option value="">Seleccione diagnóstico</option>
              <?php
              $res = $conn->query("SELECT id, descripcion FROM diagnostico ORDER BY descripcion ASC");
              while($diag = $res->fetch_assoc()){
                  echo "<option value='{$diag['id']}'>{$diag['descripcion']}</option>";
              }
              ?>
            </select>
          </div>

          <div class="col-md-6">
            <label class="form-label">Origen</label>
            <input type="text" name="origen" class="form-control form-control-sm">
          </div>

          <div class="col-md-6">
            <label class="form-label">Entidad</label>
            <select name="Entidad" class="form-control form-control-sm" required>
              <option value="">Seleccione...</option>
              <option value="ARL">ARL</option>
              <option value="EPS">EPS</option>
              <option value="MEDICO EMPRESA">MÉDICO EMPRESA</option>
            </select>
          </div>

          <div class="col-md-6">
            <label class="form-label">Tipo de Evento</label>
            <select name="tipo_evento" class="form-control form-control-sm" required>
              <option value="">Seleccione...</option>
              <option value="AT">AT</option>
              <option value="EL">EL</option>
              <option value="EC">EC</option>
              <option value="ATRANSITO">ATRÁNSITO</option>
            </select>
          </div>

        </div>
      </div>

      <div class="modal-footer">
        <button type="submit" class="btn btn-success btn-sm">Guardar</button>
        <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Cancelar</button>
      </div>
    </form>
  </div>
</div>

<script>
// Toggle icon +/-
document.querySelectorAll('.main-row').forEach(function(row){
  row.addEventListener('click', function(){
    const icon = this.querySelector('.toggle-icon');
    icon.classList.toggle('rotate');
  });
});

// Enviar formulario por AJAX
document.getElementById('formDiagnostico').addEventListener('submit', function(e){
  e.preventDefault();
  const formData = new FormData(this);

  fetch('pacientes/guardar_diagnostico.php', {
    method: 'POST',
    body: formData
  })
  .then(r => r.text())
  .then(resp => {
    alert(resp);
    location.reload();
  })
  .catch(err => alert('Error al guardar diagnóstico: ' + err));
});
</script>