<?php
session_start();
require __DIR__ . '/includes/admin_lib.php';
$pageTitle = 'Navigation Manager — Thunder Mountain Radio';

$file = __DIR__ . '/../data/nav_links.json';
$links = smurf_read_json($file, []);
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);
  smurf_write_json($file, array_values($links));
  smurf_redirect_self(['deleted'=>1], ['action','i']);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $CAN_EDIT) {
  $mode = $_POST['mode'] ?? 'create';
  $editIndex = (int)($_POST['edit_index'] ?? -1);
  $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 ($mode === 'update' && isset($links[$editIndex])) $links[$editIndex] = $row; else $links[] = $row;
    smurf_write_json($file, array_values($links));
    smurf_redirect_self(['saved'=>1], ['action','i']);
  }
}
include __DIR__ . '/includes/admin_header.php';
?>
<section class="content-card">
  <h1 class="admin-title">🧭 Navigation Manager</h1>
  <p class="admin-note">Use this to add extra navigation links without editing code. Core links like Home, News, Schedule and Chat still use their own toggles on their matching admin pages.</p>
  <?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;"><?= $action === 'edit' ? 'Edit Link' : 'Add Extra Link' ?></h3>
      <form method="post" class="form">
        <input type="hidden" name="mode" value="<?= $action === 'edit' ? 'update' : 'create' ?>">
        <input type="hidden" name="edit_index" value="<?= (int)$i ?>">
        <div class="admin-form-grid">
          <div>
            <label>Label</label>
            <input class="input-field" name="label" value="<?= smurf_h($edit['label'] ?? '') ?>" placeholder="Sponsor Store">
          </div>
          <div>
            <label>URL</label>
            <input class="input-field" name="url" value="<?= smurf_h($edit['url'] ?? '') ?>" placeholder="https://...">
          </div>
          <div>
            <label>Icon / Emoji</label>
            <input class="input-field" name="icon" value="<?= smurf_h($edit['icon'] ?? '🔗') ?>" placeholder="🔗">
          </div>
          <div>
            <label style="display:flex;gap:10px;align-items:center;margin-top:30px;"><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-top:8px;"><input type="checkbox" name="enabled" <?= array_key_exists('enabled',$edit) ? (!empty($edit['enabled']) ? 'checked':'') : 'checked' ?>> Enabled</label>
          </div>
        </div>
        <div class="page-action-row">
          <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; ?>
        </div>
      </form>
    </section>
    <section class="content-card" style="text-align:left;">
      <h3 style="margin-top:0;">Current extra links</h3>
      <?php if (empty($links)): ?>
        <p>No extra links added yet.</p>
      <?php else: ?>
        <div class="nav-manager-list">
          <?php foreach ($links as $idx => $link): ?>
            <div class="nav-link-row">
              <div><strong><?= smurf_h($link['icon'] ?? '🔗') ?> <?= smurf_h($link['label'] ?? '') ?></strong></div>
              <div style="opacity:.85;word-break:break-all;"><?= smurf_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; ?>
        </div>
      <?php endif; ?>
    </section>
  </div>
</section>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
