<?php


if($_SESSION["tipo_usuario"] == "Inventario"){

  echo '<script>

    window.location = "inicio";

  </script>';

  return;

}

?>

<style>
  /* Estilo para la tabla de Auditoría */
.tablaAuditoria thead th {
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 1px;
    font-weight: 700;
    border: none;
}

.tablaAuditoria tbody tr {
    transition: all 0.2s ease;
    border-bottom: 1px solid #f4f6f9;
}

.tablaAuditoria tbody tr:hover {
    background-color: #f8f9fa !important;
    transform: scale(1.002);
    box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1);
    z-index: 10;
}

.badge {
    font-weight: 600;
    font-size: 0.75rem;
}
</style>

<div class="content-wrapper">
  <section class="content-header">
    <div class="container-fluid">
      <div class="row mb-2">
        <div class="col-sm-6">
          <h1 class="m-0 text-dark"><i class="fas fa-shield-alt mr-2"></i> Registro de Auditoría</h1>
        </div>
      </div>
    </div>
  </section>

  <section class="content">
    <div class="container-fluid">
      
      <div class="card card-outline card-primary shadow">
        
        <div class="card-header bg-white">
          <div class="d-flex justify-content-between align-items-center">
            <div>
              <button type="button" class="btn btn-outline-primary btn-sm rounded-pill" id="daterange-btn-auditoria">
                <i class="far fa-calendar-alt mr-1"></i> 
                <span>
                  <?php
                    echo (isset($_GET["fechaInicial"])) ? $_GET["fechaInicial"]." - ".$_GET["fechaFinal"] : "Filtrar por fecha";
                  ?>
                </span>
                <i class="fas fa-caret-down ml-1"></i>
              </button>

              <?php if(isset($_GET["fechaInicial"])): ?>
                <a href="auditoria" class="btn btn-link btn-sm text-danger"><i class="fas fa-eraser"></i> Borrar Filtro</a>
              <?php endif; ?>
            </div>

            <div class="card-tools">
               <span class="badge badge-info">Historial Reciente</span>
            </div>
          </div>
        </div>

        <div class="card-body p-0"> <table class="table table-hover table-borderless dt-responsive tablaAuditoria" width="100%">
            
            <thead class="thead-light">
              <tr>
                <th class="pl-4">#</th>
                <th>Usuario</th>
                <th>Ámbito / Módulo</th>
                <th>Acción</th>
                <th>Detalle de la Actividad</th>
                <th>Fecha y Hora</th>
              </tr>
            </thead>

            <tbody>
              <?php
                $fechaInicial = isset($_GET["fechaInicial"]) ? $_GET["fechaInicial"] : null;
                $fechaFinal = isset($_GET["fechaFinal"]) ? $_GET["fechaFinal"] : null;

                $auditoria = ControladorAuditoria::ctrMostrarAuditoria($fechaInicial, $fechaFinal);

                foreach ($auditoria as $key => $value) {

                  // Colores de badges modernos (Bootstrap 4 pill style)
                  $claseBadge = "badge-secondary";
                  $icono = "fa-info-circle";

                  switch($value["accion"]){
                    case "VISUALIZAR": $claseBadge = "badge-info"; $icono = "fa-eye"; break;
                    case "GUARDAR":
                    case "INSERTAR":   $claseBadge = "badge-success"; $icono = "fa-plus-circle"; break;
                    case "ACTUALIZAR":
                    case "EDITAR":     $claseBadge = "badge-warning"; $icono = "fa-edit"; break;
                    case "ELIMINAR":   $claseBadge = "badge-danger"; $icono = "fa-trash-alt"; break;
                  }

                  echo '<tr>
                          <td class="pl-4 align-middle">'.($key+1).'</td>
                          <td class="align-middle">
                            <div class="d-flex align-items-center">
                              <div class="img-circle bg-gray-light mr-2 d-flex justify-content-center align-items-center" style="width:30px; height:30px">
                                <i class="fas fa-user text-muted small"></i>
                              </div>
                              <span class="text-bold text-primary" style="font-size: 0.9rem;">'.strtoupper($value["usuario"]).'</span>
                            </div>
                          </td>
                          <td class="align-middle"><span class="text-muted">'.$value["modulo"].'</span></td>
                          <td class="align-middle"><span class="badge '.$claseBadge.' px-3 py-2 rounded-pill shadow-sm"><i class="fas '.$icono.' mr-1"></i> '.$value["accion"].'</span></td>
                          <td class="align-middle" style="max-width: 300px;">
                            <p class="mb-0 text-wrap" style="font-size: 0.85rem; line-height: 1.2;">'.$value["descripcion"].'</p>
                          </td>
                          <td class="align-middle">
                            <span class="text-dark d-block">'.date("d/m/Y", strtotime($value["fecha_hora"])).'</span>
                            <small class="text-muted"><i class="far fa-clock"></i> '.date("h:i A", strtotime($value["fecha_hora"])).'</small>
                          </td>
                        </tr>';
                }
              ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </section>
</div>

<script src="vistas/js/auditoria.js"></script>
  <script>
      
     var elemento = document.getElementById("mantenimiento");
      elemento.className += " active";

      var elemento = document.getElementById("mante");
      elemento.className += " menu-open";

      var elemento = document.getElementById("auditoria");
      elemento.className += " active";
</script>
