<?php ob_start(); ?>

<?php
$flashSuccess = flash('contact_success');
$flashError   = flash('contact_error');
$formErrors   = flash('contact_errors') ?: [];
$formOld      = flash('contact_old') ?: [];
?>

<section class="page-section">
    <div class="container container-narrow">
        <?php if ($system_page['show_title'] ?? 1): ?>
        <h1 class="page-title"><?= e($system_page['title'] ?? 'Contact') ?></h1>
        <?php endif; ?>

        <?php if (!empty($system_page['content'])): ?>
        <div class="page-content module-intro"><?= $system_page['content'] ?></div>
        <?php endif; ?>

        <?php if ($flashSuccess): ?>
        <div class="contact-alert contact-alert--success" role="alert">
            <?= e($flashSuccess) ?>
        </div>
        <?php endif; ?>
        <?php if ($flashError): ?>
        <div class="contact-alert contact-alert--error" role="alert">
            <?= e($flashError) ?>
        </div>
        <?php endif; ?>

        <div class="contact-info">
            <?php if ($email = setting('contact_email')): ?>
            <p>Email: <a href="mailto:<?= e($email) ?>"><?= e($email) ?></a></p>
            <?php endif; ?>
            <?php if ($phone = setting('contact_phone')): ?>
            <p>Phone: <a href="tel:<?= e(preg_replace('/[^+\d]/', '', $phone)) ?>"><?= e($phone) ?></a></p>
            <?php endif; ?>
        </div>

        <h2 class="contact-section-title">Send a Message</h2>

        <form method="POST" action="/contact" novalidate class="contact-form">
            <?= csrf_field() ?>

            <!-- Honeypot: hidden from real users -->
            <div style="display:none;" aria-hidden="true">
                <input type="text" name="website" tabindex="-1" autocomplete="off" value="">
            </div>

            <div>
                <label for="cf_name">Name *</label>
                <input type="text" id="cf_name" name="name"
                       value="<?= e($formOld['name'] ?? '') ?>"
                       placeholder="Your name"
                       class="<?= isset($formErrors['name']) ? 'is-error' : '' ?>"
                       required>
                <?php if (isset($formErrors['name'])): ?>
                <p class="field-error"><?= e($formErrors['name']) ?></p>
                <?php endif; ?>
            </div>

            <div>
                <label for="cf_email">Email *</label>
                <input type="email" id="cf_email" name="email"
                       value="<?= e($formOld['email'] ?? '') ?>"
                       placeholder="you@example.com"
                       class="<?= isset($formErrors['email']) ? 'is-error' : '' ?>"
                       required>
                <?php if (isset($formErrors['email'])): ?>
                <p class="field-error"><?= e($formErrors['email']) ?></p>
                <?php endif; ?>
            </div>

            <div>
                <label for="cf_subject">Subject *</label>
                <input type="text" id="cf_subject" name="subject"
                       value="<?= e($formOld['subject'] ?? '') ?>"
                       placeholder="Subject"
                       class="<?= isset($formErrors['subject']) ? 'is-error' : '' ?>"
                       required>
                <?php if (isset($formErrors['subject'])): ?>
                <p class="field-error"><?= e($formErrors['subject']) ?></p>
                <?php endif; ?>
            </div>

            <div>
                <label for="cf_message">Message *</label>
                <textarea id="cf_message" name="message"
                          placeholder="Your message …"
                          rows="6"
                          class="<?= isset($formErrors['message']) ? 'is-error' : '' ?>"
                          required><?= e($formOld['message'] ?? '') ?></textarea>
                <?php if (isset($formErrors['message'])): ?>
                <p class="field-error"><?= e($formErrors['message']) ?></p>
                <?php endif; ?>
            </div>

            <div>
                <button type="submit" class="btn btn-primary">Send Message</button>
            </div>
        </form>
    </div>
</section>

<?php $content = ob_get_clean(); include __DIR__ . '/layout.php'; ?>
