Files
gestoria-lp/admin/tramites.php
2026-03-02 00:30:03 +00:00

225 lines
9.0 KiB
PHP

<?php
$pageTitle = 'Tr&aacute;mites';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/includes/admin-header.php';
$db = getDB();
// Label maps
$tipoLabels = [
'visa' => 'Visa',
'sentri' => 'Sentri/Global',
'pasaporte' => 'Pasaporte',
'adelanto_cita' => 'Adelanto Cita',
'doble_nacionalidad' => 'Doble Nacionalidad',
];
$estadoLabels = [
'nuevo' => 'Nuevo',
'en_proceso' => 'En Proceso',
'en_revision' => 'En Revisi&oacute;n',
'completado' => 'Completado',
'cancelado' => 'Cancelado',
];
// Filters & Pagination
$q = trim($_GET['q'] ?? '');
$fEstado = trim($_GET['estado'] ?? '');
$fTipo = trim($_GET['tipo'] ?? '');
$page = max(1, (int)($_GET['page'] ?? 1));
$perPage = 15;
$offset = ($page - 1) * $perPage;
// Validate filter values against allowed enums
$validEstados = array_keys($estadoLabels);
$validTipos = array_keys($tipoLabels);
if ($fEstado !== '' && !in_array($fEstado, $validEstados, true)) $fEstado = '';
if ($fTipo !== '' && !in_array($fTipo, $validTipos, true)) $fTipo = '';
// Build WHERE clause dynamically
$conditions = [];
$params = [];
if ($q !== '') {
$conditions[] = 'c.nombre LIKE ?';
$params[] = '%' . $q . '%';
}
if ($fEstado !== '') {
$conditions[] = 't.estado = ?';
$params[] = $fEstado;
}
if ($fTipo !== '') {
$conditions[] = 't.tipo = ?';
$params[] = $fTipo;
}
$where = '';
if (!empty($conditions)) {
$where = 'WHERE ' . implode(' AND ', $conditions);
}
// Count total
$countSql = "SELECT COUNT(*) FROM tramites t LEFT JOIN clientes c ON t.cliente_id = c.id $where";
$countStmt = $db->prepare($countSql);
$countStmt->execute($params);
$totalTramites = $countStmt->fetchColumn();
$totalPages = max(1, (int)ceil($totalTramites / $perPage));
// Ensure page is within range
if ($page > $totalPages) $page = $totalPages;
// Fetch tramites with client name
$sql = "SELECT t.*, c.nombre AS cliente_nombre
FROM tramites t
LEFT JOIN clientes c ON t.cliente_id = c.id
$where
ORDER BY t.created_at DESC
LIMIT $perPage OFFSET $offset";
$stmt = $db->prepare($sql);
$stmt->execute($params);
$tramites = $stmt->fetchAll();
// Check if any filters are active
$hasFilters = ($q !== '' || $fEstado !== '' || $fTipo !== '');
?>
<div class="admin-content__header">
<h1><i class="fas fa-file-alt"></i> Tr&aacute;mites</h1>
<p>Gesti&oacute;n de procesos y tr&aacute;mites</p>
</div>
<!-- Toolbar: Filters + New Tramite -->
<div class="toolbar">
<div class="toolbar__left">
<form method="GET" class="search-bar search-bar--wide" action="tramites.php" style="display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap;">
<div style="position: relative;">
<i class="fas fa-search search-bar__icon"></i>
<input type="text" name="q" class="search-bar__input" placeholder="Buscar por cliente..." value="<?= htmlspecialchars($q) ?>">
</div>
<select name="estado" class="form-control" style="width: auto; min-width: 140px;">
<option value="">-- Estado --</option>
<?php foreach ($estadoLabels as $val => $label): ?>
<option value="<?= htmlspecialchars($val) ?>" <?= $fEstado === $val ? 'selected' : '' ?>>
<?= $label ?>
</option>
<?php endforeach; ?>
</select>
<select name="tipo" class="form-control" style="width: auto; min-width: 160px;">
<option value="">-- Tipo --</option>
<?php foreach ($tipoLabels as $val => $label): ?>
<option value="<?= htmlspecialchars($val) ?>" <?= $fTipo === $val ? 'selected' : '' ?>>
<?= htmlspecialchars($label) ?>
</option>
<?php endforeach; ?>
</select>
<button type="submit" class="btn btn--sm btn--secondary">
<i class="fas fa-filter"></i> Filtrar
</button>
</form>
<?php if ($hasFilters): ?>
<a href="tramites.php" class="btn btn--sm btn--secondary" style="margin-left: 0.5rem;">
<i class="fas fa-times"></i> Limpiar
</a>
<?php endif; ?>
</div>
<div class="toolbar__right">
<a href="tramite-detalle.php" class="btn btn--primary">
<i class="fas fa-plus"></i> Nuevo Tr&aacute;mite
</a>
</div>
</div>
<!-- Tramites Table -->
<div class="card">
<div class="card__body">
<?php if (empty($tramites)): ?>
<div class="table-empty">
<i class="fas fa-file-alt"></i>
<p><?= $hasFilters ? 'No se encontraron tr&aacute;mites con esos filtros.' : 'No hay tr&aacute;mites registrados a&uacute;n.' ?></p>
<?php if ($hasFilters): ?>
<a href="tramites.php" class="btn btn--sm btn--outline">Ver todos</a>
<?php endif; ?>
</div>
<?php else: ?>
<div class="table-responsive">
<table class="admin-table">
<thead>
<tr>
<th>Cliente</th>
<th>Tipo</th>
<th>Estado</th>
<th>Fecha Solicitud</th>
<th>Fecha Cita</th>
<th>Precio</th>
<th>Acci&oacute;n</th>
</tr>
</thead>
<tbody>
<?php foreach ($tramites as $t): ?>
<tr>
<td>
<strong><?= htmlspecialchars($t['cliente_nombre'] ?? 'Sin cliente') ?></strong>
</td>
<td><?= htmlspecialchars($tipoLabels[$t['tipo']] ?? $t['tipo']) ?></td>
<td>
<span class="badge badge--<?= htmlspecialchars($t['estado']) ?>">
<?= $estadoLabels[$t['estado']] ?? htmlspecialchars($t['estado']) ?>
</span>
</td>
<td><?= $t['fecha_solicitud'] ? date('d/m/Y', strtotime($t['fecha_solicitud'])) : '-' ?></td>
<td><?= $t['fecha_cita'] ? date('d/m/Y', strtotime($t['fecha_cita'])) : '-' ?></td>
<td><?= $t['precio'] ? '$' . number_format((float)$t['precio'], 2) : '-' ?></td>
<td>
<a href="tramite-detalle.php?id=<?= (int)$t['id'] ?>" class="btn btn--sm btn--primary">
<i class="fas fa-eye"></i> Ver
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!-- Pagination -->
<?php if ($totalPages > 1): ?>
<div class="pagination">
<?php
$queryParams = '';
if ($q !== '') $queryParams .= '&q=' . urlencode($q);
if ($fEstado !== '') $queryParams .= '&estado=' . urlencode($fEstado);
if ($fTipo !== '') $queryParams .= '&tipo=' . urlencode($fTipo);
?>
<a href="?page=1<?= $queryParams ?>" class="pagination__link <?= $page <= 1 ? 'pagination__link--disabled' : '' ?>">
<i class="fas fa-angle-double-left"></i>
</a>
<a href="?page=<?= $page - 1 ?><?= $queryParams ?>" class="pagination__link <?= $page <= 1 ? 'pagination__link--disabled' : '' ?>">
<i class="fas fa-angle-left"></i>
</a>
<?php
$start = max(1, $page - 2);
$end = min($totalPages, $page + 2);
for ($i = $start; $i <= $end; $i++):
?>
<a href="?page=<?= $i ?><?= $queryParams ?>" class="pagination__link <?= $i === $page ? 'pagination__link--active' : '' ?>">
<?= $i ?>
</a>
<?php endfor; ?>
<a href="?page=<?= $page + 1 ?><?= $queryParams ?>" class="pagination__link <?= $page >= $totalPages ? 'pagination__link--disabled' : '' ?>">
<i class="fas fa-angle-right"></i>
</a>
<a href="?page=<?= $totalPages ?><?= $queryParams ?>" class="pagination__link <?= $page >= $totalPages ? 'pagination__link--disabled' : '' ?>">
<i class="fas fa-angle-double-right"></i>
</a>
<span class="pagination__info">
P&aacute;gina <?= $page ?> de <?= $totalPages ?> (<?= $totalTramites ?> tr&aacute;mites)
</span>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
<?php require_once __DIR__ . '/includes/admin-footer.php'; ?>