<?php
require_once __DIR__ . '/includes/config.php';
session_start();

$flash = null;
$flash_type = 'ok';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  // Honeypot — moet leeg zijn (bots vullen alles in)
  if (!empty($_POST['website'] ?? '')) {
    // Bot, doe niets, faalstil
    $flash = t('contact.form.success');
  } elseif (!csrf_check($_POST['_csrf'] ?? '')) {
    $flash = t('contact.form.error');
    $flash_type = 'error';
  } else {
    $name    = trim($_POST['name'] ?? '');
    $email   = trim($_POST['email'] ?? '');
    $phone   = trim($_POST['phone'] ?? '');
    $subject = trim($_POST['subject'] ?? '');
    $message = trim($_POST['message'] ?? '');

    if ($name === '' || $email === '' || $message === '' ||
        !filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $flash = t('contact.form.invalid');
      $flash_type = 'error';
    } else {
      $to      = CONTACT_EMAIL;
      $subj    = '[Site] ' . ($subject !== '' ? $subject : 'Nieuw bericht via website');
      $body    = "Naam:    $name\n"
               . "E-mail:  $email\n"
               . "Telefoon:$phone\n"
               . "Onderw.: $subject\n\n"
               . "Bericht:\n$message\n";
      $headers = "From: " . SITE_NAME . " <no-reply@" . ($_SERVER['HTTP_HOST'] ?? 'localhost') . ">\r\n"
               . "Reply-To: $name <$email>\r\n"
               . "Content-Type: text/plain; charset=UTF-8\r\n"
               . "X-Mailer: PHP/" . phpversion();

      $sent = @mail($to, $subj, $body, $headers);
      if ($sent) {
        $flash = t('contact.form.success');
        $_POST = [];
      } else {
        $flash = t('contact.form.error');
        $flash_type = 'error';
      }
    }
  }
}

$page_active = 'contact';
$page_title  = $lang === 'en' ? 'Contact' : 'Contact';
include __DIR__ . '/includes/header.php';

$subject_options = explode('|', t('contact.form.subject.options'));
?>

<header class="page-head">
  <div class="container">
    <div class="eyebrow"><?= e(t('home.contact.eyebrow')) ?></div>
    <h1><?= e(t('home.contact.title')) ?> <em><?= e(t('home.contact.title.em')) ?></em><?= e(t('home.contact.title.suffix')) ?></h1>
    <p class="lede"><?= e(t('home.contact.intro')) ?></p>
  </div>
</header>

<section style="background:var(--canvas)">
  <div class="container">
    <div class="contact-card">
      <?php if ($flash): ?>
        <div class="flash <?= $flash_type === 'error' ? 'error' : '' ?>"><?= e($flash) ?></div>
      <?php endif; ?>

      <form method="post" action="<?= e(url('contact.php')) ?>" novalidate>
        <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
        <input type="text" name="website" class="honeypot" tabindex="-1" autocomplete="off" aria-hidden="true">

        <div class="row">
          <div class="field">
            <label for="name"><?= e(t('contact.form.name')) ?> *</label>
            <input id="name" name="name" type="text" required value="<?= e($_POST['name'] ?? '') ?>">
          </div>
          <div class="field">
            <label for="email"><?= e(t('contact.form.email')) ?> *</label>
            <input id="email" name="email" type="email" required value="<?= e($_POST['email'] ?? '') ?>">
          </div>
        </div>

        <div class="row">
          <div class="field">
            <label for="phone"><?= e(t('contact.form.phone')) ?></label>
            <input id="phone" name="phone" type="tel" value="<?= e($_POST['phone'] ?? '') ?>">
          </div>
          <div class="field">
            <label for="subject"><?= e(t('contact.form.subject')) ?></label>
            <select id="subject" name="subject">
              <?php foreach ($subject_options as $opt): ?>
                <option value="<?= e($opt) ?>" <?= ($_POST['subject'] ?? '') === $opt ? 'selected' : '' ?>><?= e($opt) ?></option>
              <?php endforeach; ?>
            </select>
          </div>
        </div>

        <div class="field">
          <label for="message"><?= e(t('contact.form.message')) ?> *</label>
          <textarea id="message" name="message" required><?= e($_POST['message'] ?? '') ?></textarea>
        </div>

        <div style="text-align:center;margin-top:8px">
          <button type="submit" class="btn"><?= e(t('contact.form.submit')) ?></button>
        </div>
      </form>

      <div class="contact-info">
        <div>
          <h4><?= e(t('contact.info.email')) ?></h4>
          <a href="mailto:<?= e(CONTACT_EMAIL) ?>"><?= e(CONTACT_EMAIL) ?></a>
        </div>
        <div>
          <h4><?= e(t('contact.info.phone')) ?></h4>
          <a href="tel:<?= e(preg_replace('/[^+\d]/', '', CONTACT_PHONE)) ?>"><?= e(CONTACT_PHONE) ?></a>
        </div>
      </div>
      <div style="margin-top:32px;border-top:1px solid var(--line);padding-top:24px">
        <h4 style="font-size:11px;letter-spacing:.25em;text-transform:uppercase;color:var(--ochre);margin-bottom:10px;font-weight:500"><?= e(t('contact.info.address')) ?></h4>
        <p style="font-family:'Lora',serif;font-size:18px"><?= e(CONTACT_ADDRESS) ?></p>
      </div>
    </div>

    <div style="max-width:820px;margin:48px auto 0">
      <iframe src="<?= e(MAPS_EMBED) ?>" width="100%" height="360" style="border:1px solid var(--line)" allowfullscreen="" loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>
    </div>
  </div>
</section>

<?php include __DIR__ . '/includes/footer.php'; ?>
