CrestRoad/website/admin/admins.php

113 lines
4.1 KiB
PHP

<?php
require_once __DIR__ . '/../includes/auth.php';
require_login();
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = trim($_POST['email'] ?? '');
$password = trim($_POST['password'] ?? '');
if ($email === '' || $password === '') {
$error = 'Email and password are required.';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = 'Please enter a valid email.';
} else {
try {
create_admin($email, $password);
$success = 'Admin added successfully.';
} catch (Throwable $e) {
$error = 'Could not add admin (maybe email already exists).';
}
}
}
$pdo = db();
$admins = $pdo->query("SELECT email, created_at FROM admins ORDER BY created_at DESC");
$admins = is_array($admins) ? $admins : [];
$assetPathPrefix = '../';
$pageTitle = 'Admins';
$currentPage = '';
$extraStyles = <<<CSS
<style>
body { background: #f6faf7; }
.admin-shell { padding: 40px 0; }
.table { width: 100%; border-collapse: collapse; margin-top: 20px; }
.table th, .table td { text-align: left; padding: 12px 10px; border-bottom: 1px solid #e1ede7; }
.table th { color: #0f4333; }
form input { width: 100%; padding: 12px 14px; border-radius: 10px; border: 1px solid #d8e7e0; font-size: 15px; margin-top: 8px; }
form label { font-weight: 700; color: #0f4333; display: block; margin-top: 12px; }
</style>
CSS;
require __DIR__ . '/../includes/admin-header.php';
?>
<main class="admin-shell">
<div class="container">
<div class="topbar" style="display:flex; justify-content:space-between; align-items:center; margin-bottom:16px;">
<div>
<p class="eyebrow">Admins</p>
<h2>Manage administrator accounts</h2>
</div>
</div>
<div class="admin-nav">
<a href="/admin/tutorials.php">Tutorials</a>
<a href="/admin/sessions.php">Upcoming Sessions</a>
<a href="/admin/contacts.php">Contact Entries</a>
<a href="/admin/newsletter.php">Newsletter</a>
<a href="/admin/admins.php">Admins</a>
<a href="/admin/logout.php">Logout (<?php echo htmlspecialchars(current_admin_email() ?? ''); ?>)</a>
</div>
<?php if ($success): ?>
<div class="alert good"><?php echo htmlspecialchars($success); ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert bad"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<div class="card">
<h3>Add Admin</h3>
<form method="post">
<label for="email">Email</label>
<input type="email" id="email" name="email" required placeholder="admin@example.com">
<label for="password">Password</label>
<input type="password" id="password" name="password" required placeholder="Choose a secure password">
<div style="margin-top:16px;">
<button type="submit" class="btn primary">Add admin</button>
</div>
</form>
</div>
<div class="card" style="margin-top:20px;">
<h3>Existing Admins</h3>
<?php if (empty($admins)): ?>
<p class="body muted">No admins found.</p>
<?php else: ?>
<table class="table">
<thead>
<tr>
<th>Email</th>
<th>Created</th>
</tr>
</thead>
<tbody>
<?php foreach ($admins as $admin): ?>
<tr>
<td><?php echo htmlspecialchars($admin['email']); ?></td>
<td><?php echo htmlspecialchars($admin['created_at']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
</main>
<?php require __DIR__ . '/../includes/admin-footer.php'; ?>