<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="admin-page-title">Rent Collection Report</h1>
    <p class="text-muted small">Total: <strong class="text-success"><?= Helper::money($total) ?></strong> | Period: <?= Helper::date($from) ?> — <?= Helper::date($to) ?></p>
  </div>
  <div class="d-flex gap-2">
    <button onclick="window.print()" class="btn btn-outline-primary btn-sm"><i class="bi bi-printer me-1"></i>Print PDF</button>
    <form method="POST" action="<?= APP_URL ?>/admin/reports/export-excel" class="d-inline">
      <?= Helper::csrf() ?>
      <input type="hidden" name="report_type" value="rent-collection">
      <input type="hidden" name="from" value="<?= $from ?>">
      <input type="hidden" name="to" value="<?= $to ?>">
      <button class="btn btn-success btn-sm"><i class="bi bi-file-earmark-excel me-1"></i>Export Excel</button>
    </form>
    <a href="<?= APP_URL ?>/admin/reports" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left me-1"></i>Back</a>
  </div>
</div>

<!-- Filters -->
<div class="form-card mb-4">
  <form method="GET" class="row g-2 align-items-end">
    <div class="col-md-3">
      <label class="form-label">From Date</label>
      <input type="date" name="from" class="form-control" value="<?= $from ?>">
    </div>
    <div class="col-md-3">
      <label class="form-label">To Date</label>
      <input type="date" name="to" class="form-control" value="<?= $to ?>">
    </div>
    <div class="col-md-3">
      <label class="form-label">Property</label>
      <select name="property_id" class="form-select">
        <option value="">All Properties</option>
        <?php foreach ($properties as $p): ?>
        <option value="<?= $p['id'] ?>" <?= $propId==$p['id']?'selected':'' ?>><?= Helper::e($p['name']) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-3">
      <button class="btn btn-primary w-100">Generate</button>
    </div>
  </form>
</div>

<div class="data-card">
  <div class="table-responsive">
    <table class="table mb-0">
      <thead><tr><th>Receipt No</th><th>Tenant</th><th>Unit</th><th>Property</th><th>Amount</th><th>Method</th><th>Date</th><th>Status</th></tr></thead>
      <tbody>
        <?php foreach ($data as $row): ?>
        <tr>
          <td class="fw-600 text-primary small"><?= Helper::e($row['receipt_number'] ?? $row['payment_ref']) ?></td>
          <td class="small"><?= Helper::e($row['tenant_name']) ?></td>
          <td class="small"><?= Helper::e($row['unit_number'] ?? '—') ?></td>
          <td class="small text-muted"><?= Helper::e($row['property_name'] ?? '—') ?></td>
          <td class="fw-700"><?= Helper::money($row['amount']) ?></td>
          <td class="small"><?= Helper::e($row['method_name'] ?? 'Cash') ?></td>
          <td class="small text-muted"><?= Helper::date($row['payment_date']) ?></td>
          <td><?= Helper::statusBadge($row['status']) ?></td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($data)): ?>
        <tr><td colspan="8" class="text-center py-4 text-muted">No payments in selected period.</td></tr>
        <?php endif; ?>
      </tbody>
      <?php if (!empty($data)): ?>
      <tfoot class="table-light fw-700">
        <tr><td colspan="4" class="text-end">TOTAL COLLECTED:</td><td class="text-success"><?= Helper::money($total) ?></td><td colspan="3"></td></tr>
      </tfoot>
      <?php endif; ?>
    </table>
  </div>
</div>
<style>@media print { .admin-topbar, .admin-sidebar, .form-card, .d-flex.mb-4 { display:none!important; } .admin-main { margin:0; } }</style>
