<?php
require_once __DIR__ . '/config/database.php';

$contactSuccess = false;
$contactError = '';

$name = trim((string)($_POST['name'] ?? ''));
$email = trim((string)($_POST['email'] ?? ''));
$phone = trim((string)($_POST['phone'] ?? ''));
$subject = trim((string)($_POST['subject'] ?? ''));
$message = trim((string)($_POST['message'] ?? ''));

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($subject === '') {
        $subject = 'General Inquiry';
    }

    if ($name === '' || $email === '' || $phone === '' || $message === '') {
        $contactError = 'Please fill in all required fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $contactError = 'Please enter a valid email address.';
    } else {
        $stmt = $conn->prepare("INSERT INTO contact_messages (name, email, phone, subject, message) VALUES (?, ?, ?, ?, ?)");
        if ($stmt) {
            $stmt->bind_param('sssss', $name, $email, $phone, $subject, $message);
            if ($stmt->execute()) {
                $contactSuccess = true;
                $name = '';
                $email = '';
                $phone = '';
                $subject = '';
                $message = '';
            } else {
                $contactError = 'Unable to save your message right now.';
            }
            $stmt->close();
        } else {
            $contactError = 'Unable to process your request.';
        }
    }

    // Keep compatibility with existing AJAX validation-contact.js.
    $isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower((string)$_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
    if ($isAjax) {
        echo $contactSuccess ? 'sent' : 'error';
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="zxx">

<head>
    <title>zemaraim — Hotel Website Template</title>
    <link rel="icon" href="images/icon.webp" type="image/gif" sizes="16x16">
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="width=device-width, initial-scale=1.0" name="viewport" >
    <meta content="zemaraim — Hotel Website Template" name="description" >
    <meta content="" name="keywords" >
    <meta content="" name="author" >
    <!-- CSS Files
    ================================================== -->
    <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" id="bootstrap">
    <link href="css/plugins.css" rel="stylesheet" type="text/css" >
    <link href="css/swiper.css" rel="stylesheet" type="text/css" >
    <link href="css/style.css" rel="stylesheet" type="text/css" >
    <link href="css/coloring.css" rel="stylesheet" type="text/css" >
    <!-- color scheme -->
    <link id="colors" href="css/colors/scheme-01.css" rel="stylesheet" type="text/css" >

</head>

<body>
    <div id="wrapper">
        <a href="#" id="back-to-top"></a>
        
        <!-- page preloader begin -->
        <div id="de-loader"></div>
        <!-- page preloader close -->

        <!-- header begin -->
        <?php include 'header.php'; ?>
        <!-- header close -->
        <!-- content begin -->
        <div class="no-bottom no-top" id="content">
            <div id="top"></div>
            <section class="page-breadcrumb-hero" style="--page-hero-bg: url('images/background/3.webp');">
                <div class="container">
                    <div class="page-breadcrumb-content text-center">
                        <span class="page-breadcrumb-kicker">Guest Support</span>
                        <h1>Contact Us</h1>
                        <p>Need help with bookings or special requests? Our team is available to support your stay plans.</p>
                        <ul class="page-breadcrumb-list">
                            <li><a href="index.php">Home</a></li>
                            <li class="active">Contact Us</li>
                        </ul>
                    </div>
                </div>
            </section>

            <!-- contact section-->
            <section class="contact-modern-section">
                <div class="container">
                    <div class="contact-modern-wrap">
                        <div class="row g-4 g-lg-5">
                            <div class="col-lg-7">
                                <form name="contactForm" id="contact_form" class="contact-modern-form position-relative z1000" method="post" action="contact.php">
                                    <div id="contact_form_wrap" class="row g-3">
                                        <?php if ($contactError !== ''): ?>
                                            <div class="col-12">
                                                <div class="alert alert-danger mb-0"><?php echo htmlspecialchars($contactError, ENT_QUOTES, 'UTF-8'); ?></div>
                                            </div>
                                        <?php endif; ?>
                                        <?php if ($contactSuccess): ?>
                                            <div class="col-12">
                                                <div class="alert alert-success mb-0">Your message has been sent successfully.</div>
                                            </div>
                                        <?php endif; ?>
                                        <div class="col-12">
                                            <span class="contact-kicker">Talk to Us</span>
                                            <h3 class="contact-title">Plan your perfect hotel stay with our team.</h3>
                                            <p class="contact-subtitle">Send us your travel dates or special requests, and we will help you with room options and booking support.</p>
                                        </div>

                                        <div class="col-md-6">
                                            <div class="field-set mb-0">
                                                <input type="text" name="name" id="name" class="form-control" placeholder="Your Name" value="<?php echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8'); ?>" required>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="field-set mb-0">
                                                <input type="text" name="email" id="email" class="form-control" placeholder="Your Email" value="<?php echo htmlspecialchars($email, ENT_QUOTES, 'UTF-8'); ?>" required>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="field-set mb-0">
                                                <input type="text" name="phone" id="phone" class="form-control" placeholder="Your Phone" value="<?php echo htmlspecialchars($phone, ENT_QUOTES, 'UTF-8'); ?>" required>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="field-set mb-0">
                                                <input type="text" name="subject" id="subject" class="form-control" placeholder="Subject" value="<?php echo htmlspecialchars($subject, ENT_QUOTES, 'UTF-8'); ?>">
                                            </div>
                                        </div>
                                        <div class="col-12">
                                            <div class="field-set mb-0">
                                                <textarea name="message" id="message" class="form-control" placeholder="Your Message" required><?php echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); ?></textarea>
                                            </div>
                                        </div>

                                        <div class="col-12">
                                            <div class="contact-submit-wrap">
                                                <input type="submit" id="send_message" value="Send Message" class="btn-main">
                                            </div>
                                        </div>
                                    </div>

                                    <div id="success_message" class="success">
                                        Your message has been sent successfully. Refresh this page if you want to send more messages.
                                    </div>
                                    <div id="error_message" class="error">
                                        Sorry there was an error sending your form.
                                    </div>
                                </form>
                            </div>

                            <div class="col-lg-5">
                                <aside class="contact-side-card">
                                    <h4>Booking Assistance</h4>
                                    <p>Need help choosing a room or completing your reservation? Our support team is available every day.</p>

                                    <ul class="contact-side-list">
                                        <li><i class="icofont-check-circled"></i> Fast response from hotel staff</li>
                                        <li><i class="icofont-check-circled"></i> Reservation and room guidance</li>
                                        <li><i class="icofont-check-circled"></i> Flexible booking support</li>
                                    </ul>

                                    <div class="contact-side-meta">
                                        <div>
                                            <span>Phone</span>
                                            <strong>+929 333 9296</strong>
                                        </div>
                                        <div>
                                            <span>Email</span>
                                            <strong>contact@zemaraim.com</strong>
                                        </div>
                                        <div>
                                            <span>Address</span>
                                            <strong>742 Evergreen Terrace, Brooklyn, NY</strong>
                                        </div>
                                    </div>

                                    <a href="reservation.php" class="btn-main">Book Your Stay</a>
                                </aside>
                            </div>
                        </div>
                    </div>
                </div>
            </section>            

        </div>
        <!-- content close -->
        <!-- footer begin -->
        <?php include 'footer.php'; ?>
        <!-- footer close -->
    </div>

    <!-- Javascript Files
    ================================================== -->
    <script src="js/plugins.js"></script>
    <script src="js/designesia.js"></script>
    <script src="js/validation-contact.js"></script>
   
</body>

</html>
