<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <h1 class="admin-page-title">Invoices</h1>
  <div class="d-flex gap-2">
    <button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#generateModal"><i class="bi bi-lightning me-1"></i>Generate Invoices</button>
    <a href="<?= APP_URL ?>/admin/invoices?status=overdue" class="btn btn-outline-danger"><i class="bi bi-exclamation-triangle me-1"></i>Overdue (<?= $summary['overdue'] ?>)</a>
  </div>
</div>

<!-- Summary Cards -->
<div class="row g-3 mb-4">
  <?php foreach ([
    ['label'=>'Total Billed','value'=>Helper::money($summary['total_billed']),'color'=>'blue','icon'=>'bi-receipt'],
    ['label'=>'Total Collected','value'=>Helper::money($summary['total_paid']),'color'=>'green','icon'=>'bi-check-circle'],
    ['label'=>'Overdue Invoices','value'=>$summary['overdue'],'color'=>'red','icon'=>'bi-exclamation-triangle'],
    ['label'=>'Overdue Amount','value'=>Helper::money($summary['overdue_amt']),'color'=>'orange','icon'=>'bi-cash'],
  ] as $s): ?>
  <div class="col-md-3">
    <div class="admin-stat-card">
      <div class="stat-icon-wrap <?= $s['color'] ?>"><i class="bi <?= $s['icon'] ?>"></i></div>
      <div><div class="stat-val" style="font-size:<?= strlen($s['value'])>10?'1.1rem':'1.5rem' ?>"><?= $s['value'] ?></div><div class="stat-lbl"><?= $s['label'] ?></div></div>
    </div>
  </div>
  <?php endforeach; ?>
</div>

<!-- Status filter -->
<div class="d-flex gap-2 mb-3 flex-wrap">
  <?php foreach ([''=> 'All', 'draft'=>'Draft', 'sent'=>'Sent', 'partial'=>'Partial', 'paid'=>'Paid', 'overdue'=>'Overdue', 'cancelled'=>'Cancelled'] as $s=>$l): ?>
  <a href="<?= APP_URL ?>/admin/invoices?status=<?= $s ?>" class="btn btn-sm <?= $status===$s?'btn-primary':'btn-outline-secondary' ?>"><?= $l ?></a>
  <?php endforeach; ?>
</div>

<div class="data-card">
  <div class="table-responsive">
    <table class="table mb-0">
      <thead><tr><th>Invoice No</th><th>Tenant</th><th>Unit</th><th>Period</th><th>Total</th><th>Paid</th><th>Balance</th><th>Due</th><th>Status</th><th>Action</th></tr></thead>
      <tbody>
        <?php foreach ($invoices as $inv): ?>
        <tr>
          <td class="fw-600 text-primary small"><?= Helper::e($inv['invoice_number']) ?></td>
          <td class="small"><?= Helper::e($inv['tenant_name']) ?></td>
          <td class="small"><?= Helper::e($inv['unit_number']) ?></td>
          <td class="small text-muted"><?= 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="<?= $inv['balance']>0?'text-danger fw-700':'' ?>"><?= Helper::money($inv['balance']??$inv['total_amount']-$inv['paid_amount']) ?></td>
          <td class="small <?= strtotime($inv['due_date']??'')<time()&&$inv['status']!=='paid'?'text-danger':'' ?>"><?= Helper::date($inv['due_date']) ?></td>
          <td><?= Helper::statusBadge($inv['status']) ?></td>
          <td>
            <a href="<?= APP_URL ?>/admin/invoices/<?= $inv['id'] ?>/pdf" class="btn btn-sm btn-outline-secondary" target="_blank"><i class="bi bi-file-pdf"></i></a>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php if (empty($invoices)): ?><tr><td colspan="10" class="text-center py-4 text-muted">No invoices found.</td></tr><?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<!-- Generate Modal -->
<div class="modal fade" id="generateModal" tabindex="-1">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header"><h5 class="modal-title fw-700">Generate Monthly Invoices</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
      <form method="POST" action="<?= APP_URL ?>/admin/invoices/generate">
        <?= Helper::csrf() ?>
        <div class="modal-body">
          <label class="form-label">Select Month</label>
          <input type="month" name="month" class="form-control" value="<?= date('Y-m') ?>">
          <div class="form-text mt-2">This will create invoices for all active leases for the selected month.</div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-primary btn-sm">Generate</button>
        </div>
      </form>
    </div>
  </div>
</div>
