35286-vm/partials/admin_dashboard.php
Flatlogic Bot 6e132e0b38 0.2
2025-10-27 21:34:11 +00:00

169 lines
8.5 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
try {
$pdo = db();
// --- Pagination Settings ---
$limit = 10; // Subscriptions per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;
// --- Filtering and Searching ---
$base_sql = "FROM subscriptions";
$params = [];
$where_clauses = [];
if (!empty($_GET['status'])) {
$where_clauses[] = "status = :status";
$params[':status'] = $_GET['status'];
}
if (!empty($_GET['search'])) {
$where_clauses[] = "(fullName LIKE :search OR email LIKE :search)";
$params[':search'] = '%' . $_GET['search'] . '%';
}
if (!empty($where_clauses)) {
$base_sql .= " WHERE " . implode(' AND ', $where_clauses);
}
// --- Get Total Count for Pagination ---
$count_stmt = $pdo->prepare("SELECT COUNT(id) " . $base_sql);
$count_stmt->execute($params);
$total_subscriptions = $count_stmt->fetchColumn();
$total_pages = ceil($total_subscriptions / $limit);
// --- Get Subscriptions for Current Page ---
$subscriptions_sql = "SELECT id, fullName, email, insuranceType, status, created_at " . $base_sql . " ORDER BY created_at DESC LIMIT :limit OFFSET :offset";
$stmt = $pdo->prepare($subscriptions_sql);
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
foreach ($params as $key => &$val) {
$stmt->bindParam($key, $val);
}
$stmt->execute();
$subscriptions = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Database error: " . $e->getMessage());
}
?>
<div class="row mb-4">
<div class="col-12">
<div class="card shadow-sm border-0" style="border-radius: 0.75rem;">
<div class="card-body p-4">
<h2 class="card-title h5 fw-bold">Application Trends</h2>
<p class="text-muted small">New applications over the last 30 days.</p>
<canvas id="applicationsChart"></canvas>
</div>
</div>
</div>
</div>
<div class="card shadow-sm border-0" style="border-radius: 0.75rem;">
<div class="card-body p-4">
<h2 class="card-title h5 fw-bold">All Applications</h2>
<p class="text-muted small">Manage all insurance applications.</p>
<!-- Filter and Search Form -->
<form action="admin.php" method="GET" class="row g-3 align-items-center mb-4">
<div class="col-md-5">
<input type="text" name="search" class="form-control" placeholder="Search by name or email..." value="<?php echo htmlspecialchars($_GET['search'] ?? ''); ?>">
</div>
<div class="col-md-4">
<select name="status" class="form-select">
<option value="">All Statuses</option>
<option value="Pending" <?php echo (($_GET['status'] ?? '') === 'Pending') ? 'selected' : ''; ?>>Pending</option>
<option value="Approved" <?php echo (($_GET['status'] ?? '') === 'Approved') ? 'selected' : ''; ?>>Approved</option>
<option value="Rejected" <?php echo (($_GET['status'] ?? '') === 'Rejected') ? 'selected' : ''; ?>>Rejected</option>
</select>
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-primary-modern w-100">Filter</button>
<a href="admin.php" class="btn btn-secondary w-100 mt-2">Clear</a>
</div>
</form>
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead class="table-light">
<tr>
<th scope="col">ID</th>
<th scope="col">Full Name</th>
<th scope="col">Email</th>
<th scope="col">Insurance Type</th>
<th scope="col">Status</th>
<th scope="col">Date</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($subscriptions)): ?>
<tr>
<td colspan="7" class="text-center text-muted py-4">No applications found.</td>
</tr>
<?php else: ?>
<?php foreach ($subscriptions as $sub): ?>
<tr>
<td><?php echo htmlspecialchars($sub['id']); ?></td>
<td><?php echo htmlspecialchars($sub['fullName']); ?></td>
<td><?php echo htmlspecialchars($sub['email']); ?></td>
<td><?php echo htmlspecialchars($sub['insuranceType']); ?></td>
<td>
<?php
$status = $sub['status'];
$badge_class = 'bg-secondary'; // Default
if ($status === 'Pending') {
$badge_class = 'bg-warning text-dark';
} elseif ($status === 'Approved') {
$badge_class = 'bg-success';
} elseif ($status === 'Rejected') {
$badge_class = 'bg-danger';
}
?>
<span class="badge <?php echo $badge_class; ?>"><?php echo htmlspecialchars($status); ?></span>
</td>
<td><?php echo date("Y-m-d", strtotime($sub['created_at'])); ?></td>
<td>
<div class="dropdown">
<button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" id="dropdownMenuButton-<?php echo $sub['id']; ?>" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-gear"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton-<?php echo $sub['id']; ?>">
<li><a class="dropdown-item" href="admin-details.php?id=<?php echo $sub['id']; ?>">Details</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#" onclick="updateStatus(<?php echo $sub['id']; ?>, 'Pending')">Set Pending</a></li>
<li><a class="dropdown-item" href="#" onclick="updateStatus(<?php echo $sub['id']; ?>, 'Approved')">Set Approved</a></li>
<li><a class="dropdown-item" href="#" onclick="updateStatus(<?php echo $sub['id']; ?>, 'Rejected')">Set Rejected</a></li>
</ul>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<!-- Pagination -->
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<?php if ($page > 1): ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $page - 1; ?>&search=<?php echo htmlspecialchars($_GET['search'] ?? ''); ?>&status=<?php echo htmlspecialchars($_GET['status'] ?? ''); ?>">Previous</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; ?>&search=<?php echo htmlspecialchars($_GET['search'] ?? ''); ?>&status=<?php echo htmlspecialchars($_GET['status'] ?? ''); ?>"><?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; ?>&search=<?php echo htmlspecialchars($_GET['search'] ?? ''); ?>&status=<?php echo htmlspecialchars($_GET['status'] ?? ''); ?>">Next</a></li>
<?php endif; ?>
</ul>
</nav>
</div>
</div>