<?php use App\Helpers\Helper; ?>
<section class="page-header">
  <div class="container">
    <h1 class="fw-700 mb-2">Search Properties</h1>
    <nav aria-label="breadcrumb"><ol class="breadcrumb mb-0"><li class="breadcrumb-item"><a href="<?= APP_URL ?>/">Home</a></li><li class="breadcrumb-item active">Search</li></ol></nav>
  </div>
</section>

<section class="py-5">
  <div class="container">
    <!-- Advanced Filters -->
    <div class="search-box mb-5">
      <form method="GET" action="<?= APP_URL ?>/search">
        <div class="row g-3">
          <div class="col-md-4 col-lg-3">
            <label class="form-label small fw-600">Keyword</label>
            <input type="text" name="keyword" class="form-control" placeholder="Area, property name..." value="<?= Helper::e($filters['keyword']) ?>">
          </div>
          <div class="col-md-3 col-lg-2">
            <label class="form-label small fw-600">Property Type</label>
            <select name="type_id" class="form-select">
              <option value="">All Types</option>
              <?php foreach ($types as $t): ?><option value="<?= $t['id'] ?>" <?= $filters['type_id']==$t['id']?'selected':'' ?>><?= Helper::e($t['name']) ?></option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-2 col-lg-2">
            <label class="form-label small fw-600">Region</label>
            <select name="region_id" class="form-select" id="region_id">
              <option value="">All Regions</option>
              <?php foreach ($regions as $r): ?><option value="<?= $r['id'] ?>" <?= $filters['region_id']==$r['id']?'selected':'' ?>><?= Helper::e($r['name']) ?></option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-2 col-lg-2">
            <label class="form-label small fw-600">Bedrooms</label>
            <select name="bedrooms" class="form-select">
              <option value="">Any</option>
              <?php foreach ([1,2,3,4,5] as $b): ?><option value="<?= $b ?>" <?= $filters['bedrooms']==$b?'selected':'' ?>><?= $b ?>+</option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-2 col-lg-2">
            <label class="form-label small fw-600">Max Price</label>
            <select name="max_price" class="form-select">
              <option value="">Any Price</option>
              <?php foreach ([200000,500000,800000,1200000,2000000,5000000] as $p): ?><option value="<?= $p ?>" <?= $filters['max_price']==$p?'selected':'' ?>><?= Helper::money($p) ?></option><?php endforeach; ?>
            </select>
          </div>
          <div class="col-md-2 col-lg-1">
            <label class="form-label small fw-600">&nbsp;</label>
            <button type="submit" class="btn btn-primary w-100"><i class="bi bi-search"></i></button>
          </div>
        </div>
        <div class="row g-3 mt-1">
          <div class="col-auto"><div class="form-check"><input class="form-check-input" type="checkbox" name="is_furnished" value="1" <?= $filters['is_furnished']?'checked':'' ?>><label class="form-check-label small">Furnished</label></div></div>
          <div class="col-auto"><div class="form-check"><input class="form-check-input" type="checkbox" name="has_parking" value="1" <?= $filters['has_parking']?'checked':'' ?>><label class="form-check-label small">Parking</label></div></div>
        </div>
      </form>
    </div>

    <div class="d-flex justify-content-between align-items-center mb-4">
      <h5 class="fw-700"><?= count($properties) ?> Properties Found</h5>
      <?php if (array_filter($filters)): ?>
      <a href="<?= APP_URL ?>/search" class="btn btn-sm btn-outline-secondary"><i class="bi bi-x me-1"></i>Clear Filters</a>
      <?php endif; ?>
    </div>

    <?php if (empty($properties)): ?>
    <div class="text-center py-5">
      <i class="bi bi-search text-muted" style="font-size:4rem"></i>
      <h4 class="mt-3 fw-700">No Properties Found</h4>
      <p class="text-muted">Try adjusting your search filters or <a href="<?= APP_URL ?>/residences">browse all properties</a>.</p>
    </div>
    <?php else: ?>
    <div class="row g-4">
      <?php foreach ($properties as $p): ?>
      <div class="col-lg-4 col-md-6">
        <div class="card property-card h-100 border-0">
          <div class="property-img-wrap">
            <img src="https://images.unsplash.com/photo-1560448204-e02f11c3d0e2?w=600&q=60" alt="<?= Helper::e($p['name']) ?>" loading="lazy">
            <div class="property-badge"><span class="badge bg-<?= ($p['available_units']??0)>0?'success':'danger' ?>"><?= ($p['available_units']??0)>0?'Available':'Full' ?></span></div>
          </div>
          <div class="property-body">
            <h5 class="fw-700 mb-1 fs-6"><a href="<?= APP_URL ?>/residences/<?= $p['id'] ?>" class="text-decoration-none text-dark stretched-link"><?= Helper::e($p['name']) ?></a></h5>
            <p class="text-muted small mb-2"><i class="bi bi-geo-alt me-1"></i><?= Helper::e($p['region_name']??'') ?><?= $p['area']?', '.$p['area']:'' ?></p>
            <div class="property-meta mb-3">
              <span><i class="bi bi-door-open me-1"></i><?= $p['total_units']??0 ?> Units</span>
              <span><i class="bi bi-check-circle text-success me-1"></i><?= $p['available_units']??0 ?> Free</span>
            </div>
            <?php if ($p['min_rent']): $mo=$p['min_rent']; $wk=$mo/4; $dy=$mo/30; ?>
            <div class="d-flex gap-2 mb-3">
              <div class="flex-fill text-center rounded-2 py-2" style="background:#f0f9ff;border:1px solid #bae6fd">
                <div class="fw-700" style="font-size:.8rem;color:#2563eb"><?= Helper::money($dy) ?></div>
                <div class="text-muted" style="font-size:.68rem">/day</div>
              </div>
              <div class="flex-fill text-center rounded-2 py-2" style="background:#f0fdf4;border:1px solid #bbf7d0">
                <div class="fw-700" style="font-size:.8rem;color:#16a34a"><?= Helper::money($wk) ?></div>
                <div class="text-muted" style="font-size:.68rem">/week</div>
              </div>
              <div class="flex-fill text-center rounded-2 py-2" style="background:#faf5ff;border:1px solid #e9d5ff">
                <div class="fw-700" style="font-size:.8rem;color:#7c3aed"><?= Helper::money($mo) ?></div>
                <div class="text-muted" style="font-size:.68rem">/month</div>
              </div>
            </div>
            <?php endif; ?>
            <div class="d-flex justify-content-end pt-2" style="border-top:1px solid #f1f5f9">
              <a href="<?= APP_URL ?>/residences/<?= $p['id'] ?>" class="btn btn-sm btn-primary fw-600 position-relative px-3" style="z-index:1">
                <i class="bi bi-eye me-1"></i>View Units
              </a>
            </div>
          </div>
        </div>
      </div>
      <?php endforeach; ?>
    </div>
    <?php endif; ?>
  </div>
</section>
