<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <h1 class="admin-page-title">Lease Agreements</h1>
  <a href="<?= APP_URL ?>/admin/leases/create" class="btn btn-primary"><i class="bi bi-plus me-1"></i>New Lease</a>
</div>

<div class="d-flex gap-2 mb-4 flex-wrap">
  <a href="<?= APP_URL ?>/admin/leases" class="btn btn-sm <?= !$status?'btn-primary':'btn-outline-secondary' ?>">All</a>
  <?php foreach (['pending','active','expired','terminated','renewed'] as $s): ?>
  <a href="<?= APP_URL ?>/admin/leases?status=<?= $s ?>" class="btn btn-sm btn-outline-secondary <?= $status===$s?'active':'' ?>">
    <?= ucfirst($s) ?>
  </a>
  <?php endforeach; ?>
</div>

<div class="data-card">
  <div class="table-responsive">
    <table class="table mb-0">
      <thead><tr><th>Lease No</th><th>Tenant</th><th>Property / Unit</th><th>Monthly Rent</th><th>Start</th><th>End</th><th>Status</th><th>Actions</th></tr></thead>
      <tbody>
        <?php foreach ($leases as $l): ?>
        <tr>
          <td class="fw-600 text-primary small"><?= Helper::e($l['lease_number']) ?></td>
          <td>
            <div class="fw-600 small"><?= Helper::e($l['tenant_name']) ?></div>
            <div class="text-muted" style="font-size:0.72rem"><?= Helper::e($l['phone']) ?></div>
          </td>
          <td class="small"><?= Helper::e($l['property_name']) ?> / <?= Helper::e($l['unit_number']) ?></td>
          <td class="fw-700 text-primary"><?= Helper::money($l['monthly_rent']) ?></td>
          <td class="small"><?= Helper::date($l['start_date']) ?></td>
          <td class="small">
            <span class="<?= strtotime($l['end_date']??'') < time() ? 'text-danger fw-600' : '' ?>">
              <?= Helper::date($l['end_date']) ?>
            </span>
          </td>
          <td><?= Helper::statusBadge($l['status']) ?></td>
          <td>
            <div class="d-flex gap-1">
              <a href="<?= APP_URL ?>/admin/leases/<?= $l['id'] ?>" class="btn btn-action btn-outline-info" title="View"><i class="bi bi-eye"></i></a>
              <a href="<?= APP_URL ?>/admin/leases/<?= $l['id'] ?>/edit" class="btn btn-action btn-outline-primary" title="Edit"><i class="bi bi-pencil"></i></a>
              <a href="<?= APP_URL ?>/admin/leases/<?= $l['id'] ?>/pdf" class="btn btn-action btn-outline-secondary" title="PDF" target="_blank"><i class="bi bi-file-pdf"></i></a>
            </div>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($leases)): ?>
        <tr><td colspan="8" class="text-center py-5 text-muted"><i class="bi bi-file-earmark-text fs-1 d-block mb-2"></i>No leases found.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
