<?php $this->start('body'); ?>
<?php $rows = $this->data['rows']; $banks = $this->data['banks']; $filters = $this->data['filters']; ?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-cash-register me-2 text-muted"></i>Bank Reconciliation</h5>
    <div class="text-muted small">Import a bank statement, match transactions against the ledger, post adjustments.</div>
  </div>
  <a href="<?= PROOT; ?>BankRecon/add" class="btn btn-sm btn-primary"><i class="fas fa-file-import me-1"></i>Import statement</a>
</div>

<?= flashHtml(); ?>

<!-- Filter bar -->
<form method="GET" class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2 d-flex flex-wrap gap-2 align-items-end">
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">Bank</label>
      <select name="bank_account_id" class="form-select form-select-sm">
        <option value="0">All banks</option>
        <?php foreach ($banks as $b): ?>
          <option value="<?= (int)$b['c0d']; ?>" <?= ((int)$filters['bank_account_id'] === (int)$b['c0d']) ? 'selected' : ''; ?>>
            <?= htmlspecialchars($b['account_code']); ?> · <?= htmlspecialchars($b['account_name']); ?>
          </option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">Status</label>
      <select name="status" class="form-select form-select-sm">
        <?php foreach (['' => 'All', 'OPEN' => 'Open', 'RECONCILED' => 'Reconciled', 'VOID' => 'Void'] as $k => $v): ?>
          <option value="<?= $k; ?>" <?= ($filters['status'] === $k) ? 'selected' : ''; ?>><?= $v; ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-sync me-1"></i>Filter</button>
  </div>
</form>

<div class="card border-0 shadow-sm">
  <div class="table-responsive">
    <table class="table table-sm mb-0 align-middle">
      <thead class="table-light">
        <tr>
          <th>Bank</th>
          <th>Period</th>
          <th class="money">Opening</th>
          <th class="money">Closing</th>
          <th class="text-center">Lines</th>
          <th class="text-center">Matched</th>
          <th>Status</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($rows)): ?>
          <tr><td colspan="8" class="text-center text-muted py-5">
            No statements yet. <a href="<?= PROOT; ?>BankRecon/add">Import the first one</a>.
          </td></tr>
        <?php else: ?>
          <?php foreach ($rows as $r):
              $badge = ['OPEN' => 'warning text-dark', 'RECONCILED' => 'success', 'VOID' => 'secondary'][$r['status']] ?? 'light';
              $pct = $r['line_count'] > 0 ? round($r['matched_count'] * 100 / $r['line_count']) : 0;
          ?>
            <tr>
              <td>
                <div class="fw-semibold"><span class="font-monospace"><?= htmlspecialchars($r['account_code']); ?></span></div>
                <div class="small text-muted"><?= htmlspecialchars($r['account_name']); ?></div>
              </td>
              <td class="small"><?= humanDate($r['statement_from']); ?> &rarr; <?= humanDate($r['statement_to']); ?></td>
              <td class="money font-monospace small"><?= number_format((float)$r['opening_balance'], 2); ?></td>
              <td class="money font-monospace small"><?= number_format((float)$r['closing_balance'], 2); ?></td>
              <td class="text-center small"><?= (int)$r['line_count']; ?></td>
              <td class="text-center small">
                <?= (int)$r['matched_count']; ?> / <?= (int)$r['line_count']; ?>
                <?php if ($r['line_count'] > 0): ?>
                  <div class="progress" style="height:4px;"><div class="progress-bar" style="width:<?= $pct; ?>%;"></div></div>
                <?php endif; ?>
              </td>
              <td><span class="badge bg-<?= $badge; ?>"><?= htmlspecialchars($r['status']); ?></span></td>
              <td class="text-end">
                <a href="<?= PROOT; ?>BankRecon/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-primary">
                  <i class="fas fa-arrow-right"></i>
                </a>
              </td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<?= $this->raw('pg_html'); ?>
<?php $this->end(); ?>
