<?php
require __DIR__ . '/config.php';
$settings = get_settings();
$success = '';
$error = '';

if (empty($settings['submission_page_open'])) {
    http_response_code(403);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($settings['submission_page_open'])) {
    $entryType = trim($_POST['entry_type'] ?? '');
    $fieldOne = trim($_POST['field_one'] ?? '');
    $fieldTwo = trim($_POST['field_two'] ?? '');
    $messageText = trim($_POST['message_text'] ?? '');
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $platform = trim($_POST['platform'] ?? '');

    if ($fieldOne === '' || $email === '' || $platform === '') {
        $error = 'Please complete all required fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } else {
        $submissions = get_submissions();
        $submissions[] = [
            'entry_type' => $entryType,
            'field_one' => $fieldOne,
            'field_two' => $fieldTwo,
            'message_text' => $messageText,
            'name' => $name,
            'email' => $email,
            'platform' => $platform,
            'ip' => $_SERVER['REMOTE_ADDR'] ?? '',
            'timestamp' => date('Y-m-d H:i:s')
        ];
        save_submissions($submissions);
        $success = $settings['success_message'] ?? 'Thanks. Your submission has been received.';
    }
}

$options = $settings['entry_type_options'] ?? [];
$platforms = $settings['platforms'] ?? [];
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= e($settings['submission_title']) ?></title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Oswald:wght@400;500;600;700&family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
<style>
:root{
  --bg:#06070a;--panel:rgba(10,11,16,.86);--line:rgba(255,170,68,.22);
  --text:#eef6ff;--muted:rgba(238,246,255,.75);--gold:#ffb347;--gold2:#ff7f11;
}
*{box-sizing:border-box}
body{
  margin:0;color:var(--text);font-family:Inter,system-ui,sans-serif;
  background:
    linear-gradient(180deg, rgba(4,5,7,.55), rgba(4,5,7,.92)),
    url('<?= e($settings['site_logo']) ?>') center top / cover no-repeat fixed,
    linear-gradient(180deg,#040507,#0b0d12);
}
.wrap{max-width:760px;margin:0 auto;padding:30px 18px 60px}
.back{display:inline-block;margin-bottom:18px;color:#ffd391}
.panel{
  background:var(--panel);border:1px solid var(--line);border-radius:28px;
  box-shadow:0 28px 70px rgba(0,0,0,.45);padding:26px;
}
h1,h2{font-family:Oswald,Inter,sans-serif;line-height:1.05;margin:0 0 10px}
.helper{color:var(--muted)}
.label{display:block;margin:14px 0 6px;font-weight:700}
input,select,textarea{
  width:100%;max-width:100%;
  padding:13px 14px;border-radius:14px;border:1px solid rgba(255,255,255,.1);
  background:rgba(255,255,255,.06);color:var(--text);outline:none
}
textarea{min-height:120px;resize:vertical}
option{color:#111}
button{
  width:100%;margin-top:18px;padding:15px 18px;border:0;border-radius:999px;
  background:linear-gradient(180deg,var(--gold),var(--gold2));color:#210d00;
  font-weight:800;cursor:pointer
}
.alert{margin:16px 0;padding:14px 16px;border-radius:16px}
.success{background:rgba(36,121,24,.28);border:1px solid rgba(126,217,87,.3)}
.error{background:rgba(135,18,18,.28);border:1px solid rgba(255,123,123,.3)}
.grid{display:grid;grid-template-columns:1fr 1fr;gap:14px}
@media (max-width:680px){.grid{grid-template-columns:1fr}}
</style>
</head>
<body>
<div class="wrap">
  <a class="back" href="<?= e(site_url('index.php')) ?>">← Back to site</a>

  <div class="panel">
    <h1><?= e($settings['submission_title']) ?></h1>
    <p class="helper"><?= e($settings['event_name']) ?> — <?= e($settings['submission_intro']) ?></p>

    <?php if (empty($settings['submission_page_open'])): ?>
      <div class="alert error">The submission page is currently closed. Please check back when the event opens.</div>
    <?php else: ?>
      <?php if ($success): ?><div class="alert success"><?= e($success) ?></div><?php endif; ?>
      <?php if ($error): ?><div class="alert error"><?= e($error) ?></div><?php endif; ?>

      <form method="post">
        <div class="grid">
          <div>
            <label class="label"><?= e($settings['entry_type_label']) ?></label>
            <select name="entry_type">
              <?php foreach ($options as $option): ?>
                <option value="<?= e($option) ?>" <?= (($_POST['entry_type'] ?? '') === $option) ? 'selected' : '' ?>><?= e($option) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div>
            <label class="label"><?= e($settings['platform_label']) ?></label>
            <select name="platform" required>
              <option value="">Choose one</option>
              <?php foreach ($platforms as $option): ?>
                <option value="<?= e($option) ?>" <?= (($_POST['platform'] ?? '') === $option) ? 'selected' : '' ?>><?= e($option) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
        </div>

        <label class="label"><?= e($settings['field_one_label']) ?></label>
        <input type="text" name="field_one" placeholder="<?= e($settings['field_one_placeholder']) ?>" value="<?= e($_POST['field_one'] ?? '') ?>" required>

        <label class="label"><?= e($settings['field_two_label']) ?></label>
        <input type="text" name="field_two" placeholder="<?= e($settings['field_two_placeholder']) ?>" value="<?= e($_POST['field_two'] ?? '') ?>">

        <label class="label"><?= e($settings['field_message_label']) ?></label>
        <textarea name="message_text" placeholder="<?= e($settings['field_message_placeholder']) ?>"><?= e($_POST['message_text'] ?? '') ?></textarea>

        <div class="grid">
          <div>
            <label class="label"><?= e($settings['name_label']) ?></label>
            <input type="text" name="name" value="<?= e($_POST['name'] ?? '') ?>">
          </div>
          <div>
            <label class="label"><?= e($settings['email_label']) ?></label>
            <input type="email" name="email" value="<?= e($_POST['email'] ?? '') ?>" required>
          </div>
        </div>

        <button type="submit">Send Submission</button>
      </form>
    <?php endif; ?>
  </div>
</div>
</body>
</html>
