<?php
session_start();
require __DIR__ . '/auth.php';
$pageTitle = "Dashboard — Home Time Radio Radio";
include __DIR__ . '/includes/admin_header.php';
$settings = smurf_read_json(__DIR__ . '/../data/settings.json', []);
$pages = smurf_read_json(__DIR__ . '/../data/pages.json', []);
$news = smurf_read_json(__DIR__ . '/../data/news.json', []);
$djs = smurf_read_json(__DIR__ . '/../data/djs.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');
$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'){ echo '<a class="admin-card-link" href="'.htmlspecialchars($href,ENT_QUOTES,'UTF-8').'"><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>Welcome back</h3>
    <p class="admin-stat-meta">This upgraded Home Time admin keeps your current tools but makes them easier to find and manage. Use the sidebar to jump between content, community and system tools.</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 managed in the builder.</div>
      </div>
      <div class="admin-stat-card">
        <h3>News Items</h3>
        <div class="admin-stat-value"><?= is_array($news) ? count($news) : 0 ?></div>
        <div class="admin-stat-meta">Stories, featured posts and homepage items.</div>
      </div>
      <div class="admin-stat-card">
        <h3>DJs</h3>
        <div class="admin-stat-value"><?= is_array($djs) ? count($djs) : 0 ?></div>
        <div class="admin-stat-meta">Profiles, show details and live highlights.</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 + fans entries currently on the site.</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, CTA buttons and preview.'); ?>
    <?php dashCard('/admin/news.php','📰','News','Post updates, feature homepage stories and manage article images.'); ?>
    <?php dashCard('/admin/events.php','🎉','Events','Build promotions, contests and event landing pages.'); ?>
    <?php dashCard('/admin/top50.php','🎵','Top 50','Manage your chart page and stream-fed ranking data.'); ?>
    <?php dashCard('/admin/schedule.php','📅','Schedule','Visual schedule editor powering live DJ automation.'); ?>
    <?php dashCard('/admin/about.php','ℹ️','About','Edit your 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/sponsor style cards with images and links.'); ?>
    <?php dashCard('/admin/fans.php','⭐','Fans','Highlight supporters, descriptions and top-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 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.'); ?>
  </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/westats.php','📈','Web Stats','Review Cloudflare and local traffic reporting.'); ?>
    <?php dashCard('http://cr3.broadcasturself.com:1982/status.xsl','📶','Icecast Status','Open the broadcaster control status page in a new tab.','Open external'); ?>
  </div>
</section>
<div class="admin-dashboard-footer-spacer" style="height:6px;"></div>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
