<?php $this->start('body');
    $rows       = $this->data['rows'] ?? [];
    $box        = $this->data['box']  ?? null;
    $filterType = (string)($this->data['filter_type'] ?? '');
    $currency   = $box['currency'] ?? 'TZS';
    $movementChip = [
        'DISBURSE'   => ['#F5EAE1','#C48579','Disbursed'],
        'REPLENISH'  => ['#EAF2E8','#3F5C43','Replenished'],
        'RETURN'     => ['#EEF2F6','#7A8BA1','Returned'],
        'ADJUSTMENT' => ['#EDE7DC','#5B4F3C','Adjusted'],
    ];
    $filters = ['' => 'All movements','DISBURSE' => 'Disbursements','REPLENISH' => 'Replenishments','RETURN' => 'Returns','ADJUSTMENT' => 'Adjustments'];
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-history me-2 text-muted"></i>Petty cash movements</h5>
        <div class="text-muted small mt-1" style="font-size:.72rem;">
            <?= htmlspecialchars($box['name'] ?? 'Petty Cash Box'); ?> · Current balance <?= $currency; ?> <?= number_format((float)($box['current_balance'] ?? 0)); ?>
        </div>
    </div>
    <div>
        <a href="<?= PROOT; ?>PettyCashBox" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back to box</a>
    </div>
</div>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-body p-2" style="background:#F8FAFC;">
        <?php foreach ($filters as $k => $lbl):
            $active = ($k === $filterType);
        ?>
            <a href="<?= PROOT; ?>PettyCashBox/movements<?= $k ? '?type=' . $k : ''; ?>" class="btn btn-sm me-1 mb-1"
               style="background:<?= $active ? '#334155' : '#fff'; ?>;color:<?= $active ? '#fff' : '#334155'; ?>;border:1px solid #E2E8F0;font-weight:<?= $active ? 600 : 400; ?>;font-size:.68rem;">
                <?= htmlspecialchars($lbl); ?>
            </a>
        <?php endforeach; ?>
    </div>
</div>

<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 style="width:130px;">When</th>
                    <th style="width:120px;">Type</th>
                    <th class="text-end" style="width:140px;">Amount</th>
                    <th class="text-end" style="width:140px;">Balance after</th>
                    <th>Narrative</th>
                    <th style="width:110px;">LPO</th>
                    <th style="width:160px;">By</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $m):
                    $mc = $movementChip[$m['movement_type']] ?? ['#F1F5F9','#334155',$m['movement_type']];
                    $sign = ($m['movement_type'] === 'DISBURSE' || ((float)$m['amount'] < 0)) ? '−' : '+';
                    $absAmt = abs((float)$m['amount']);
                ?>
                    <tr>
                        <td class="small text-muted"><?= htmlspecialchars(date('d M · H:i', strtotime($m['performed_on']))); ?></td>
                        <td><span class="badge" style="background:<?= $mc[0]; ?>;color:<?= $mc[1]; ?>;border:1px solid <?= $mc[1]; ?>;font-size:.62rem;text-transform:capitalize;"><?= htmlspecialchars($mc[2]); ?></span></td>
                        <td class="text-end small" style="font-weight:600;color:<?= $sign === '−' ? '#C48579' : '#3F5C43'; ?>;"><?= $sign; ?> <?= $currency; ?> <?= number_format($absAmt); ?></td>
                        <td class="text-end small text-muted"><?= $currency; ?> <?= number_format((float)$m['balance_after']); ?></td>
                        <td class="small text-muted"><?= htmlspecialchars((string)$m['narrative']); ?></td>
                        <td class="small">
                            <?php if (!empty($m['lpo_no'])): ?>
                                <a href="<?= PROOT; ?>Lpos/view/<?= (int)$m['related_lpo_id']; ?>" class="text-decoration-none" style="color:#7A8BA1;font-weight:600;"><?= htmlspecialchars($m['lpo_no']); ?></a>
                            <?php else: ?>
                                <span class="text-muted">—</span>
                            <?php endif; ?>
                        </td>
                        <td class="small text-muted"><?= htmlspecialchars((string)$m['performed_by_name']); ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="7" class="text-center text-muted py-4" style="font-size:.72rem;">No movements match this filter.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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