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

function field_on(array $settings, string $key, bool $default = false): bool {
    return isset($settings[$key]) ? (bool) $settings[$key] : $default;
}

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'] ?? '');
    $fieldThree = trim($_POST['field_three'] ?? '');
    $messageText = trim($_POST['message_text'] ?? '');
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $platform = trim($_POST['platform'] ?? '');

    $errors = [];

    if (field_on($settings, 'show_entry_type', true) && field_on($settings, 'require_entry_type') && $entryType === '') {
        $errors[] = 'Please choose an entry type.';
    }
    if (field_on($settings, 'show_field_one', true) && field_on($settings, 'require_field_one', true) && $fieldOne === '') {
        $errors[] = 'Please complete the first field.';
    }
    if (field_on($settings, 'show_field_two', true) && field_on($settings, 'require_field_two') && $fieldTwo === '') {
        $errors[] = 'Please complete the second field.';
    }
    if (field_on($settings, 'show_field_three') && field_on($settings, 'require_field_three') && $fieldThree === '') {
        $errors[] = 'Please complete the third field.';
    }
    if (field_on($settings, 'show_message_field', true) && field_on($settings, 'require_message_field') && $messageText === '') {
        $errors[] = 'Please complete the message field.';
    }
    if (field_on($settings, 'show_name_field', true) && field_on($settings, 'require_name_field') && $name === '') {
        $errors[] = 'Please enter your name.';
    }
    if (field_on($settings, 'show_email_field', true) && field_on($settings, 'require_email_field', true) && $email === '') {
        $errors[] = 'Please enter your email address.';
    }
    if (field_on($settings, 'show_email_field', true) && $email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'Please enter a valid email address.';
    }
    if (field_on($settings, 'show_platform_field', true) && field_on($settings, 'require_platform_field', true) && $platform === '') {
        $errors[] = 'Please choose where you heard about this event.';
    }

    if ($errors) {
        $error = implode(' ', $errors);
    } else {
        $submissions = get_submissions();
        $submissions[] = [
            'entry_type' => field_on($settings, 'show_entry_type', true) ? $entryType : '',
            'field_one' => field_on($settings, 'show_field_one', true) ? $fieldOne : '',
            'field_two' => field_on($settings, 'show_field_two', true) ? $fieldTwo : '',
            'field_three' => field_on($settings, 'show_field_three') ? $fieldThree : '',
            'message_text' => field_on($settings, 'show_message_field', true) ? $messageText : '',
            'name' => field_on($settings, 'show_name_field', true) ? $name : '',
            'email' => field_on($settings, 'show_email_field', true) ? $email : '',
            'platform' => field_on($settings, 'show_platform_field', true) ? $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.';
        $_POST = [];
    }
}

