<?php
require_once 'includes/helpers.php';
require_once 'includes/header.php';
require_once 'includes/footer.php';

$errors  = [];
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name    = trim($_POST['name'] ?? '');
    $email   = trim($_POST['email'] ?? '');
    $phone   = trim($_POST['phone'] ?? '');
    $subject = trim($_POST['subject'] ?? '');
    $message = trim($_POST['message'] ?? '');

    if (!$name)    $errors[] = 'Name is required.';
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'A valid email is required.';
    if (!$message) $errors[] = 'Message cannot be empty.';

    if (empty($errors)) {
        $stmt = db()->prepare(
            'INSERT INTO contact_messages (name, email, phone, subject, message)
             VALUES (?, ?, ?, ?, ?)'
        );
        $stmt->execute([$name, $email, $phone, $subject, $message]);
        $success = true;
    }
}

renderHeader('Contact Us');
?>

<div class="page-hero">
  <div class="container">
    <span class="section-label">Get In Touch</span>
    <h1 class="section-title">Contact &amp; Directions</h1>
    <p class="section-sub" style="margin:0 auto;text-align:center">
      Questions? Need assistance? Our support team is available 24/7 to help.
    </p>
  </div>
</div>

<section class="section">
  <div class="container">
    <div class="contact-grid">

      <!-- Left: Info blocks -->
      <div class="contact-info">

        <div class="info-block">
          <div class="info-icon"><i class="fa fa-location-dot"></i></div>
          <div>
            <div class="info-label">OUR LOCATION</div>
            <div class="info-value">Gate 7, Airport City Ring Road<br/>Terminal Zone, Bengaluru – 560300</div>
          </div>
        </div>

        <div class="info-block">
          <div class="info-icon"><i class="fa fa-phone"></i></div>
          <div>
            <div class="info-label">PHONE</div>
            <div class="info-value">
              <a href="tel:+919876543210">+91 98765 43210</a><br/>
              <a href="tel:+918012345678" style="font-size:0.88rem;color:var(--text-muted)">+91 80 1234 5678 (Office)</a>
            </div>
          </div>
        </div>

        <div class="info-block">
          <div class="info-icon"><i class="fa fa-envelope"></i></div>
          <div>
            <div class="info-label">EMAIL</div>
            <div class="info-value">
              <a href="mailto:hello@airportrestarena.com">hello@airportrestarena.com</a><br/>
              <a href="mailto:bookings@airportrestarena.com" style="font-size:0.88rem;color:var(--text-muted)">bookings@airportrestarena.com</a>
            </div>
          </div>
        </div>

        <div class="info-block">
          <div class="info-icon"><i class="fa fa-clock"></i></div>
          <div>
            <div class="info-label">HOURS</div>
            <div class="info-value">Open 24 hours / 7 days<br/><span style="color:var(--teal);font-size:0.88rem">365 days a year — never closed</span></div>
          </div>
        </div>

        <div class="info-block">
          <div class="info-icon"><i class="fa fa-route"></i></div>
          <div>
            <div class="info-label">DIRECTIONS</div>
            <div class="info-value">
              <p style="font-size:0.9rem;line-height:1.6;margin-bottom:8px">Take the Airport Expressway exit towards Terminal 2. Follow signs to "Driver Services Zone". We're at Gate 7 on the ring road — look for the amber signage.</p>
              <a href="https://maps.google.com/?q=Airport+City+Bengaluru" target="_blank" class="btn-outline-sm" style="display:inline-flex;align-items:center;gap:6px">
                <i class="fa fa-map"></i> Open in Google Maps
              </a>
            </div>
          </div>
        </div>
      </div>

      <!-- Right: Contact form -->
      <div>
        <?php if ($success): ?>
        <div style="background:rgba(0,201,167,0.1);border:1px solid var(--teal);border-radius:var(--radius);padding:48px;text-align:center">
          <div style="font-size:3rem;color:var(--teal);margin-bottom:16px"><i class="fa fa-circle-check"></i></div>
          <h3 style="font-family:var(--font-head);font-size:2rem;margin-bottom:8px">Message Sent!</h3>
          <p style="color:var(--text-muted)">Thank you for reaching out. Our team will get back to you within 2 hours.</p>
          <a href="contact.php" class="btn btn-outline" style="margin-top:20px">Send Another</a>
        </div>
        <?php else: ?>
        <div class="form-card">
          <h2 style="font-family:var(--font-head);font-size:2rem;margin-bottom:8px">Send Us a Message</h2>
          <p style="color:var(--text-muted);font-size:0.92rem;margin-bottom:28px">We typically respond within 2 hours.</p>

          <?php if ($errors): ?>
          <div style="background:rgba(224,81,81,0.1);border:1px solid var(--red);border-radius:8px;padding:14px 18px;margin-bottom:20px;color:var(--red);font-family:var(--font-cond);font-size:0.9rem">
            <?php foreach ($errors as $e): ?><div>⚠ <?= h($e) ?></div><?php endforeach; ?>
          </div>
          <?php endif; ?>

          <form method="POST" action="contact.php">
            <div class="form-grid">
              <div class="form-group">
                <label>YOUR NAME *</label>
                <input type="text" name="name" placeholder="John Doe"
                       value="<?= h($_POST['name'] ?? '') ?>" required />
              </div>
              <div class="form-group">
                <label>EMAIL ADDRESS *</label>
                <input type="email" name="email" placeholder="driver@example.com"
                       value="<?= h($_POST['email'] ?? '') ?>" required />
              </div>
              <div class="form-group">
                <label>PHONE NUMBER</label>
                <input type="tel" name="phone" placeholder="+91 98765 43210"
                       value="<?= h($_POST['phone'] ?? '') ?>" />
              </div>
              <div class="form-group">
                <label>SUBJECT</label>
                <select name="subject">
                  <option value="">— Select subject —</option>
                  <option value="Booking Inquiry">Booking Inquiry</option>
                  <option value="Feedback">Feedback</option>
                  <option value="Complaint">Complaint</option>
                  <option value="Directions">Directions Help</option>
                  <option value="Partnership">Business Partnership</option>
                  <option value="Other">Other</option>
                </select>
              </div>
              <div class="form-group full">
                <label>MESSAGE *</label>
                <textarea name="message" placeholder="Tell us how we can help you..."><?= h($_POST['message'] ?? '') ?></textarea>
              </div>
              <div class="form-group full">
                <button type="submit" class="btn btn-amber" style="width:100%;justify-content:center">
                  <i class="fa fa-paper-plane"></i> Send Message
                </button>
              </div>
            </div>
          </form>
        </div>
        <?php endif; ?>
      </div>
    </div>

    <!-- Map embed placeholder -->
    <div style="margin-top:64px;border-radius:var(--radius-lg);overflow:hidden;border:1px solid var(--border);height:400px;position:relative">
      <div style="position:absolute;inset:0;background:var(--bg2);display:grid;place-items:center;text-align:center">
        <div>
          <div style="font-size:2.5rem;color:var(--amber);margin-bottom:12px"><i class="fa fa-map-location-dot"></i></div>
          <h4 style="font-family:var(--font-head);font-size:1.4rem;margin-bottom:8px">Gate 7, Airport City Ring Road</h4>
          <p style="color:var(--text-muted);margin-bottom:16px">Terminal Zone, Bengaluru – 560300</p>
          <a href="https://maps.google.com/?q=Bangalore+International+Airport" target="_blank" class="btn btn-amber">
            <i class="fa fa-map"></i> Open Google Maps
          </a>
          <!-- Replace the div above with an actual Google Maps embed iframe once you have a Maps API key -->
        </div>
      </div>
    </div>
  </div>
</section>

<?php renderFooter(); ?>
