<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="admin-page-title">Tenants</h1>
    <p class="text-muted small mb-0"><?= $pagination['total'] ?> tenants found</p>
  </div>
  <a href="<?= APP_URL ?>/admin/tenants/create" class="btn btn-primary"><i class="bi bi-person-plus me-1"></i>Add Tenant</a>
</div>

<!-- Search -->
<div class="data-card mb-3" style="border-radius:12px">
  <div class="p-3">
    <form method="GET" class="d-flex gap-2">
      <input type="text" name="search" class="form-control" placeholder="Search by name, phone, code..." value="<?= Helper::e($search) ?>">
      <button class="btn btn-primary px-4">Search</button>
      <?php if ($search): ?><a href="<?= APP_URL ?>/admin/tenants" class="btn btn-outline-secondary">Clear</a><?php endif; ?>
    </form>
  </div>
</div>

<div class="data-card">
  <div class="table-responsive">
    <table class="table mb-0">
      <thead>
        <tr><th>#</th><th>Code</th><th>Tenant</th><th>Phone</th><th>Nationality</th><th>Active Lease</th><th>Balance</th><th>Status</th><th>Actions</th></tr>
      </thead>
      <tbody>
        <?php foreach ($tenants as $i => $t):
          $rowNum = ($pagination['offset'] ?? 0) + $i + 1;
          $lease = null;
          try { $db = \Core\Database::getInstance(); $lease = $db->fetch("SELECT l.*, u.unit_number FROM leases l JOIN units u ON u.id=l.unit_id WHERE l.tenant_id=? AND l.status='active' LIMIT 1", [$t['id']]); } catch(Exception $e){}
          $balance = 0;
          try { $balance = (float)$db->fetchColumn("SELECT COALESCE(SUM(balance),0) FROM invoices WHERE tenant_id=? AND status!='cancelled'", [$t['id']]); } catch(Exception $e){}
        ?>
        <tr>
          <td class="text-muted small fw-600"><?= $rowNum ?></td>
          <td class="fw-600 text-primary small"><?= Helper::e($t['tenant_code']) ?></td>
          <td>
            <div class="d-flex align-items-center gap-2">
              <div class="avatar-sm bg-primary d-flex align-items-center justify-content-center text-white fw-700 rounded-circle flex-shrink-0">
                <?= strtoupper(substr($t['first_name'],0,1)) ?>
              </div>
              <div>
                <div class="fw-600"><?= Helper::e($t['first_name'].' '.$t['last_name']) ?></div>
                <div class="text-muted" style="font-size:0.75rem"><?= Helper::e($t['occupation'] ?? '—') ?></div>
              </div>
            </div>
          </td>
          <td><?= Helper::e($t['phone']) ?></td>
          <td class="small"><?= Helper::e($t['nationality'] ?? 'Tanzanian') ?></td>
          <td class="small"><?= $lease ? Helper::e($lease['unit_number']) : '<span class="text-muted">—</span>' ?></td>
          <td class="<?= $balance > 0 ? 'text-danger fw-600' : 'text-success' ?>"><?= Helper::money($balance) ?></td>
          <td><?= Helper::statusBadge($t['status']) ?></td>
          <td>
            <div class="d-flex gap-1">
              <a href="<?= APP_URL ?>/admin/tenants/<?= $t['id'] ?>" class="btn btn-action btn-outline-info" title="View"><i class="bi bi-eye"></i></a>
              <a href="<?= APP_URL ?>/admin/tenants/<?= $t['id'] ?>/edit" class="btn btn-action btn-outline-primary" title="Edit"><i class="bi bi-pencil"></i></a>
            </div>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($tenants)): ?>
        <tr><td colspan="9" class="text-center py-5 text-muted"><i class="bi bi-people fs-1 d-block mb-2"></i>No tenants found.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
  <?php if ($pagination['totalPages'] > 1): ?>
  <div class="p-3"><?= Helper::paginator($pagination, APP_URL.'/admin/tenants') ?></div>
  <?php endif; ?>
</div>
