<?php
require_once __DIR__ . '/../includes/functions.php';
requireAdmin();
require __DIR__ . '/includes/ui.php';
$pageTitle = 'Navigation Bar';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrfVerify($_POST['csrf_token'] ?? null)) {
$action = $_POST['action'] ?? '';
if ($action === 'save') {
$id = (int) ($_POST['id'] ?? 0);
$data = [
trim($_POST['label'] ?? ''),
trim($_POST['url'] ?? ''),
(int) ($_POST['sort_order'] ?? 0),
isset($_POST['open_in_new_tab']) ? 1 : 0,
isset($_POST['is_active']) ? 1 : 0,
];
if ($id > 0) {
db()->prepare('UPDATE nav_links SET label=?, url=?, sort_order=?, open_in_new_tab=?, is_active=? WHERE id=?')->execute([...$data, $id]);
} else {
db()->prepare('INSERT INTO nav_links (label, url, sort_order, open_in_new_tab, is_active) VALUES (?,?,?,?,?)')->execute($data);
}
flash('success', 'Navigation link saved.');
} elseif ($action === 'delete') {
db()->prepare('DELETE FROM nav_links WHERE id = ?')->execute([(int) ($_POST['id'] ?? 0)]);
flash('success', 'Navigation link deleted.');
}
redirect('nav-links.php');
}
require __DIR__ . '/includes/admin-header.php';
adminFlashBox();
$links = db()->query('SELECT * FROM nav_links ORDER BY sort_order ASC, id ASC')->fetchAll();
$editId = (int) ($_GET['edit'] ?? 0);
$editing = $editId ? array_values(array_filter($links, fn($l) => (int) $l['id'] === $editId))[0] ?? null : null;
?>
<div class="grid lg:grid-cols-3 gap-6">
<div class="lg:col-span-2 bg-white rounded-2xl border border-gray-100 overflow-hidden">
<table class="w-full text-sm text-left">
<thead class="bg-gray-50 text-xs uppercase text-gray-500"><tr><th class="px-4 py-3">Order</th><th class="px-4 py-3">Label</th><th class="px-4 py-3">URL</th><th class="px-4 py-3">Active</th><th class="px-4 py-3"></th></tr></thead>
<tbody class="divide-y divide-gray-100">
<?php foreach ($links as $l): ?>
<tr>
<td class="px-4 py-3"><?= (int) $l['sort_order'] ?></td>
<td class="px-4 py-3 font-medium"><?= e($l['label']) ?></td>
<td class="px-4 py-3 text-gray-500"><?= e($l['url']) ?></td>
<td class="px-4 py-3"><?= $l['is_active'] ? '<span class="text-green-600">Yes</span>' : '<span class="text-gray-400">No</span>' ?></td>
<td class="px-4 py-3 text-right space-x-3">
<a href="?edit=<?= (int) $l['id'] ?>" class="text-primary font-semibold">Edit</a>
<form method="post" class="inline" onsubmit="return confirm('Delete this nav link?');">
<input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>" />
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?= (int) $l['id'] ?>" />
<button class="text-red-600 font-semibold">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="bg-white rounded-2xl border border-gray-100 p-6 h-fit">
<h2 class="font-heading font-semibold mb-4"><?= $editing ? 'Edit Link' : 'Add Link' ?></h2>
<form method="post">
<input type="hidden" name="csrf_token" value="<?= e(csrfToken()) ?>" />
<input type="hidden" name="action" value="save" />
<input type="hidden" name="id" value="<?= (int) ($editing['id'] ?? 0) ?>" />
<?php inputField('Label', 'label', $editing['label'] ?? '', 'text', true); ?>
<?php inputField('URL (e.g. services.php)', 'url', $editing['url'] ?? '', 'text', true); ?>
<?php numberField('Sort Order', 'sort_order', $editing['sort_order'] ?? 0); ?>
<?php checkboxField('Open in new tab', 'open_in_new_tab', !empty($editing['open_in_new_tab'])); ?>
<?php checkboxField('Active', 'is_active', $editing ? !empty($editing['is_active']) : true); ?>
<button class="btn-primary-brand bg-primary text-white px-5 py-2.5 rounded-lg font-semibold w-full"><?= $editing ? 'Update' : 'Add Link' ?></button>
<?php if ($editing): ?><a href="nav-links.php" class="block text-center text-sm text-gray-500 mt-3">Cancel edit</a><?php endif; ?>
</form>
</div>
</div>
<?php require __DIR__ . '/includes/admin-footer.php'; ?>