<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<div class="page-header">
  <div><h1><i class="bi bi-cash-coin me-2"></i>Payments</h1></div>
  <a href="<?= base_url('finance/invoices') ?>" class="btn btn-fm-outline btn-sm">All Invoices</a>
</div>

<div class="row g-3 mb-3">
  <div class="col-md-4"><div class="kpi-card kpi-orange"><div class="kpi-label">Outstanding</div><div class="kpi-value" style="font-size:1.1rem"><?= $currency ?> <?= number_format((float)($stats['outstanding']??0),2) ?></div><div class="kpi-sub"><?= (int)($stats['pending_count']??0) ?> invoices</div></div></div>
  <div class="col-md-4"><div class="kpi-card kpi-green"><div class="kpi-label">Collected (YTD)</div><div class="kpi-value" style="font-size:1.1rem"><?= $currency ?> <?= number_format((float)($stats['collected_ytd']??0),2) ?></div></div></div>
</div>

<div class="row g-3">
  <div class="col-lg-7">
    <div class="fm-card">
      <div class="card-header-fm"><h5><i class="bi bi-hourglass-split me-2"></i>Pending Collection</h5></div>
      <div class="fm-card-body p-0">
        <table class="fm-table">
          <thead><tr><th>Invoice</th><th>Facility</th><th>Due</th><th>Status</th><th class="text-end">Amount</th><th></th></tr></thead>
          <tbody>
          <?php foreach($pending as $inv): ?>
          <tr>
            <td><a href="<?= base_url('finance/invoices/view/'.$inv['id']) ?>" class="fw-semibold text-primary"><?= esc($inv['invoice_number']) ?></a></td>
            <td class="small"><?= esc($inv['facility_name']??'') ?></td>
            <td class="small"><?= date('d M Y', strtotime($inv['due_date'])) ?></td>
            <td><span class="fm-badge badge-status-<?= esc($inv['status']) ?>"><?= ucfirst($inv['status']) ?></span></td>
            <td class="text-end fw-semibold"><?= $currency ?> <?= number_format((float)$inv['total'],2) ?></td>
            <td>
              <?php if(in_array(session()->get('user_role'), ['super_admin','finance_manager'], true)): ?>
              <?= form_open(base_url('finance/payments/record/'.$inv['id']), ['class'=>'d-inline-flex gap-1 align-items-center']) ?>
              <?= csrf_field() ?>
              <input type="number" name="amount" class="form-control form-control-sm" style="width:90px" step="0.01" min="0.01" max="<?= (float)$inv['total'] ?>" value="<?= (float)$inv['total'] ?>" title="Amount">
              <select name="payment_method" class="form-select form-select-sm" style="width:80px">
                <option value="bank">Bank</option><option value="cash">Cash</option><option value="card">Card</option><option value="online">Online</option>
              </select>
              <button type="submit" class="btn btn-success btn-sm" onclick="return confirm('Record payment?')">Pay</button>
              <?= form_close() ?>
              <?php endif; ?>
            </td>
          </tr>
          <?php endforeach; ?>
          <?php if(empty($pending)): ?><tr><td colspan="6" class="text-center py-4 text-muted">No pending payments</td></tr><?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
  <div class="col-lg-5">
    <div class="fm-card">
      <div class="card-header-fm"><h5><i class="bi bi-check-circle me-2"></i>Recently Paid</h5></div>
      <div class="fm-card-body p-0">
        <?php foreach($paidRecent as $inv): ?>
        <div class="d-flex justify-content-between px-3 py-2 border-bottom border-light">
          <div><div class="small fw-semibold"><?= esc($inv['invoice_number']) ?></div><div class="x-small text-muted"><?= esc($inv['facility_name']??'') ?></div></div>
          <div class="text-end"><div class="small fw-bold text-success"><?= $currency ?> <?= number_format((float)$inv['total'],2) ?></div><div class="x-small text-muted"><?= $inv['paid_at']?date('d M Y',strtotime($inv['paid_at'])):'' ?></div></div>
        </div>
        <?php endforeach; ?>
        <?php if(empty($paidRecent)): ?><p class="text-center py-4 text-muted small">No recent payments</p><?php endif; ?>
      </div>
    </div>
  </div>
</div>
<?= $this->endSection() ?>
