<?php
/**
 * Plattr, Contact page.
 *
 * Apple-style editorial: light cream theme, generous whitespace,
 * Playfair Display headlines + Inter body. Uses the shared
 * page-head / page-foot partials so the chrome matches the rest
 * of the site.
 *
 * Form: POST → mail() with honeypot + time-based spam filter.
 */
declare(strict_types=1);

/* ── Form handling ─────────────────────────────────────────────
   Server-side so submissions work even when JS fails. On success
   we set $sent=true and render the thank-you state; on validation
   error we re-render the form with the user's values preserved. */
$sent = false;
$errors = [];
$form = ['name' => '', 'email' => '', 'business' => '', 'subject' => '', 'message' => ''];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!empty($_POST['website'])) {
        // Honeypot triggered, silently pretend success.
        $sent = true;
    } else {
        $form['name']     = trim((string)($_POST['name']     ?? ''));
        $form['email']    = trim((string)($_POST['email']    ?? ''));
        $form['business'] = trim((string)($_POST['business'] ?? ''));
        $form['subject']  = trim((string)($_POST['subject']  ?? ''));
        $form['message']  = trim((string)($_POST['message']  ?? ''));

        if ($form['name'] === '' || mb_strlen($form['name']) < 2)       $errors['name']    = 'Please tell us your name.';
        if (!filter_var($form['email'], FILTER_VALIDATE_EMAIL))         $errors['email']   = "That email doesn't look quite right.";
        if ($form['message'] === '' || mb_strlen($form['message']) < 10) $errors['message'] = 'Could you add a little more so we can help?';

        $startedAt = (int)($_POST['started_at'] ?? 0);
        if ($startedAt > 0 && (time() - $startedAt) < 3) {
            $errors['_'] = 'Please take a moment before submitting.';
        }

        if (!$errors) {
            $subject = '[Plattr contact] ' . ($form['subject'] ?: 'Message from ' . $form['name']);
            $bodyLines = [
                'Name: '     . $form['name'],
                'Email: '    . $form['email'],
                'Business: ' . ($form['business'] ?: '-'),
                'Subject: '  . ($form['subject']  ?: '-'),
                '',
                'Message:',
                $form['message'],
                '',
                '---',
                'IP: ' . ($_SERVER['REMOTE_ADDR'] ?? '?'),
                'UA: ' . substr((string)($_SERVER['HTTP_USER_AGENT'] ?? '?'), 0, 200),
            ];
            $body = implode("\n", $bodyLines);
            // Postmark house send, there is no local MTA on the fleet, so the
            // old mail() call silently dropped every submission. Failures now
            // surface an honest error instead of a fake thank-you.
            if (!defined('PLATTR_ACCESS')) define('PLATTR_ACCESS', true);
            require_once __DIR__ . '/../config.env.php';
            require_once __DIR__ . '/../config.php';
            require_once __DIR__ . '/../includes/postmark-mailer.php';
            $r = pmmSendHouse(
                'accounts@onplattr.com',
                $subject,
                nl2br(htmlspecialchars($body, ENT_QUOTES | ENT_HTML5, 'UTF-8')),
                $body,
                ['reply_to' => $form['email'], 'from_name' => 'Plattr Website']
            );
            if (!empty($r['ok'])) {
                $sent = true;
            } else {
                $errors['_'] = "We couldn't send your message just now, please email us directly at accounts@onplattr.com.";
                @error_log('[contact] Postmark send failed: ' . pmmErrMsg($r));
            }
        }
    }
}

function h($v){ return htmlspecialchars((string)$v, ENT_QUOTES | ENT_HTML5, 'UTF-8'); }

$pageTitle = 'Contact Plattr, real humans, real fast.';
$pageDesc  = "Got a question? Drop us a line. We're a small team and we answer every email, usually within a few hours during NZ business hours.";
$pageSlug  = 'contact';

require __DIR__ . '/includes/page-head.php';
?>

