<?php
$recaptchaSiteKey = '';
$mailConfigPath = dirname(__DIR__, 3) . '/breathe-mail-config.php';

if (file_exists($mailConfigPath)) {
    $mailConfig = require $mailConfigPath;
    $recaptchaSiteKey = isset($mailConfig['recaptcha_site_key']) ? trim($mailConfig['recaptcha_site_key']) : '';
}
?>

<style>
.contact-section-three .contact-form-status {
    display: none;
    border-radius: 8px;
    margin-bottom: 24px;
    padding: 16px 18px;
    font-size: 15px;
    line-height: 1.55;
}
.contact-section-three .contact-form-status.is-success {
    display: block;
    color: #235a3b;
    background-color: #e9f7ef;
    border: 1px solid #bfe7ce;
}
.contact-section-three .contact-form-status.is-error {
    display: block;
    color: #7a302d;
    background-color: #fbeeee;
    border: 1px solid #f0c8c5;
}
.contact-section-three .g-recaptcha {
    margin-bottom: 24px;
}
</style>

<!-- Contact Section -->
<section class="contact-section-three pt-0" id="contact">
    <div class="auto-container">
        <div class="flower-8"></div>
        <div class="outer-box">
        <div class="row g-0">
            <!-- form-column -->
            <div class="form-column col-lg-7 col-md-12 col-sm-12 wow fadeInLeft" data-wow-delay="300ms">
            <div class="inner-column">
                <div class="icon-flower-15"></div>

                <!-- Contact Form -->
                <div class="contact-form-two wow fadeInLeft">
                <h3 class="title">Get in touch with us</h3>
                <!--Contact Form-->
                <form method="post" action="includes/sendmail.php" id="home1-contact-form" class="contact-email-us-form" data-inline-submit="true">
                    <div class="row">
                    <div class="form-group col-lg-6 col-md-6 col-sm-6">
                        <!-- <div class="radio-box">
                            <label class="custom-radio-box-two">Accept with pleasure
                                <input type="radio" name="radio">
                                <span class="checkmark"></span>
                            </label>
                        </div>
                    </div>
                    <div class="form-group col-lg-6 col-md-6 col-sm-6">
                        <div class="radio-box">
                            <label class="custom-radio-box-two">Decline with regret
                                <input type="radio" name="radio">
                                <span class="checkmark"></span>
                            </label>
                        </div> -->
                    </div>
                    <div class="form-group col-lg-12">
                        <div class="input-outer">
                        <input type="text" name="form_name" placeholder="Your Name" required />
                        </div>
                    </div>
                    <div class="form-group col-lg-6">
                        <div class="input-outer">
                        <input type="tel" name="form_phone" placeholder="Phone" />
                        </div>
                    </div>
                    <div class="form-group col-lg-6">
                        <div class="input-outer">
                        <input type="email" name="form_email" placeholder="Email" required />
                        </div>
                    </div>
                    <div class="form-group col-lg-12">
                        <select class="custom-select" name="form_subject" required>
                        <option value="">Subject</option>
                        <option value="General Question">General Question</option>
                        <option value="Prices">Prices</option>
                        <option value="Bookings">Bookings</option>
                        <option value="Other">Other</option>
                        </select>
                    </div>
                    <div class="form-group col-lg-12">
                        <div class="input-outer">
                        <textarea name="form_message" placeholder="Message" required></textarea>
                        </div>
                    </div>
                    <?php if ($recaptchaSiteKey !== ''): ?>
                    <div class="form-group col-lg-12">
                        <div class="g-recaptcha" data-sitekey="<?php echo htmlspecialchars($recaptchaSiteKey, ENT_QUOTES, 'UTF-8'); ?>"></div>
                    </div>
                    <?php endif; ?>
                    <div class="form-group col-lg-12">
                        <div class="contact-form-status" role="status" aria-live="polite"></div>
                    </div>
                    <div class="form-group col-lg-12">
                        <div class="input-outer">
                        <input name="form_botcheck" type="hidden" value="" />
                        <button type="submit" name="submit-form" class="theme-btn btn-style-one" data-loading-text="Please wait..."><span class="btn-title">SEND</span></button>
                        </div>
                    </div>
                    </div>
                </form>
                </div>
                <!--End Contact Form -->
            </div>
            </div>

            <!-- Content Column -->
            <div class="content-column col-lg-5 col-md-12 col-sm-12 wow fadeInRight">
            <div class="inner-column">
                <div class="icon-leave"></div>
                <div class="contact-btn">
                <h6 class="title">Call us</h6>
                <a href="tel:+36-55-540-069" class="num">(473) 414-9451</a>
                </div>
                <div class="content-box">
                <h6 class="title">eMail</h6>
                <div class="text">
                breathe1w@gmail.com

                </div>
                </div>
                <div class="content-box">
                <h6 class="title">Location</h6>
                <div class="text">
                Birchgrove, St. Andrews <br />
                Grenada W.I.
                </div>
                </div>
            </div>
            </div>
        </div>
        </div>
    </div>
</section>
<!-- End Contact Section -->

<?php if ($recaptchaSiteKey !== ''): ?>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<?php endif; ?>

<script>
document.addEventListener('DOMContentLoaded', function() {
    var form = document.querySelector('#home1-contact-form');

    if (!form) {
        return;
    }

    form.addEventListener('submit', function(event) {
        event.preventDefault();

        var statusBox = form.querySelector('.contact-form-status');
        var submitButton = form.querySelector('button[type="submit"]');
        var submitText = submitButton ? submitButton.querySelector('.btn-title') : null;
        var originalText = submitText ? submitText.textContent : '';

        if (statusBox) {
            statusBox.className = 'contact-form-status';
            statusBox.style.display = 'none';
            statusBox.textContent = '';
        }

        if (submitButton) {
            submitButton.disabled = true;
        }

        if (submitText) {
            submitText.textContent = submitButton.getAttribute('data-loading-text') || 'Please wait...';
        }

        fetch(form.getAttribute('action'), {
            method: form.getAttribute('method') || 'POST',
            body: new FormData(form),
            headers: {
                'Accept': 'application/json'
            }
        })
            .then(function(response) {
                return response.json();
            })
            .then(function(response) {
                if (!statusBox) {
                    return;
                }

                if (response && response.status === 'true') {
                    statusBox.className = 'contact-form-status is-success';
                    statusBox.style.display = 'block';
                    statusBox.textContent = 'We have successfully received your message and will get back to you as soon as possible.';
                    form.reset();
                    if (typeof grecaptcha !== 'undefined') {
                        grecaptcha.reset();
                    }
                } else {
                    statusBox.className = 'contact-form-status is-error';
                    statusBox.style.display = 'block';
                    statusBox.textContent = response && response.message ? stripHtml(response.message) : 'Email could not be sent. Please try again later.';
                    if (typeof grecaptcha !== 'undefined') {
                        grecaptcha.reset();
                    }
                }
            })
            .catch(function() {
                if (statusBox) {
                    statusBox.className = 'contact-form-status is-error';
                    statusBox.style.display = 'block';
                    statusBox.textContent = 'Email could not be sent. Please try again later.';
                }
                if (typeof grecaptcha !== 'undefined') {
                    grecaptcha.reset();
                }
            })
            .finally(function() {
                if (submitButton) {
                    submitButton.disabled = false;
                }

                if (submitText) {
                    submitText.textContent = originalText;
                }
            });
    });

    function stripHtml(html) {
        var div = document.createElement('div');
        div.innerHTML = html;
        return div.textContent || div.innerText || '';
    }
});
</script>
