<?php $this->start('body');
    $banks    = $this->data['banks']       ?? [];
    $selectedId = (int)($this->data['selected_id'] ?? 0);
    $sel      = $this->data['sel_bank']    ?? [];
    $lines    = $this->data['lines']       ?? [];
    $closing  = (float)($this->data['closing'] ?? 0);
    $from     = $this->v('from');
    $to       = $this->v('to');
    $curr     = $sel['currency'] ?? 'TZS';
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-book me-2 text-muted"></i>Cashbook</h5>
</div>

<form method="get" action="<?= PROOT; ?>Cashbook" class="card border-0 shadow-sm mb-3">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-5">
                <label class="form-label">Bank account</label>
                <select name="bank_id" class="form-select form-select-sm" onchange="this.form.submit()">
                    <?php foreach ($banks as $b): ?>
                        <option value="<?= (int)$b['c0d']; ?>" <?= $selectedId===(int)$b['c0d']?'selected':''; ?>>
                            <?= htmlspecialchars($b['bank_name']); ?> · <?= htmlspecialchars($b['account_name']); ?> (<?= htmlspecialchars($b['currency']); ?>)
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label">From</label>
                <input type="date" name="from" value="<?= htmlspecialchars($from); ?>" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label">To</label>
                <input type="date" name="to" value="<?= htmlspecialchars($to); ?>" class="form-control form-control-sm">
            </div>
            <div class="col-md-1 text-end">
                <button type="submit" class="btn btn-sm btn-outline-secondary" title="Filter"><i class="fas fa-filter"></i></button>
            </div>
        </div>
    </div>
</form>

<?php if (!empty($sel)): ?>
<div class="row g-3 mb-3">
    <div class="col-md-4"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Bank</div><div style="font-weight:600;"><?= htmlspecialchars($sel['bank_name']); ?></div><div class="text-muted small"><?= htmlspecialchars($sel['account_number']); ?></div></div></div></div>
    <div class="col-md-4"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Opening balance</div><div style="font-weight:600;font-size:1.05rem;"><?= htmlspecialchars($curr); ?> <?= number_format((float)$sel['opening_balance']); ?></div><div class="text-muted small">as of <?= htmlspecialchars($sel['opening_on'] ?: '—'); ?></div></div></div></div>
    <div class="col-md-4"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Closing balance</div><div style="font-weight:600;font-size:1.05rem;color:<?= $closing<0?'#ef4444':'var(--rs-text,#1e2d3d)'; ?>;"><?= htmlspecialchars($curr); ?> <?= number_format($closing); ?></div><div class="text-muted small">after <?= count($lines); ?> line(s)</div></div></div></div>
</div>
<?php endif; ?>

<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>Date</th><th>JE #</th><th>Source</th><th>Ref</th><th>Narrative</th>
                    <th class="text-end">Debit (in)</th><th class="text-end">Credit (out)</th><th class="text-end">Running</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($lines as $ln): ?>
                    <tr>
                        <td class="text-muted small"><?= htmlspecialchars($ln['entry_date']); ?></td>
                        <td><a href="<?= PROOT; ?>Journals/view/<?= (int)$ln['je_id']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($ln['je_no']); ?></strong></a></td>
                        <td><span class="badge bg-light text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($ln['source_type'])); ?></span></td>
                        <td class="text-muted small"><?= htmlspecialchars($ln['source_ref']); ?></td>
                        <td class="text-muted"><?= htmlspecialchars($ln['line_narr'] ?: $ln['je_narr']); ?></td>
                        <td class="text-end"><?= (float)$ln['debit']  > 0 ? number_format((float)$ln['debit'])  : '—'; ?></td>
                        <td class="text-end"><?= (float)$ln['credit'] > 0 ? number_format((float)$ln['credit']) : '—'; ?></td>
                        <td class="text-end" style="font-weight:600;color:<?= (float)$ln['running_balance']<0?'#ef4444':''; ?>;"><?= number_format((float)$ln['running_balance']); ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($lines)): ?>
                    <tr><td colspan="8" class="text-center text-muted py-4">No cashbook activity for this bank in this period.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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