<?php
$page_title = 'Contact Us - SOL Christian Academy';
$page_image = 'assets/images/hero-3.jpg';
$current_page = 'contact.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';

$success = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = sanitize($_POST['name'] ?? '');
    $email = sanitize($_POST['email'] ?? '');
    $subject = sanitize($_POST['subject'] ?? '');
    $message = sanitize($_POST['message'] ?? '');
    $token = $_POST['csrf_token'] ?? '';

    if (!verify_csrf($token)) {
        $error = 'Invalid form submission. Please try again.';
    } elseif (empty($name) || empty($email) || empty($message)) {
        $error = 'Please fill in all required fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Please enter a valid email address.';
    } elseif ($pdo) {
        $stmt = $pdo->prepare("INSERT INTO contact_messages (name, email, subject, message) VALUES (?, ?, ?, ?)");
        if ($stmt->execute([$name, $email, $subject, $message])) {
            $success = 'Thank you for your message. We will get back to you shortly.';
        } else {
            $error = 'Something went wrong. Please try again later.';
        }
    } else {
        $error = 'Database unavailable. Please email us directly.';
    }
}

require_once 'includes/header.php';
?>

<section class="page-header page-header-image" style="--hero-bg: url('<?php echo $page_image; ?>');">
  <div class="container">
    <div class="breadcrumb-custom mb-2">
      <a href="index.php">Home</a> / Contact
    </div>
    <h1>Contact Us</h1>
    <p>We would love to hear from you</p>
  </div>
</section>

<section class="content-section">
  <div class="container">
    <div class="row g-5">
      <div class="col-lg-5">
        <h2 class="section-title" style="font-size:1.5rem;">Get in Touch</h2>
        <p class="text-muted mb-4">Have a question about admissions, our curriculum, or anything else? We are here to help.</p>

        <div class="d-flex align-items-start gap-3 contact-info-card">
          <div class="icon"><i class="ri-phone-line"></i></div>
          <div>
            <h6>Phone</h6>
            <p><?php echo SITE_PHONE; ?></p>
          </div>
        </div>
        <div class="d-flex align-items-start gap-3 contact-info-card">
          <div class="icon"><i class="ri-mail-line"></i></div>
          <div>
            <h6>Email</h6>
            <p><?php echo SITE_EMAIL; ?></p>
          </div>
        </div>
        <div class="d-flex align-items-start gap-3 contact-info-card">
          <div class="icon"><i class="ri-map-pin-2-line"></i></div>
          <div>
            <h6>Address</h6>
            <p><?php echo SITE_ADDRESS; ?></p>
          </div>
        </div>
        <div class="d-flex align-items-start gap-3 contact-info-card">
          <div class="icon"><i class="ri-time-line"></i></div>
          <div>
            <h6>Office Hours</h6>
            <p>Monday – Friday: 8:00 AM – 4:00 PM</p>
          </div>
        </div>

        <div class="mt-4" style="border-radius:16px;overflow:hidden;">
          <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2375.123!2d-2.219!3d53.474!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x0!2zNTPCsDI4JzI2LjQiTiAywrAxMycwOC40Ilc!5e0!3m2!1sen!2suk!4v1" width="100%" height="250" style="border:0;" allowfullscreen loading="lazy"></iframe>
        </div>
      </div>

      <div class="col-lg-7">
        <div class="contact-form-wrapper">
          <?php if ($success): ?>
          <div class="alert alert-success"><?php echo $success; ?></div>
          <?php endif; ?>
          <?php if ($error): ?>
          <div class="alert alert-danger"><?php echo $error; ?></div>
          <?php endif; ?>

          <form method="post" novalidate>
            <input type="hidden" name="csrf_token" value="<?php echo csrf_token(); ?>">
            <div class="row g-3">
              <div class="col-md-6">
                <label class="form-label fw-semibold">Full Name <span class="text-danger">*</span></label>
                <input type="text" name="name" class="form-control" required>
              </div>
              <div class="col-md-6">
                <label class="form-label fw-semibold">Email Address <span class="text-danger">*</span></label>
                <input type="email" name="email" class="form-control" required>
              </div>
              <div class="col-12">
                <label class="form-label fw-semibold">Subject</label>
                <input type="text" name="subject" class="form-control">
              </div>
              <div class="col-12">
                <label class="form-label fw-semibold">Message <span class="text-danger">*</span></label>
                <textarea name="message" class="form-control" rows="6" required></textarea>
              </div>
              <div class="col-12">
                <a href="#" onclick="this.closest('form').submit();return false;" class="btn-shell" style="display:inline-flex;width:100%;justify-content:center;">
                  <span class="btn-core" style="width:100%;justify-content:center;">
                    Send Message
                    <span class="icon-ring"><i class="ri-send-plane-line"></i></span>
                  </span>
                </a>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</section>

<!-- Map -->
<section class="content-section" style="padding-top:0;">
  <div class="container">
    <div class="contact-map">
      <iframe src="https://www.google.com/maps?q=115+Fairfield+Street+Manchester+M12+6EL+UK&output=embed&z=15" loading="lazy" referrerpolicy="no-referrer-when-downgrade" title="SOL Christian Academy location"></iframe>
    </div>
  </div>
</section>

<?php require_once 'includes/footer.php'; ?>
