<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="admin-page-title">Payments</h1>
    <p class="text-muted small mb-0">Total collected: <strong class="text-success"><?= Helper::money($totalAmount) ?></strong></p>
  </div>
  <a href="<?= APP_URL ?>/admin/payments/create" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>Record Payment</a>
</div>

<!-- Filters -->
<div class="form-card mb-3">
  <form method="GET" class="row g-2 align-items-end">
    <div class="col-md-3">
      <label class="form-label">Tenant</label>
      <input type="text" name="tenant" class="form-control" placeholder="Name or phone..." value="<?= Helper::e($tenantSearch) ?>">
    </div>
    <div class="col-md-2">
      <label class="form-label">Status</label>
      <select name="status" class="form-select">
        <option value="">All Status</option>
        <?php foreach (['completed','pending','failed','refunded'] as $s): ?>
        <option value="<?= $s ?>" <?= $status===$s?'selected':'' ?>><?= ucfirst($s) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-2">
      <label class="form-label">From</label>
      <input type="date" name="from" class="form-control" value="<?= $from ?>">
    </div>
    <div class="col-md-2">
      <label class="form-label">To</label>
      <input type="date" name="to" class="form-control" value="<?= $to ?>">
    </div>
    <div class="col-md-2">
      <button class="btn btn-primary w-100">Filter</button>
    </div>
    <div class="col-md-1">
      <a href="<?= APP_URL ?>/admin/payments" class="btn btn-outline-secondary w-100">Clear</a>
    </div>
  </form>
</div>

<div class="data-card">
  <div class="table-responsive">
    <table class="table mb-0">
      <thead>
        <tr><th>Receipt</th><th>Tenant</th><th>Unit</th><th>Amount</th><th>Period</th><th>For</th><th>Method</th><th>Date</th><th>Status</th><th>Action</th></tr>
      </thead>
      <tbody>
        <?php foreach ($payments as $p): ?>
        <tr>
          <td class="fw-600 text-primary small"><?= Helper::e($p['receipt_number'] ?? $p['payment_ref']) ?></td>
          <td>
            <div class="fw-600 small"><?= Helper::e($p['tenant_name']) ?></div>
            <div class="text-muted" style="font-size:0.72rem"><?= Helper::e($p['tenant_phone']) ?></div>
          </td>
          <td class="small"><?= Helper::e($p['unit_number'] ?? '—') ?></td>
          <td>
            <div class="fw-700 text-success"><?= Helper::money($p['amount']) ?></div>
            <?php $bp = $p['billing_period'] ?? 'monthly'; ?>
            <span class="badge bg-<?= $bp==='daily'?'danger':($bp==='weekly'?'warning':'success') ?> bg-opacity-15 text-<?= $bp==='daily'?'danger':($bp==='weekly'?'warning':'success') ?>" style="font-size:0.68rem"><?= ucfirst($bp) ?></span>
          </td>
          <td>
            <?php if ($p['period_from'] && $p['period_to']): ?>
            <div style="font-size:0.72rem" class="text-muted"><?= Helper::date($p['period_from']) ?></div>
            <div style="font-size:0.72rem" class="text-muted">to <?= Helper::date($p['period_to']) ?></div>
            <?php else: ?><span class="text-muted small">—</span><?php endif; ?>
          </td>
          <td><span class="badge bg-light text-dark"><?= ucfirst(str_replace('_',' ',$p['payment_for'])) ?></span></td>
          <td class="small"><?= Helper::e($p['method_name'] ?? 'Cash') ?></td>
          <td class="small text-muted"><?= Helper::date($p['payment_date']) ?></td>
          <td><?= Helper::statusBadge($p['status']) ?></td>
          <td>
            <a href="<?= APP_URL ?>/admin/payments/<?= $p['id'] ?>" class="btn btn-action btn-outline-info" title="View Receipt"><i class="bi bi-receipt"></i></a>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($payments)): ?>
        <tr><td colspan="9" class="text-center py-5 text-muted">No payments found.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
  <?php if ($pagination['totalPages'] > 1): ?>
  <div class="p-3"><?= Helper::paginator($pagination, APP_URL.'/admin/payments') ?></div>
  <?php endif; ?>
</div>
