<?php use App\Helpers\Helper; ?>
<div class="mb-4">
  <h1 class="admin-page-title">My Payments</h1>
</div>

<!-- Outstanding Invoices -->
<?php $outstanding = array_filter($invoices, fn($i) => in_array($i['status'],['sent','overdue','partial'])); ?>
<?php if (!empty($outstanding)): ?>
<div class="data-card mb-4">
  <div class="data-card-header">
    <h6><i class="bi bi-exclamation-triangle text-warning me-2"></i>Outstanding Invoices</h6>
  </div>
  <div class="table-responsive">
    <table class="table mb-0">
      <thead><tr><th>Invoice</th><th>Period</th><th>Total</th><th>Paid</th><th>Balance</th><th>Due</th><th>Status</th><th>Pay</th></tr></thead>
      <tbody>
        <?php foreach ($outstanding as $inv): ?>
        <tr>
          <td class="fw-600 text-primary small"><?= Helper::e($inv['invoice_number']) ?></td>
          <td class="small"><?= date('M Y', strtotime($inv['period_from'])) ?></td>
          <td><?= Helper::money($inv['total_amount']) ?></td>
          <td class="text-success"><?= Helper::money($inv['paid_amount']) ?></td>
          <td class="fw-700 text-danger"><?= Helper::money($inv['balance'] ?? $inv['total_amount'] - $inv['paid_amount']) ?></td>
          <td class="small text-danger"><?= Helper::date($inv['due_date']) ?></td>
          <td><?= Helper::statusBadge($inv['status']) ?></td>
          <td>
            <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#payModal" data-inv="<?= $inv['id'] ?>" data-amount="<?= $inv['balance'] ?? $inv['total_amount'] - $inv['paid_amount'] ?>">
              <i class="bi bi-credit-card me-1"></i>Pay
            </button>
          </td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<?php endif; ?>

<!-- Payment History -->
<div class="data-card">
  <div class="data-card-header"><h6>Payment History</h6></div>
  <div class="table-responsive">
    <table class="table mb-0">
      <thead><tr><th>Receipt</th><th>Amount</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 class="fw-700 text-success"><?= Helper::money($p['amount']) ?></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 ?>/tenant/receipt/<?= $p['id'] ?>" class="btn btn-sm btn-outline-secondary" target="_blank"><i class="bi bi-download me-1"></i>Receipt</a></td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($payments)): ?><tr><td colspan="7" class="text-center text-muted py-4">No payments yet.</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<!-- Pay Modal -->
<div class="modal fade" id="payModal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header"><h5 class="modal-title fw-700">Make Payment</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
      <div class="modal-body">
        <div class="alert alert-info small"><i class="bi bi-info-circle me-2"></i>Please pay using mobile money or bank transfer, then enter your transaction ID below.</div>
        <div class="mb-3">
          <div class="fw-700 fs-5 text-primary" id="payAmountDisplay">TZS 0</div>
          <div class="small text-muted">Amount due</div>
        </div>
        <div class="bg-light rounded-3 p-3 mb-3 small">
          <strong>M-Pesa:</strong> Paybill <?= defined('MPESA_PAYBILL') ? MPESA_PAYBILL : '123456' ?><br>
          <strong>Airtel Money:</strong> Number <?= defined('AIRTEL_NUMBER') ? AIRTEL_NUMBER : '0767000000' ?><br>
          <strong>Tigo Pesa:</strong> Number 0717000000
        </div>
        <div class="mb-3">
          <label class="form-label">Payment Method</label>
          <select class="form-select" id="payMethodSelect">
            <?php foreach ($methods as $m): ?><option value="<?= $m['id'] ?>"><?= Helper::e($m['name']) ?></option><?php endforeach; ?>
          </select>
        </div>
        <div class="mb-3">
          <label class="form-label">Transaction ID *</label>
          <input type="text" class="form-control" id="payTransId" placeholder="e.g. MP123456789" required>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary" onclick="alert('Payment confirmation sent to management. They will verify and confirm.')">Confirm Payment</button>
      </div>
    </div>
  </div>
</div>
<script>
document.getElementById('payModal')&&document.getElementById('payModal').addEventListener('show.bs.modal', function(e){
  var btn=e.relatedTarget;
  var amount=btn&&btn.dataset.amount||0;
  document.getElementById('payAmountDisplay').textContent='TZS '+Number(amount).toLocaleString();
});
</script>
