
<?php
session_start();
$pageTitle = "Dashboard — Thunder Mountain Radio";
include __DIR__ . '/includes/admin_header.php';
$settings = smurf_read_json(__DIR__ . '/../data/settings.json', []);
$pages = smurf_read_json(__DIR__ . '/../data/pages.json', []);
$friends = smurf_read_json(__DIR__ . '/../data/friends.json', []);
$fans = smurf_read_json(__DIR__ . '/../data/fans.json', []);
$contactItems = smurf_parse_blocks(__DIR__ . '/../data/contact_messages.txt');
$appItems = smurf_parse_blocks(__DIR__ . '/../data/applications.txt');
$requestsRaw = trim((string)@file_get_contents(__DIR__ . '/../data/requests.txt'));
$requestsItems = [];
if ($requestsRaw !== '') {
  if (strpos($requestsRaw, '---') !== false && preg_match('/(^|\n)\w+\s*:/', $requestsRaw)) {
    $requestsItems = smurf_parse_blocks(__DIR__ . '/../data/requests.txt');
  } else {
    $lines = file(__DIR__ . '/../data/requests.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [];
    foreach ($lines as $line) { if (trim((string)$line) !== '') $requestsItems[] = ['_raw'=>$line]; }
  }
}
$chatEnabled = !empty($settings['chat_enabled']);
$eventsNav = array_key_exists('events_show_in_nav',$settings) ? !empty($settings['events_show_in_nav']) : true;
$playerNav = !empty($settings['player_show_in_nav']);
$top50Nav = !empty($settings['top50_show_in_nav']);
function dashCard($href,$emoji,$title,$desc,$foot='Open tool',$external=false){
  $attrs = $external ? ' target="_blank" rel="noopener"' : '';
  echo '<a class="admin-card-link" href="'.htmlspecialchars($href,ENT_QUOTES,'UTF-8').'"'.$attrs.'><div><div class="card-emoji">'.$emoji.'</div><div class="card-title">'.htmlspecialchars($title,ENT_QUOTES,'UTF-8').'</div><div class="card-desc">'.htmlspecialchars($desc,ENT_QUOTES,'UTF-8').'</div></div><div class="card-foot">'.htmlspecialchars($foot,ENT_QUOTES,'UTF-8').' →</div></a>';
}
?>
<section class="admin-hero-grid">
  <section class="admin-card-panel">
    <h3>Thunder Mountain Radio</h3>
    <p class="admin-stat-meta">Manage pages, requests, media, schedule, users, radio tools and site controls from one dashboard.</p>
    <div class="admin-stat-grid">
      <div class="admin-stat-card"><h3>Custom Pages</h3><div class="admin-stat-value"><?= is_array($pages) ? count($pages) : 0 ?></div><div class="admin-stat-meta">Published and draft pages.</div></div>
      <div class="admin-stat-card"><h3>Messages</h3><div class="admin-stat-value"><?= is_array($contactItems) ? count($contactItems) : 0 ?></div><div class="admin-stat-meta">Contact form messages waiting in the inbox.</div></div>
      <div class="admin-stat-card"><h3>Applications</h3><div class="admin-stat-value"><?= is_array($appItems) ? count($appItems) : 0 ?></div><div class="admin-stat-meta">Join-the-team applications saved by the site.</div></div>
      <div class="admin-stat-card"><h3>Requests</h3><div class="admin-stat-value"><?= is_array($requestsItems) ? count($requestsItems) : 0 ?></div><div class="admin-stat-meta">Public requests now feed straight into the admin request queue.</div></div>
      <div class="admin-stat-card"><h3>Community</h3><div class="admin-stat-value"><?= (is_array($friends)?count($friends):0) + (is_array($fans)?count($fans):0) ?></div><div class="admin-stat-meta">Friends and fans entries currently active.</div></div>
    </div>
  </section>
  <section class="admin-card-panel">
    <h3>Live toggles snapshot</h3>
    <table class="admin-table">
      <thead><tr><th>Module</th><th>Status</th></tr></thead>
      <tbody>
        <tr><td>Live Chat in Navigation</td><td><?= $chatEnabled ? 'Enabled' : 'Hidden' ?></td></tr>
        <tr><td>Events Link</td><td><?= $eventsNav ? 'Shown' : 'Hidden' ?></td></tr>
        <tr><td>Top 50 Link</td><td><?= $top50Nav ? 'Shown' : 'Hidden' ?></td></tr>
        <tr><td>Popout Player Link</td><td><?= $playerNav ? 'Shown' : 'Hidden' ?></td></tr>
      </tbody>
    </table>
  </section>
</section>

<section class="content-card mt-3">
  <h2 class="admin-title">Content Studio</h2>
  <div class="admin-grid-v2">
    <?php dashCard('/admin/pages.php','🧩','Page Builder','Create custom pages with hero images, summaries, buttons and previews.'); ?>
    <?php dashCard('/admin/newsreel_admin.php','📰','News','Post updates, feature homepage stories and manage article images.'); ?>
    <?php dashCard('/admin/events.php','🎉','Events','Manage promotions, contests and event content.'); ?>
    <?php dashCard('/admin/top50.php','🎵','Top 50','Manage your chart page and ranking data.'); ?>
    <?php dashCard('/admin/schedule.php','📅','Schedule','Visual schedule editor powering live DJ automation.'); ?>
    <?php dashCard('/admin/about.php','ℹ️','About','Edit the station bio and front-page story.'); ?>
    <?php dashCard('/admin/media.php','🖼️','Media Library','Upload artwork and copy image paths for reuse.'); ?>
    <?php dashCard('/admin/navigation.php','🧭','Navigation','Add extra external links and tidy the navigation.'); ?>
  </div>
</section>

<section class="content-card mt-3">
  <h2 class="admin-title">Community & Radio Tools</h2>
  <div class="admin-grid-v2">
    <?php dashCard('/admin/djs.php','🎙️','DJs','Manage presenter profiles, images and bios.'); ?>
    <?php dashCard('/admin/friends.php','🤝','Friends','Maintain partner and supporter cards with images and links.'); ?>
    <?php dashCard('/admin/fans.php','⭐','Fans','Highlight supporters, descriptions and featured fan badges.'); ?>
    <?php dashCard('/admin/contact.php','📨','Contact Messages','Review and clear messages from the public contact form.'); ?>
    <?php dashCard('/admin/applications.php','📝','Join the Team','Review presenter and volunteer applications sent from the site.'); ?>
    <?php dashCard('/admin/requests.php','🎶','Requests','Track request workflow, statuses and DJ notes.'); ?>
    <?php dashCard('/admin/comments.php','💬','Comments','Approve, reply to and moderate homepage discussion.'); ?>
    <?php dashCard('/admin/chat.php','🗨️','Live Chat','Moderate room messages, toggles and DJ-side posting.'); ?>
    <?php dashCard('/admin/player.php','🪟','Popout Player','Control player visibility and stream settings.'); ?>
    <?php dashCard('/admin/radiostats.php','📡','Radio Stats','Monitor Icecast stats, now playing and listeners.'); ?>
    <?php dashCard('/admin/file-finder.php','🔎','File Finder','Search your uploads and pull in safe downloads for staff tools.'); ?>
  </div>
</section>

<section class="content-card mt-3">
  <h2 class="admin-title">System & Safety</h2>
  <div class="admin-grid-v2">
    <?php dashCard('/admin/users.php','👤','User Management','Edit roles, passwords and page permissions from checkbox grants.'); ?>
    <?php dashCard('/admin/backup.php','🧰','Backup / Restore','Download data backups or restore from zip.'); ?>
    <?php dashCard('/admin/poll.php','📊','Poll Manager','Control the homepage poll, options and vote reset tools.'); ?>
    <?php dashCard('/admin/admin-webstats.php','📈','Web Stats','Review Cloudflare and local traffic reporting.'); ?>
    <?php dashCard('http://cr3.broadcasturself.com:6660/admin/','📶','Backup Icecast','Open the backup panel in a new tab.','Open external',true); ?>
  </div>
</section>
<div class="admin-dashboard-footer-spacer" style="height:6px;"></div>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
