
<?php
session_start();
include __DIR__ . '/auth.php';
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';

function count_lines_safe($file){
    return (is_file($file) ? count(array_filter(file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [])) : 0);
}
function count_json_safe($file){
    if (!is_file($file)) return 0;
    $data = json_decode((string)file_get_contents($file), true);
    return is_array($data) ? count($data) : 0;
}
$stats = [
  'news' => count_lines_safe(__DIR__ . '/../data/news.txt'),
  'djs' => count_json_safe(__DIR__ . '/../data/djs.json') ?: count_lines_safe(__DIR__ . '/../data/djs.txt'),
  'friends' => count_json_safe(__DIR__ . '/../data/friends.json') ?: count_lines_safe(__DIR__ . '/../data/friends.txt'),
  'fans' => count_json_safe(__DIR__ . '/../data/fans.json'),
  'comments' => count_lines_safe(__DIR__ . '/../data/comments.txt'),
  'requests' => count_lines_safe(__DIR__ . '/../data/requests.txt'),
  'applications' => count_lines_safe(__DIR__ . '/../data/applications.txt'),
  'contact' => count_lines_safe(__DIR__ . '/../data/contact_messages.txt'),
];
function dashCard($href,$emoji,$title,$desc){
  echo '<a href="'.htmlspecialchars($href,ENT_QUOTES,'UTF-8').'" class="admin-card"><div><span class="admin-card-title">'.$emoji.' '.htmlspecialchars($title,ENT_QUOTES,'UTF-8').'</span><span class="admin-card-desc">'.htmlspecialchars($desc,ENT_QUOTES,'UTF-8').'</span></div><strong>Open →</strong></a>';
}
?>
<div class="admin-toolbar">
  <div>
    <h1>Oldies And More Admin Dashboard</h1>
    <p>Professional admin shell with the public site navigation kept in place.</p>
  </div>
  <div class="admin-toolbar-links">
    <a href="../index.php">🏠 Main Site</a>
    <a href="logout.php">🔓 Logout</a>
  </div>
</div>
<div class="main-wrap">
  <div class="content-panel">
    <section class="admin-hero-grid">
      <section class="admin-card-panel">
        <h3>Site snapshot</h3>
        <p class="admin-stat-meta">Use the cards below to manage featured homepage sections, schedules, DJs, community tools and admin content in one place.</p>
        <div class="admin-stat-grid">
          <div class="admin-stat-card"><h3>News</h3><div class="admin-stat-value"><?= $stats['news'] ?></div><div class="admin-stat-meta">Stories and featured homepage posts.</div></div>
          <div class="admin-stat-card"><h3>DJs</h3><div class="admin-stat-value"><?= $stats['djs'] ?></div><div class="admin-stat-meta">Presenter profiles on the public site.</div></div>
          <div class="admin-stat-card"><h3>Community</h3><div class="admin-stat-value"><?= $stats['friends'] + $stats['fans'] ?></div><div class="admin-stat-meta">Friends and fan spotlight entries.</div></div>
          <div class="admin-stat-card"><h3>Inbox</h3><div class="admin-stat-value"><?= $stats['contact'] + $stats['applications'] + $stats['requests'] ?></div><div class="admin-stat-meta">Contact, applications and requests waiting.</div></div>
        </div>
      </section>
      <section class="admin-card-panel">
        <h3>Quick checks</h3>
        <table class="admin-table">
          <thead><tr><th>Area</th><th>Status</th></tr></thead>
          <tbody>
            <tr><td>Featured News Toggle</td><td>Managed in News</td></tr>
            <tr><td>Featured Event Toggle</td><td>Managed in Events</td></tr>
            <tr><td>Admin / Public Styling</td><td>Unified shell active</td></tr>
            <tr><td>Public Navigation</td><td>Visible inside admin</td></tr>
          </tbody>
        </table>
      </section>
    </section>

    <section class="content-panel" style="margin-top:18px;">
      <h2 class="section-title">Content & Homepage</h2>
      <div class="admin-grid-v2">
        <?php dashCard('news.php','📰','News','Create, edit, delete and feature homepage news stories.'); ?>
        <?php dashCard('events.php','🎉','Events','Control the public events page and homepage featured event block.'); ?>
        <?php dashCard('schedule.php','📅','Schedule','Edit the station schedule feeding public pages and homepage blocks.'); ?>
        <?php dashCard('top50.php','🎵','Top 50','Manage chart data and page presentation.'); ?>
        <?php dashCard('about.php','ℹ️','About','Edit station information and public about content.'); ?>
        <?php dashCard('pages.php','📄','Custom Pages','Create custom pages and extra public content.'); ?>
        <?php dashCard('navigation.php','🧭','Navigation','Manage extra public navigation links.'); ?>
        <?php dashCard('player.php','🪟','Popout Player','Adjust player settings and links.'); ?>
      </div>
    </section>

    <section class="content-panel" style="margin-top:18px;">
      <h2 class="section-title">People & Community</h2>
      <div class="admin-grid-v2">
        <?php dashCard('djs.php','🎙️','DJs','Manage presenter images, bios and profile cards.'); ?>
        <?php dashCard('friends.php','🤝','Friends','Manage partner and supporter cards.'); ?>
        <?php dashCard('fans.php','⭐','Fans','Highlight top listeners and fan entries.'); ?>
        <?php dashCard('comments.php','💬','Comments','Approve, edit and moderate public comments.'); ?>
        <?php dashCard('requests.php','🎶','Requests','Review and moderate song requests.'); ?>
        <?php dashCard('contact.php','📨','Contact','Read, delete and clear contact submissions.'); ?>
        <?php dashCard('applications.php','📝','Join the Team','Review join-the-team applications.'); ?>
        <?php dashCard('media.php','🖼️','Media Library','Upload and manage reusable site images.'); ?>
      </div>
    </section>

    <section class="content-panel" style="margin-top:18px;">
      <h2 class="section-title">System Tools</h2>
      <div class="admin-grid-v2">
        <?php if (($_SESSION['role'] ?? '') === 'admin') dashCard('users.php','👤','User Management','Manage roles, permissions and access.'); ?>
        <?php dashCard('poll.php','📊','Poll','Manage homepage poll options and votes.'); ?>
        <?php dashCard('radiostats.php','📡','Radio Stats','Monitor stream and listener info.'); ?>
        <?php dashCard('westats.php','📈','Web Stats','Review web analytics.'); ?>
        <?php dashCard('file-finder.php','🗂️','File Finder','Search and manage files in the admin area.'); ?>
        <?php dashCard('backup.php','🧰','Backup','Open backup and restore tools.'); ?>
      </div>
    </section>
  </div>
</div>
<?php include __DIR__ . '/includes/footer.php'; ?>
