<?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">
            <?= e($flashSuccess) ?>
        </div>
        <?php endif; ?>
        <?php if ($flashError): ?>
        <div class="contact-alert contact-alert--error">
            <?= 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>

        <div class="contact-form-section">
            <h2 class="contact-form-heading">Send a Message</h2>
            <form method="POST" action="/contact" novalidate class="contact-form">
                <?= csrf_field() ?>

                <!-- Honeypot: hidden from real users -->
                <div class="contact-form-honeypot" aria-hidden="true">
                    <input type="text" name="website" tabindex="-1" autocomplete="off" value="">
                </div>

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

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

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

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

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

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