161 lines
7.1 KiB
PHP
161 lines
7.1 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION["user_id"])) {
|
|
header("Location: auth/login.php");
|
|
exit();
|
|
}
|
|
require_once 'includes/header.php';
|
|
|
|
// Search term
|
|
$search_term = isset($_GET['search']) ? trim($_GET['search']) : '';
|
|
|
|
// Pagination settings
|
|
$items_per_page = 10;
|
|
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
|
|
$offset = ($page - 1) * $items_per_page;
|
|
|
|
$pdo = db();
|
|
|
|
// Base query
|
|
$sql_count = "SELECT COUNT(*) FROM usuarios";
|
|
$sql = "SELECT id, nombre, email, rol FROM usuarios";
|
|
|
|
$params = [];
|
|
|
|
// Apply search filter
|
|
if (!empty($search_term)) {
|
|
$sql_count .= " WHERE nombre LIKE :search OR email LIKE :search";
|
|
$sql .= " WHERE nombre LIKE :search OR email LIKE :search";
|
|
$params[':search'] = '%' . $search_term . '%';
|
|
}
|
|
|
|
// Get total number of items (filtered or not)
|
|
$total_stmt = $pdo->prepare($sql_count);
|
|
$total_stmt->execute($params);
|
|
$total_items = $total_stmt->fetchColumn();
|
|
$total_pages = ceil($total_items / $items_per_page);
|
|
|
|
// Add ordering and pagination to the main query
|
|
$sql .= " ORDER BY id DESC LIMIT :limit OFFSET :offset";
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
|
|
// Bind search param if it exists
|
|
if (!empty($search_term)) {
|
|
$stmt->bindValue(':search', $params[':search'], PDO::PARAM_STR);
|
|
}
|
|
|
|
$stmt->bindValue(':limit', $items_per_page, PDO::PARAM_INT);
|
|
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
$usuarios = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
?>
|
|
|
|
<div class="container-fluid">
|
|
|
|
<!-- Page Heading -->
|
|
<div class="d-sm-flex justify-content-between align-items-center mb-4">
|
|
<h3 class="text-dark mb-0">Gestión de Colaboradores</h3>
|
|
<?php if (isset($_SESSION['user_rol']) && $_SESSION['user_rol'] === 'Administrador General'): ?>
|
|
<a class="btn btn-primary btn-sm" role="button" href="agregar_colaborador.php">
|
|
<i class="fas fa-plus fa-sm text-white-50"></i> Agregar Colaborador
|
|
</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<!-- Success/Error Messages -->
|
|
<?php if (isset($_SESSION['success_message'])): ?>
|
|
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
|
<?php echo $_SESSION['success_message']; ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php unset($_SESSION['success_message']); ?>
|
|
<?php endif; ?>
|
|
<?php if (isset($_SESSION['error_message'])): ?>
|
|
<div class="alert alert-danger alert-dismissible fade show" role="alert">
|
|
<?php echo $_SESSION['error_message']; ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php unset($_SESSION['error_message']); ?>
|
|
<?php endif; ?>
|
|
|
|
<!-- Search Form -->
|
|
<div class="card shadow mb-4">
|
|
<div class="card-body">
|
|
<form action="colaboradores.php" method="get" class="form-inline">
|
|
<div class="input-group">
|
|
<input type="text" name="search" class="form-control" placeholder="Buscar por nombre o email..." value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>">
|
|
<button class="btn btn-primary" type="submit">Buscar</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card shadow">
|
|
<div class="card-header py-3">
|
|
<p class="text-primary m-0 fw-bold">Listado de Colaboradores</p>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria-describedby="dataTable_info">
|
|
<table class="table my-0" id="dataTable">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Nombre</th>
|
|
<th>Email</th>
|
|
<th>Rol</th>
|
|
<th>Acciones</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (count($usuarios) > 0): ?>
|
|
<?php foreach ($usuarios as $usuario): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($usuario['id']); ?></td>
|
|
<td><?php echo htmlspecialchars($usuario['nombre']); ?></td>
|
|
<td><?php echo htmlspecialchars($usuario['email']); ?></td>
|
|
<td><?php echo htmlspecialchars($usuario['rol']); ?></td>
|
|
<td>
|
|
<?php if (isset($_SESSION['user_rol']) && $_SESSION['user_rol'] === 'Administrador General'): ?>
|
|
<a href="editar_colaborador.php?id=<?php echo $usuario['id']; ?>" class="btn btn-warning btn-sm"><i class="fas fa-edit"></i> Editar</a>
|
|
<a href="eliminar_colaborador.php?id=<?php echo $usuario['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('¿Estás seguro de que quieres eliminar este colaborador?');"><i class="fas fa-trash"></i> Eliminar</a>
|
|
<?php else: ?>
|
|
<span class="text-muted">No tienes permisos</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php else: ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center">No hay colaboradores que coincidan con la búsqueda.</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- Pagination Controls -->
|
|
<div class="d-flex justify-content-center">
|
|
<nav>
|
|
<ul class="pagination">
|
|
<?php if ($page > 1): ?>
|
|
<li class="page-item"><a class="page-link" href="?page=<?php echo $page - 1; ?><?php echo isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : ''; ?>">Anterior</a></li>
|
|
<?php endif; ?>
|
|
|
|
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
|
|
<li class="page-item <?php echo ($i == $page) ? 'active' : ''; ?>">
|
|
<a class="page-link" href="?page=<?php echo $i; ?><?php echo isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : ''; ?>"><?php echo $i; ?></a>
|
|
</li>
|
|
<?php endfor; ?>
|
|
|
|
<?php if ($page < $total_pages): ?>
|
|
<li class="page-item"><a class="page-link" href="?page=<?php echo $page + 1; ?><?php echo isset($_GET['search']) ? '&search=' . htmlspecialchars($_GET['search']) : ''; ?>">Siguiente</a></li>
|
|
<?php endif; ?>
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|