<?php require_once __DIR__ . '/config.php'; $settings = get_settings(); ?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= esc($settings['event_name'] ?? ($settings['site_name'] ?? 'Broadcasturself')) ?></title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<div class="site-shell">
  <div class="topbar">
    <div class="brand">
      <img src="<?= esc($settings['site_logo'] ?? 'assets/site-logo.png') ?>" alt="Logo">
      <div>
        <h1><?= esc($settings['site_name'] ?? 'Broadcasturself') ?></h1>
        <div class="meta"><?= esc($settings['site_tagline'] ?? 'Live radio event network') ?></div>
      </div>
    </div>
    <div class="live-pill"><span class="live-dot"></span><span><?= strtoupper(esc($settings['site_status'] ?? 'live')) ?></span></div>
  </div>
  <div class="nav">
    <a href="index.php">Home</a>
    <a href="stations.php">Participating Stations</a>
    <a href="schedule.php">Schedule</a>
    <a href="submit.php">Submit Entry</a>
    <a href="poll.php">Live Polls</a>
  </div>
    <?php
    $message = '';
    $messageClass = 'info';
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      if (!can_submit($settings)) {
        $message = 'Submissions are currently closed.';
        $messageClass = 'danger';
      } else {
        $name = trim((string) ($_POST['name'] ?? ''));
        $email = trim((string) ($_POST['email'] ?? ''));
        $platform = trim((string) ($_POST['platform'] ?? ''));
        $answer = trim((string) ($_POST['answer'] ?? ''));
        $details = trim((string) ($_POST['details'] ?? ''));
        if ($answer === '') {
          $message = 'Please enter an answer before submitting.';
          $messageClass = 'danger';
        } else {
          $answerRecord = current_answer_record();
          $correct = $answerRecord ? answer_matches($answer, $answerRecord) : false;
          $timestamp = date('Y-m-d H:i:s');
          $row = [
            'name' => $name,
            'email' => $email,
            'platform' => $platform,
            'answer' => $answer,
            'details' => $details,
            'correct' => $correct,
            'timestamp' => $timestamp,
            'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
            'is_winner' => false,
          ];
          if ($correct && empty($settings['winner_locked'])) {
            $settings['winner_locked'] = true;
            $settings['winner_name'] = $name !== '' ? $name : 'Unnamed entrant';
            $settings['winner_time'] = time();
            $settings['winner_announced'] = true;
            $row['is_winner'] = true;
            save_settings($settings);
            log_event('Winner auto-detected: ' . $settings['winner_name']);
          }
          append_submission($row);
          $message = !empty($settings['success_message']) ? (string) $settings['success_message'] : 'Thanks. Your submission has been received.';
          $messageClass = $correct ? 'success' : 'info';
        }
      }
    }
    ?>
    <div class="hero">
      <h2><?= esc($settings['submission_title'] ?? 'Submit Your Entry') ?></h2>
      <p><?= esc($settings['submission_intro'] ?? 'Use the form below to send your answer.') ?></p>
      <?php if (!can_submit($settings)): ?>
        <div class="banner danger">Submissions are closed right now.</div>
      <?php endif; ?>
    </div>

    <?php if ($message !== ''): ?>
      <div class="banner <?= esc($messageClass) ?>"><?= esc($message) ?></div>
    <?php endif; ?>

    <div class="card">
      <form method="post">
        <div class="two-col">
          <div>
            <label><?= esc($settings['name_label'] ?? 'Your Name') ?></label>
            <input type="text" name="name">
          </div>
          <div>
            <label><?= esc($settings['email_label'] ?? 'Email Address') ?></label>
            <input type="email" name="email">
          </div>
        </div>
        <div class="two-col">
          <div>
            <label><?= esc($settings['platform_label'] ?? 'Where did you hear about this event?') ?></label>
            <select name="platform">
              <option value="">Select one</option>
              <?php foreach (($settings['platforms'] ?? ['Website','On Air']) as $platform): ?>
                <option value="<?= esc((string) $platform) ?>"><?= esc((string) $platform) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div>
            <label><?= esc($settings['field_one_label'] ?? 'Answer') ?></label>
            <input type="text" name="answer" required>
          </div>
        </div>
        <label><?= esc($settings['field_message_label'] ?? 'Message / Notes') ?></label>
        <textarea name="details"></textarea>
        <div class="toolbar">
          <button type="submit" <?= can_submit($settings) ? '' : 'disabled' ?>>Submit Entry</button>
        </div>
      </form>
    </div>

  <div class="footer-note"><?= esc($settings['footer_note'] ?? 'Powered by Broadcasturself') ?></div>
</div>
</body>
</html>
