<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="admin-page-title">Properties</h1>
    <p class="text-muted small mb-0"><?= count($properties) ?> properties found</p>
  </div>
  <a href="<?= APP_URL ?>/admin/properties/create" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>Add Property</a>
</div>

<div class="data-card">
  <div class="data-card-header">
    <h6>All Properties</h6>
    <input type="text" id="tableSearch" class="form-control form-control-sm" style="width:220px" placeholder="Search...">
  </div>
  <div class="table-responsive">
    <table class="table mb-0" id="propertiesTable">
      <thead>
        <tr><th>Code</th><th>Property</th><th>Type</th><th>Location</th><th>Units</th><th>Occupied</th><th>Status</th><th>Actions</th></tr>
      </thead>
      <tbody>
        <?php foreach ($properties as $p): ?>
        <tr>
          <td class="fw-600 text-primary small"><?= Helper::e($p['property_code']) ?></td>
          <td>
            <div class="fw-600"><?= Helper::e($p['name']) ?></div>
            <?php if ($p['is_featured']): ?><span class="badge bg-warning text-dark" style="font-size:0.65rem">Featured</span><?php endif; ?>
          </td>
          <td><span class="badge bg-light text-dark"><?= Helper::e($p['type_name'] ?? '—') ?></span></td>
          <td class="small text-muted"><?= Helper::e($p['region_name'] ?? '—') ?><?= $p['area'] ? ', '.$p['area'] : '' ?></td>
          <td class="text-center"><strong><?= $p['total_units'] ?? 0 ?></strong></td>
          <td class="text-center">
            <span class="text-danger fw-600"><?= $p['occupied_units'] ?? 0 ?></span>
            / <span class="text-success"><?= $p['available_units'] ?? 0 ?> free</span>
          </td>
          <td><?= Helper::statusBadge($p['status']) ?></td>
          <td>
            <div class="d-flex gap-1">
              <a href="<?= APP_URL ?>/admin/properties/<?= $p['id'] ?>" class="btn btn-action btn-outline-info" title="View"><i class="bi bi-eye"></i></a>
              <a href="<?= APP_URL ?>/admin/properties/<?= $p['id'] ?>/edit" class="btn btn-action btn-outline-primary" title="Edit"><i class="bi bi-pencil"></i></a>
              <form method="POST" action="<?= APP_URL ?>/admin/properties/<?= $p['id'] ?>/delete" class="form-delete d-inline">
                <?= Helper::csrf() ?>
                <button class="btn btn-action btn-outline-danger" title="Delete"><i class="bi bi-trash"></i></button>
              </form>
            </div>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($properties)): ?>
        <tr><td colspan="8" class="text-center py-5 text-muted"><i class="bi bi-buildings fs-1 d-block mb-2"></i>No properties found. <a href="<?= APP_URL ?>/admin/properties/create">Add your first property</a></td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<script>
document.getElementById('tableSearch').addEventListener('input', function(){
  var q = this.value.toLowerCase();
  document.querySelectorAll('#propertiesTable tbody tr').forEach(function(r){
    r.style.display = r.textContent.toLowerCase().includes(q) ? '' : 'none';
  });
});
</script>