<main class="ct">
<style>
  /* Bring Contact in line with the redesigned About page: system-sans display
     type (no Playfair serif), mono uppercase eyebrows, gradient <em>. Mirrors
     about.php's scoped override; .ct-* keeps it off the other pages.css pages. */
  .ct-hero-title, .ct-form-title, .ct-thanks-title, .ct-cta-title{
    font-family:-apple-system,BlinkMacSystemFont,'SF Pro Display','Inter',sans-serif !important;
    letter-spacing:-.03em;
  }
  .ct-hero-title, .ct-cta-title{ font-weight:900 !important; }
  .ct-form-title, .ct-thanks-title{ font-weight:800 !important; }
  .ct-hero-title em, .ct-form-title em, .ct-cta-title em{
    font-style:normal !important;
    background:linear-gradient(135deg, var(--pb-brand-light, #7AA0FB) 10%, var(--pb-brand) 90%);
    -webkit-background-clip:text; background-clip:text; color:transparent !important;
  }
  .ct-eyebrow{
    font-family:'JetBrains Mono', ui-monospace, monospace !important;
    font-weight:500 !important; font-style:normal !important;
    letter-spacing:.14em !important; text-transform:uppercase !important; font-size:11.5px !important;
  }
</style>

<!-- ═══════════════════════════════════════════════════════════════
     HERO
     ═══════════════════════════════════════════════════════════════ -->
<section class="ct-hero">
  <div class="ct-container">
    <span class="ct-eyebrow">Contact</span>
    <h1 class="ct-hero-title">
      Real humans.<br>
      <em>Real fast.</em>
    </h1>
    <p class="ct-hero-lede">
      We&rsquo;re a small team and we answer every email, usually within a few hours
      during Aotearoa business hours, and same-day after that.
    </p>
    <div class="ct-hero-meta">
      <span><i class="bx bx-envelope"></i> hello@onplattr.com</span>
      <span><i class="bx bx-time"></i> Mon–Fri 9–6 NZST</span>
      <span><i class="bx bx-check-shield"></i> Reply within 3hr</span>
    </div>
  </div>
</section>

<!-- ═══════════════════════════════════════════════════════════════
     FORM + SIDEBAR
     ═══════════════════════════════════════════════════════════════ -->
<section class="ct-section">
  <div class="ct-container ct-grid">

    <!-- ─── Form card ─────────────────────────────────────────── -->
    <div class="ct-form-card">

      <?php if ($sent): ?>
        <div class="ct-thanks">
          <div class="ct-thanks-icon"><i class="bx bx-check"></i></div>
          <h2 class="ct-thanks-title">Got it, thanks.</h2>
          <p>Your message is on its way. We&rsquo;ll get back to you shortly,
             usually the same day during NZ hours. Have a peek at your inbox
             (and occasionally the spam folder, just in case).</p>
          <a href="/" class="ct-thanks-back">← Back to Plattr</a>
        </div>

      <?php else: ?>
        <span class="ct-eyebrow ct-eyebrow--inline">01 · The form</span>
        <h2 class="ct-form-title">Tell us what&rsquo;s <em>on your mind</em>.</h2>

        <?php if (!empty($errors['_'])): ?>
          <div class="ct-banner ct-banner--err"><?= h($errors['_']) ?></div>
        <?php endif; ?>

        <form method="post" action="/contact" novalidate>
          <!-- Honeypot + time-based spam filter -->
          <div class="ct-honeypot" aria-hidden="true">
            <label>Website (leave blank)</label>
            <input type="text" name="website" tabindex="-1" autocomplete="off">
          </div>
          <input type="hidden" name="started_at" value="<?= time() ?>">

          <div class="ct-row">
            <div class="ct-field<?= isset($errors['name']) ? ' has-error' : '' ?>">
              <label for="ctName">Name <span class="ct-req">*</span></label>
              <input id="ctName" type="text" name="name" value="<?= h($form['name']) ?>" required autocomplete="name" placeholder="Your name">
              <?php if (isset($errors['name'])): ?><div class="ct-err"><?= h($errors['name']) ?></div><?php endif; ?>
            </div>
            <div class="ct-field<?= isset($errors['email']) ? ' has-error' : '' ?>">
              <label for="ctEmail">Email <span class="ct-req">*</span></label>
              <input id="ctEmail" type="email" name="email" value="<?= h($form['email']) ?>" required autocomplete="email" placeholder="you@yourbusiness.com">
              <?php if (isset($errors['email'])): ?><div class="ct-err"><?= h($errors['email']) ?></div><?php endif; ?>
            </div>
          </div>

          <div class="ct-row">
            <div class="ct-field">
              <label for="ctBusiness">Business name</label>
              <input id="ctBusiness" type="text" name="business" value="<?= h($form['business']) ?>" autocomplete="organization" placeholder="Optional">
            </div>
            <div class="ct-field">
              <label for="ctSubject">What&rsquo;s this about?</label>
              <select id="ctSubject" name="subject">
                <option value="">Pick one&hellip;</option>
                <?php foreach ([
                  'General question'      => 'General question',
                  'Pricing / plans'       => 'Pricing & plans',
                  'Demo / walkthrough'    => 'Demo or walkthrough',
                  'Migration help'        => 'Migrating from another system',
                  'Partnership / press'   => 'Partnership / press',
                  'Bug / feedback'        => 'Bug report or feedback',
                ] as $val => $lbl): ?>
                  <option value="<?= h($val) ?>" <?= $form['subject'] === $val ? 'selected' : '' ?>><?= h($lbl) ?></option>
                <?php endforeach; ?>
              </select>
            </div>
          </div>

          <div class="ct-row">
            <div class="ct-field ct-field--full<?= isset($errors['message']) ? ' has-error' : '' ?>">
              <label for="ctMessage">Your message <span class="ct-req">*</span></label>
              <textarea id="ctMessage" name="message" required rows="6" placeholder="The more context, the better, what you're running today, where it's falling short, what you're hoping Plattr can do."><?= h($form['message']) ?></textarea>
              <?php if (isset($errors['message'])): ?><div class="ct-err"><?= h($errors['message']) ?></div><?php endif; ?>
            </div>
          </div>

          <div class="ct-submit-row">
            <button type="submit" class="ct-submit">
              <span>Send message</span>
              <i class="bx bx-right-arrow-alt"></i>
            </button>
            <p class="ct-submit-note">We&rsquo;ll never share your email. Ever.</p>
          </div>
        </form>
      <?php endif; ?>
    </div>

    <!-- ─── Sidebar ────────────────────────────────────────────── -->
    <aside class="ct-side">

      <div class="ct-panel">
        <span class="ct-panel-h">Prefer direct?</span>
        <a class="ct-direct" href="mailto:hello@onplattr.com">
          <span class="ct-direct-icon"><i class="bx bx-envelope"></i></span>
          <span class="ct-direct-body">
            <strong>hello@onplattr.com</strong>
            <span>Real inbox, real humans. Usually a same-day reply.</span>
          </span>
        </a>
        <a class="ct-direct" href="https://instagram.com/onplattr" target="_blank" rel="noopener">
          <span class="ct-direct-icon"><i class="bx bxl-instagram"></i></span>
          <span class="ct-direct-body">
            <strong>@onplattr</strong>
            <span>DM us on Instagram, we check it daily.</span>
          </span>
        </a>
      </div>

      <div class="ct-panel">
        <span class="ct-panel-h">Hours &amp; response</span>
        <div class="ct-info-list">
          <div>
            <strong>NZ hours · Mon–Fri</strong>
            <span>9am – 6pm NZST / NZDT</span>
          </div>
          <div>
            <strong>First reply</strong>
            <span>Under 3 hours during business hours, same day otherwise.</span>
          </div>
          <div>
            <strong>Already a customer?</strong>
            <span>Hit the live-chat bubble inside your dashboard, you&rsquo;ll jump the queue.</span>
          </div>
        </div>
      </div>

      <div class="ct-panel">
        <span class="ct-panel-h">Fast answers</span>
        <details class="ct-faq">
          <summary>Can I get a demo first?<i class="bx bx-plus"></i></summary>
          <p>Yes. Pick &ldquo;Demo or walkthrough&rdquo; in the form and we&rsquo;ll book a 20-minute screenshare, zero pressure, just the tour.</p>
        </details>
        <details class="ct-faq">
          <summary>Do you help migrate from Square / Shopify / Toast?<i class="bx bx-plus"></i></summary>
          <p>We do. Menu, customers, order history, we handle the import. Pick &ldquo;Migrating from another system&rdquo; and we&rsquo;ll send the checklist.</p>
        </details>
        <details class="ct-faq">
          <summary>How quickly can I go live?<i class="bx bx-plus"></i></summary>
          <p>Most food businesses are live inside 48 hours. If you&rsquo;re starting fresh (no existing system to migrate), often same day.</p>
        </details>
        <details class="ct-faq">
          <summary>What if I only need one part of Plattr?<i class="bx bx-plus"></i></summary>
          <p>That&rsquo;s fine, start with just the storefront, just the POS, or any combination. Tell us what you need in the form.</p>
        </details>
      </div>

    </aside>
  </div>
</section>

<!-- ═══════════════════════════════════════════════════════════════
     CTA
     ═══════════════════════════════════════════════════════════════ -->
<section class="ct-cta">
  <div class="ct-container">
    <h2 class="ct-cta-title">Skip the form, if you like.</h2>
    <p class="ct-cta-sub">Drop your logo into the brand scanner and watch a Plattr build itself around your business in under a minute.</p>
    <div class="ct-cta-row">
      <a href="/" class="ct-cta-primary">Scan my brand <i class="bx bx-right-arrow-alt"></i></a>
      <a href="/about"   class="ct-cta-ghost">Read about us</a>
    </div>
  </div>
</section>

</main>


<?php require __DIR__ . '/includes/page-foot.php'; ?>
