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

$developments = get_developments();
$devHero = brand_asset('hero-estate-aerial') ?: brand_asset('hero-villa-complex') ?: 'assets/images/hero/home-hero-2.jpg';

$seo = [
    'title' => 'New Development | Off-Plan & New Projects in Kigali | ' . SITE_NAME,
    'description' => 'Explore off-plan residential estates, new commercial developments and upcoming investment opportunities across Kigali, Rwanda.',
    'path' => '/new-development',
];
include __DIR__ . '/includes/header.php';
?>
<section class="hero" style="min-height:55vh;">
  <div class="hero-media"><img src="/<?= e($devHero) ?>" alt="New off-plan residential development in Kigali" fetchpriority="high"></div>
  <div class="container hero-content">
    <div class="eyebrow" style="color:var(--color-accent-light);"><?= e(t('new_development.eyebrow')) ?></div>
    <h1><?= e(t('new_development.h1')) ?></h1>
    <p class="lede"><?= e(t('new_development.lede')) ?></p>
    <div class="hero-cta-row">
      <a href="/contact?subject=New%20Development" class="btn btn-accent btn-lg"><?= e(t('cta.talk_to_agent')) ?></a>
      <a href="/vip-alerts" class="btn btn-outline-light btn-lg">Get Notified of New Projects</a>
    </div>
  </div>
</section>

<section class="section-pad">
  <div class="container">
    <?php if (empty($developments)): ?>
      <div class="form-card text-center">
        <h3>New projects are being finalised</h3>
        <p class="text-muted">Check back soon, or join VIP Property Alerts to hear about them first.</p>
        <a href="/vip-alerts" class="btn btn-accent mt-4">Join VIP Property Alerts</a>
      </div>
    <?php endif; ?>
    <div class="grid grid-2">
      <?php foreach ($developments as $i => $dev): ?>
        <?php
        $district = get_district_by_id((int)$dev['district_id']);
        $highlights = json_decode($dev['highlights'] ?: '[]', true);
        ?>
        <article class="card reveal" data-delay="<?= $i % 2 + 1 ?>" style="display:flex;flex-direction:column;">
          <div class="media-frame aspect-4-3" style="position:relative;">
            <img src="/<?= e($dev['cover_image']) ?>" alt="<?= e($dev['name']) ?>" loading="lazy" width="600" height="450">
            <span class="badge badge-outline" style="position:absolute;top:var(--space-4);left:var(--space-4);background:var(--color-white);"><?= e(development_status_label($dev['status'])) ?></span>
          </div>
          <div class="body" style="padding:var(--space-6);display:flex;flex-direction:column;gap:var(--space-3);flex:1;">
            <h3 style="margin-bottom:0;"><?= e($dev['name']) ?></h3>
            <div class="loc"><?= icon('map-pin', 14) ?> <?= e($district['name'] ?? '') ?></div>
            <div class="flex gap-6">
              <div>
                <div class="text-xs text-muted uppercase">Starting From</div>
                <div class="fw-bold"><?= e($dev['currency']) ?> <?= money((float)$dev['starting_price']) ?></div>
              </div>
              <div>
                <div class="text-xs text-muted uppercase">Completion</div>
                <div class="fw-bold"><?= $dev['completion_date'] ? date('M Y', strtotime($dev['completion_date'])) : 'TBC' ?></div>
              </div>
            </div>
            <p class="text-muted text-sm"><?= e(truncate($dev['description'], 150)) ?></p>
            <?php if (!empty($highlights)): ?>
            <ul class="feature-grid" style="grid-template-columns:1fr;">
              <?php foreach (array_slice($highlights, 0, 4) as $h): ?><li><?= icon('check', 15) ?> <?= e($h) ?></li><?php endforeach; ?>
            </ul>
            <?php endif; ?>
            <div class="card-actions" style="margin-top:auto;flex-wrap:wrap;">
              <a href="/schedule-tour.php" class="btn btn-primary btn-sm">Book a Visit</a>
              <button type="button" class="btn btn-outline btn-sm brochure-trigger" data-dev-name="<?= e($dev['name']) ?>">Request Brochure</button>
              <a href="/contact?subject=<?= urlencode($dev['name']) ?>" class="btn btn-outline btn-sm">Contact Agent</a>
            </div>
          </div>
        </article>
      <?php endforeach; ?>
    </div>
  </div>
</section>

<!-- Request Brochure modal -->
<div id="brochureModal" class="lightbox" style="background:rgba(10,10,10,0.7);">
  <div class="form-card" style="max-width:420px;width:92%;position:relative;">
    <button type="button" class="lb-close" id="brochureClose" style="position:absolute;top:-16px;right:-16px;background:var(--color-black);color:#fff;"><?= icon('close', 16) ?></button>
    <h3>Request Brochure</h3>
    <p class="text-muted text-sm mb-4" id="brochureDevName"></p>
    <form data-ajax action="/api/request-brochure.php" method="post">
      <input type="text" name="website" class="visually-hidden" tabindex="-1" autocomplete="off">
      <input type="hidden" name="development_name" id="brochureDevInput">
      <div class="field mb-3"><label for="brochure_name">Full Name</label><input type="text" id="brochure_name" name="name" required></div>
      <div class="field mb-3"><label for="brochure_email">Email</label><input type="email" id="brochure_email" name="email" required></div>
      <div class="field mb-4"><label for="brochure_phone">Phone (optional)</label><input type="tel" id="brochure_phone" name="phone"></div>
      <button type="submit" class="btn btn-accent btn-block">Send Me the Brochure</button>
    </form>
  </div>
</div>

<script>
(function () {
  var modal = document.getElementById('brochureModal');
  var nameEl = document.getElementById('brochureDevName');
  var inputEl = document.getElementById('brochureDevInput');
  document.querySelectorAll('.brochure-trigger').forEach(function (btn) {
    btn.addEventListener('click', function () {
      var devName = btn.getAttribute('data-dev-name');
      nameEl.textContent = devName;
      inputEl.value = devName;
      modal.classList.add('is-open');
    });
  });
  document.getElementById('brochureClose').addEventListener('click', function () { modal.classList.remove('is-open'); });
  modal.addEventListener('click', function (e) { if (e.target === modal) modal.classList.remove('is-open'); });
})();
</script>
<?php include __DIR__ . '/includes/footer.php'; ?>
