<?php $this->start('body');
    $stmts = $this->data['statements'] ?? [];
    $statusChip = [
        'DRAFT'       => ['bg' => '#e8e0d5', 'ink' => '#7a6f63', 'label' => 'Draft'],
        'IN_PROGRESS' => ['bg' => '#eae0d0', 'ink' => '#7a6039', 'label' => 'In progress'],
        'RECONCILED'  => ['bg' => '#d8e5d1', 'ink' => '#4a6b3a', 'label' => 'Reconciled'],
    ];
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;color:#4a3f36;"><i class="fas fa-university me-2" style="color:#c9a875;"></i>Bank Reconciliation</h5>
    <a href="<?= PROOT; ?>BankRec/import" class="btn btn-sm" style="background:#c9a875;border-color:#c9a875;color:#4a3f36;font-weight:600;">
        <i class="fas fa-file-import me-1"></i>Import statement
    </a>
</div>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white" style="border-bottom:1px solid #eae0d0;">
        <strong style="color:#4a3f36;"><i class="fas fa-list me-2" style="color:#c9a875;"></i>All statements</strong>
    </div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead style="background:#f5efe6;">
                <tr>
                    <th style="color:#4a3f36;">Statement date</th>
                    <th style="color:#4a3f36;">Bank</th>
                    <th class="text-end" style="color:#4a3f36;">Opening</th>
                    <th class="text-end" style="color:#4a3f36;">Closing</th>
                    <th class="text-end" style="color:#4a3f36;">Lines</th>
                    <th style="color:#4a3f36;">Progress</th>
                    <th style="color:#4a3f36;">Status</th>
                    <th class="text-end" style="color:#4a3f36;">Imported</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($stmts as $s):
                    $chip = $statusChip[$s['status']] ?? ['bg'=>'#e8e0d5','ink'=>'#7a6f63','label'=>$s['status']];
                    $done = (int)$s['lines_done']; $tot = (int)$s['lines_total'];
                    $pct = $tot > 0 ? (int)round(100 * $done / $tot) : 0;
                ?>
                    <tr>
                        <td style="color:#4a3f36;font-weight:500;"><?= htmlspecialchars($s['statement_date']); ?></td>
                        <td style="color:#4a3f36;">
                            <span class="text-muted small" style="color:#7a6f63;"><?= htmlspecialchars($s['account_code']); ?></span>
                            <?= htmlspecialchars($s['bank_name']); ?>
                        </td>
                        <td class="text-end" style="color:#4a3f36;"><?= number_format((float)$s['opening_balance']); ?></td>
                        <td class="text-end" style="font-weight:600;color:#4a3f36;"><?= number_format((float)$s['closing_balance']); ?></td>
                        <td class="text-end text-muted small" style="color:#7a6f63;"><?= $done; ?> / <?= $tot; ?></td>
                        <td>
                            <div style="height:6px;background:#f5efe6;border-radius:3px;overflow:hidden;min-width:80px;">
                                <div style="width:<?= $pct; ?>%;height:6px;background:<?= $pct === 100 ? '#7a9e6e' : '#c9a875'; ?>;"></div>
                            </div>
                        </td>
                        <td><span class="badge" style="background:<?= $chip['bg']; ?>;color:<?= $chip['ink']; ?>;font-size:.62rem;text-transform:capitalize;"><?= $chip['label']; ?></span></td>
                        <td class="text-end text-muted small" style="color:#7a6f63;"><?= htmlspecialchars(date('d M', strtotime($s['imported_on']))); ?><br><span style="font-size:.65rem;"><?= htmlspecialchars($s['imported_by_name']); ?></span></td>
                        <td class="text-end">
                            <a href="<?= PROOT; ?>BankRec/view/<?= (int)$s['c0d']; ?>" class="btn btn-sm" style="background:#faf6f0;border:1px solid #eae0d0;color:#4a3f36;">
                                <i class="fas fa-search-plus me-1" style="color:#c9a875;"></i>Reconcile
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($stmts)): ?>
                    <tr><td colspan="9" class="text-center py-4" style="color:#a89a8a;">
                        No statements yet. <a href="<?= PROOT; ?>BankRec/import" style="color:#c9a875;font-weight:600;">Import your first statement</a>.
                    </td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $this->end(); ?>