$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:
    radial-gradient(circle at 10% 10%, rgba(255,122,0,.12), transparent 26%),
    radial-gradient(circle at 90% 18%, rgba(58,184,255,.12), transparent 24%),
    linear-gradient(180deg, rgba(4,5,7,.65), rgba(4,5,7,.94)),
    linear-gradient(180deg,#040507,#0b0d12);
}
body::before{
  content:"";
  position:fixed;
  inset:-20%;
  background:
    radial-gradient(circle at 25% 40%, rgba(255,122,0,.09), transparent 18%),
    radial-gradient(circle at 70% 30%, rgba(58,184,255,.08), transparent 22%);
  animation: formGlow 10s ease-in-out infinite alternate;
  pointer-events:none;
}
.wrap{max-width:760px;margin:0 auto;padding:30px 18px 60px;position:relative;z-index:1}
.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), 0 0 30px rgba(58,184,255,.06);padding:26px;
}
h1,h2{font-family:Oswald,Inter,sans-serif;line-height:1.05;margin:0 0 10px;text-shadow:0 0 16px rgba(58,184,255,.06)}
.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;
  transition:border-color .2s ease, box-shadow .2s ease;
}
input:focus,select:focus,textarea:focus{border-color:rgba(58,184,255,.35);box-shadow:0 0 0 3px rgba(58,184,255,.08), 0 0 18px rgba(58,184,255,.08)}
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;box-shadow:0 0 20px rgba(255,136,17,.16);
  transition:transform .2s ease, box-shadow .2s ease;
}
button:hover{transform:translateY(-1px);box-shadow:0 0 26px rgba(255,136,17,.22)}
.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}
@keyframes formGlow{0%{transform:translate3d(-1%,0,0)}100%{transform:translate3d(2%,1%,0)}}
@media (prefers-reduced-motion: reduce){*,*::before,*::after{animation:none !important;transition:none !important}}
@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">
          <?php if (field_on($settings, 'show_entry_type', true)): ?>
            <div>
              <label class="label"><?= e($settings['entry_type_label']) ?><?= field_on($settings, 'require_entry_type') ? ' *' : '' ?></label>
              <select name="entry_type" <?= field_on($settings, 'require_entry_type') ? 'required' : '' ?>>
                <option value="">Choose one</option>
                <?php foreach ($options as $option): ?>
                  <option value="<?= e($option) ?>" <?= (($_POST['entry_type'] ?? '') === $option) ? 'selected' : '' ?>><?= e($option) ?></option>
                <?php endforeach; ?>
              </select>
            </div>
          <?php endif; ?>

          <?php if (field_on($settings, 'show_platform_field', true)): ?>
            <div>
              <label class="label"><?= e($settings['platform_label']) ?><?= field_on($settings, 'require_platform_field', true) ? ' *' : '' ?></label>
              <select name="platform" <?= field_on($settings, 'require_platform_field', true) ? '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>
          <?php endif; ?>
        </div>

        <?php if (field_on($settings, 'show_field_one', true)): ?>
          <label class="label"><?= e($settings['field_one_label']) ?><?= field_on($settings, 'require_field_one', true) ? ' *' : '' ?></label>
          <input type="text" name="field_one" placeholder="<?= e($settings['field_one_placeholder']) ?>" value="<?= e($_POST['field_one'] ?? '') ?>" <?= field_on($settings, 'require_field_one', true) ? 'required' : '' ?>>
        <?php endif; ?>

        <?php if (field_on($settings, 'show_field_two', true)): ?>
          <label class="label"><?= e($settings['field_two_label']) ?><?= field_on($settings, 'require_field_two') ? ' *' : '' ?></label>
          <input type="text" name="field_two" placeholder="<?= e($settings['field_two_placeholder']) ?>" value="<?= e($_POST['field_two'] ?? '') ?>" <?= field_on($settings, 'require_field_two') ? 'required' : '' ?>>
        <?php endif; ?>

        <?php if (field_on($settings, 'show_field_three')): ?>
          <label class="label"><?= e($settings['field_three_label']) ?><?= field_on($settings, 'require_field_three') ? ' *' : '' ?></label>
          <input type="text" name="field_three" placeholder="<?= e($settings['field_three_placeholder']) ?>" value="<?= e($_POST['field_three'] ?? '') ?>" <?= field_on($settings, 'require_field_three') ? 'required' : '' ?>>
        <?php endif; ?>

        <?php if (field_on($settings, 'show_message_field', true)): ?>
          <label class="label"><?= e($settings['field_message_label']) ?><?= field_on($settings, 'require_message_field') ? ' *' : '' ?></label>
          <textarea name="message_text" placeholder="<?= e($settings['field_message_placeholder']) ?>" <?= field_on($settings, 'require_message_field') ? 'required' : '' ?>><?= e($_POST['message_text'] ?? '') ?></textarea>
        <?php endif; ?>

        <div class="grid">
          <?php if (field_on($settings, 'show_name_field', true)): ?>
            <div>
              <label class="label"><?= e($settings['name_label']) ?><?= field_on($settings, 'require_name_field') ? ' *' : '' ?></label>
              <input type="text" name="name" value="<?= e($_POST['name'] ?? '') ?>" <?= field_on($settings, 'require_name_field') ? 'required' : '' ?>>
            </div>
          <?php endif; ?>

          <?php if (field_on($settings, 'show_email_field', true)): ?>
            <div>
              <label class="label"><?= e($settings['email_label']) ?><?= field_on($settings, 'require_email_field', true) ? ' *' : '' ?></label>
              <input type="email" name="email" value="<?= e($_POST['email'] ?? '') ?>" <?= field_on($settings, 'require_email_field', true) ? 'required' : '' ?>>
            </div>
          <?php endif; ?>
        </div>

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