
<?php
session_start();
require __DIR__ . '/auth.php';
require __DIR__ . '/includes/admin_lib.php';

require_once __DIR__ . '/../includes/stream_lib.php';
$settingsFile = __DIR__ . '/../data/settings.json';
$settings = smurf_load_settings($settingsFile);
$topFile = __DIR__ . '/../data/top50.json';
$stateFile = __DIR__ . '/../data/top50_state.json';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  if (!$CAN_EDIT) die('<h1>Access Denied</h1>');
  if (isset($_POST['save_top50'])) {
    $settings['top50_enabled'] = !empty($_POST['top50_enabled']);
    $settings['top50_show_in_nav'] = !empty($_POST['top50_show_in_nav']);
    smurf_save_settings($settingsFile, $settings);
    smurf_redirect_self(['saved'=>1]);
  }
  if (isset($_POST['reset_top50'])) {
    @file_put_contents($topFile, json_encode([], JSON_PRETTY_PRINT));
    @file_put_contents($stateFile, json_encode([], JSON_PRETTY_PRINT));
    smurf_redirect_self(['saved'=>1]);
  }
}
$current = smurf_top50_touch($topFile, $stateFile);
$rows = smurf_top50_sorted($topFile);
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';
?>
<section class="content-card">
  <h1 class="admin-title">🎵 Top 50</h1>
  <?php if (!empty($_GET['saved'])): ?><div class="msg success">Saved.</div><?php endif; ?>
  <div class="admin-wrap stack" style="margin-top:10px;">
    <section class="content-card" style="text-align:left;">
      <h3 style="margin-top:0;">Page Settings</h3>
      <form method="post" class="form">
        <input type="hidden" name="save_top50" value="1">
        <label style="display:block;margin:10px 0;"><input type="checkbox" name="top50_enabled" <?= !array_key_exists('top50_enabled',$settings) || !empty($settings['top50_enabled']) ? 'checked':''; ?>> Enable Top 50 page</label>
        <label style="display:block;margin:10px 0;"><input type="checkbox" name="top50_show_in_nav" <?= !array_key_exists('top50_show_in_nav',$settings) || !empty($settings['top50_show_in_nav']) ? 'checked':''; ?>> Show Top 50 in navigation</label>
        <?php if ($CAN_EDIT): ?><button class="admin-btn primary" type="submit">Save Settings</button><?php endif; ?>
      </form>
      <form method="post" onsubmit="return confirm('Reset the Top 50 chart?');" style="margin-top:14px;">
        <input type="hidden" name="reset_top50" value="1">
        <?php if ($CAN_EDIT): ?><button class="admin-btn" type="submit">Reset Chart Data</button><?php endif; ?>
      </form>
      <?php if ($current): ?><p style="margin-top:12px;"><strong>Currently tracking:</strong> <?= smurf_h($current) ?></p><?php endif; ?>
    </section>
    <section class="content-card" style="text-align:left;">
      <h3 style="margin-top:0;">Current Top 50</h3>
      <?php if (empty($rows)): ?><p>No chart data yet.</p><?php else: ?>
        <div class="top50-admin-list">
          <?php foreach ($rows as $idx => $row): ?>
            <div class="top50-admin-row"><strong>#<?= $idx+1 ?></strong> — <?= smurf_h($row['title'] ?? '') ?> <span style="opacity:.8;">(<?= (int)($row['count'] ?? 0) ?> tracked spins)</span></div>
          <?php endforeach; ?>
        </div>
      <?php endif; ?>
    </section>
  </div>
</section>
<?php include __DIR__ . '/includes/footer.php'; ?>
