
<?php
session_start(); require __DIR__ . '/includes/header.php';
$settingsFile = __DIR__ . '/../data/settings.json';
$linksFile = __DIR__ . '/../data/nav_links.json';
$settings = admin_read_json($settingsFile, oldies_read_settings());
$links = admin_read_json($linksFile, []); if (!is_array($links)) $links = [];
$action = $_GET['action'] ?? ''; $i = isset($_GET['i']) ? (int)$_GET['i'] : -1;
$edit = ($action === 'edit' && isset($links[$i])) ? $links[$i] : ['label'=>'','url'=>'','icon'=>'🔗','new_tab'=>1,'enabled'=>1];
if ($action === 'delete' && isset($links[$i]) && $CAN_EDIT) { array_splice($links, $i, 1); admin_write_json($linksFile, array_values($links)); admin_redirect_self(['deleted'=>1], ['action','i']); }
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $CAN_EDIT) {
  if (isset($_POST['save_core'])) {
    foreach (['events_show_in_nav','fans_show_in_nav','team_show_in_nav','player_show_in_nav','chat_show_in_nav','top50_show_in_nav'] as $k) $settings[$k] = !empty($_POST[$k]);
    admin_write_json($settingsFile, $settings); admin_redirect_self(['saved'=>1]);
  }
  if (isset($_POST['save_link'])) {
    $row = ['label'=>trim($_POST['label'] ?? ''),'url'=>trim($_POST['url'] ?? ''),'icon'=>trim($_POST['icon'] ?? '🔗'),'new_tab'=>!empty($_POST['new_tab']),'enabled'=>!empty($_POST['enabled'])];
    if ($row['label'] !== '' && $row['url'] !== '') { if (isset($_POST['edit_index']) && $_POST['edit_index'] !== '' && isset($links[(int)$_POST['edit_index']])) $links[(int)$_POST['edit_index']] = $row; else $links[] = $row; admin_write_json($linksFile, array_values($links)); }
    admin_redirect_self(['saved'=>1], ['action','i']);
  }
}
?>
<section class="content-card"><h1 class="admin-title">🧭 Navigation Manager</h1><?php if (!empty($_GET['saved'])): ?><div class="msg success">Navigation saved.</div><?php endif; ?><?php if (!empty($_GET['deleted'])): ?><div class="msg success">Navigation link deleted.</div><?php endif; ?>
<div class="page-builder-grid" style="margin-top:14px;">
<section class="content-card" style="text-align:left;"><h3 style="margin-top:0;">Core page toggles</h3><form method="post" class="form"><input type="hidden" name="save_core" value="1">
<?php foreach (['events_show_in_nav'=>'Events','fans_show_in_nav'=>'Fans','team_show_in_nav'=>'Join the Team','player_show_in_nav'=>'Popout Player','chat_show_in_nav'=>'Live Chat','top50_show_in_nav'=>'Top 50'] as $key => $label): ?>
<label style="display:flex;gap:10px;align-items:center;margin:10px 0;"><input type="checkbox" name="<?= admin_h($key) ?>" <?= !empty($settings[$key]) ? 'checked' : '' ?> <?= !$CAN_EDIT ? 'disabled' : '' ?>> <span><?= admin_h($label) ?></span></label>
<?php endforeach; ?><button class="admin-btn primary" type="submit" <?= !$CAN_EDIT ? 'disabled' : '' ?>>Save toggles</button></form></section>
<section class="content-card" style="text-align:left;"><h3 style="margin-top:0;"><?= $action === 'edit' ? 'Edit Link' : 'Add Extra Link' ?></h3><form method="post" class="form"><input type="hidden" name="save_link" value="1"><input type="hidden" name="edit_index" value="<?= $action === 'edit' ? (int)$i : '' ?>">
<label>Label</label><input class="input-field" name="label" value="<?= admin_h($edit['label'] ?? '') ?>"><label>URL</label><input class="input-field" name="url" value="<?= admin_h($edit['url'] ?? '') ?>"><label>Icon / Emoji</label><input class="input-field" name="icon" value="<?= admin_h($edit['icon'] ?? '🔗') ?>">
<label style="display:flex;gap:10px;align-items:center;margin:10px 0;"><input type="checkbox" name="new_tab" <?= !empty($edit['new_tab']) ? 'checked' : '' ?>> Open in new tab</label>
<label style="display:flex;gap:10px;align-items:center;margin:10px 0;"><input type="checkbox" name="enabled" <?= array_key_exists('enabled',$edit) ? (!empty($edit['enabled']) ? 'checked' : '') : 'checked' ?>> Enabled</label>
<button class="admin-btn primary" type="submit" <?= !$CAN_EDIT ? 'disabled' : '' ?>><?= $action === 'edit' ? 'Save Link' : 'Add Link' ?></button> <?php if ($action === 'edit'): ?><a class="admin-btn" href="/admin/navigation.php">Cancel</a><?php endif; ?></form></section>
</div>
<section class="content-card" style="text-align:left;margin-top:16px;"><h3 style="margin-top:0;">Current extra links</h3><?php if (empty($links)): ?><p>No extra links yet.</p><?php else: ?><?php foreach ($links as $idx => $link): ?><div class="nav-link-row"><div><strong><?= admin_h($link['icon'] ?? '🔗') ?> <?= admin_h($link['label'] ?? '') ?></strong></div><div style="opacity:.85;word-break:break-all;"><?= admin_h($link['url'] ?? '') ?></div><div><?= !empty($link['enabled']) ? 'Shown' : 'Hidden' ?><?= !empty($link['new_tab']) ? ' · New tab' : '' ?></div><div style="display:flex;gap:8px;justify-content:flex-end;"><a class="admin-btn" href="/admin/navigation.php?action=edit&i=<?= $idx ?>">Edit</a><?php if ($CAN_EDIT): ?><a class="admin-btn" href="/admin/navigation.php?action=delete&i=<?= $idx ?>" onclick="return confirm('Delete this link?');">Delete</a><?php endif; ?></div></div><?php endforeach; ?><?php endif; ?></section></section>
<?php include __DIR__ . '/includes/footer.php'; ?>
