<?php
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/seo.php';

$propertyId = !empty($_GET['property']) ? (int)$_GET['property'] : null;
$property = null;
if ($propertyId) {
    $stmt = db()->prepare('SELECT * FROM properties WHERE id = ?');
    $stmt->execute([$propertyId]);
    $property = $stmt->fetch();
}

$seo = [
    'title' => 'Schedule a Property Tour | ' . SITE_NAME,
    'description' => 'Book a private viewing of any property listed with Kigali Yacu Properties at a time that suits you.',
    'path' => '/schedule-tour',
];
include __DIR__ . '/includes/header.php';
?>
<section class="section-pad-sm">
  <div class="container-narrow">
    <?= breadcrumb_html([['name' => 'Schedule a Tour', 'url' => '/schedule-tour']]) ?>
    <div class="section-head center">
      <div class="eyebrow" style="justify-content:center;">Book a Viewing</div>
      <h1>Schedule a Private Tour</h1>
      <p><?= $property ? 'Request a viewing for <strong>' . e($property['title']) . '</strong>.' : 'Tell us which property you\'d like to see and when works best for you.' ?></p>
    </div>

    <div class="form-card reveal">
      <form data-ajax action="/api/schedule-tour.php" method="post">
        <input type="text" name="website" class="visually-hidden" tabindex="-1" autocomplete="off">
        <?php if ($property): ?><input type="hidden" name="property_id" value="<?= (int)$property['id'] ?>"><?php endif; ?>
        <?php if ($property): ?>
          <div class="flex items-center gap-4 mb-6" style="background:var(--color-cream);padding:var(--space-4);border-radius:var(--radius-md);">
            <div class="media-frame rounded-sm" style="width:80px;height:60px;flex-shrink:0;"><img src="/<?= e(get_property_primary_image((int)$property['id']) ?: 'assets/images/properties/villa-ext-a.jpg') ?>" alt="<?= e($property['title']) ?>" width="80" height="60"></div>
            <div><strong><?= e($property['title']) ?></strong><div class="text-xs text-muted"><?= format_price((float)$property['price'], $property['currency'], $property['price_period']) ?></div></div>
          </div>
        <?php else: ?>
          <div class="field mb-4">
            <label for="property_search">Which property?</label>
            <input type="text" id="property_search" name="notes" placeholder="Property name or link" data-search-suggest>
          </div>
        <?php endif; ?>
        <div class="field-row mb-4">
          <div class="field"><label for="name">Full Name</label><input type="text" id="name" name="name" required></div>
          <div class="field"><label for="email">Email</label><input type="email" id="email" name="email" required></div>
        </div>
        <div class="field-row mb-4">
          <div class="field"><label for="phone">Phone / WhatsApp</label><input type="tel" id="phone" name="phone" required></div>
          <div class="field"><label for="preferred_date">Preferred Date</label><input type="date" id="preferred_date" name="preferred_date"></div>
        </div>
        <?php if ($property): ?>
        <div class="field mb-4"><label for="notes">Notes</label><textarea id="notes" name="notes" placeholder="Any preferred time of day or questions before the viewing?"></textarea></div>
        <?php endif; ?>
        <button type="submit" class="btn btn-accent btn-block"><?= icon('calendar', 16) ?> Request Tour</button>
      </form>
    </div>
  </div>
</section>
<?php include __DIR__ . '/includes/footer.php'; ?>
