<?php $this->start('body');
    $rows   = $this->data['rows']   ?? [];
    $status = (string)($this->data['status'] ?? '');
    $statuses = ['DRAFT','ISSUED','CANCELLED'];
    $chip = [
        'DRAFT'     => ['#F1F5F9','#334155','Draft'],
        'ISSUED'    => ['#EAF2E8','#3F5C43','Issued'],
        'CANCELLED' => ['#F5EAE1','#C48579','Cancelled'],
    ];
?>
<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-undo-alt me-2 text-muted"></i>Credit Notes
            <span class="text-muted ms-2" style="font-weight:400;font-size:.7rem;"><?= number_format(count($rows)); ?> total</span>
        </h5>
    </div>
</div>

<div class="mb-2 d-flex flex-wrap gap-1">
    <a href="<?= PROOT; ?>CreditNotes" class="badge <?= $status===''?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;">All</a>
    <?php foreach ($statuses as $s): ?>
        <a href="<?= PROOT; ?>CreditNotes?status=<?= $s; ?>" class="badge <?= $status===$s?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;text-transform:capitalize;">
            <?= htmlspecialchars(strtolower($s)); ?>
        </a>
    <?php endforeach; ?>
</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>CN #</th>
                    <th>Against Invoice</th>
                    <th>Customer</th>
                    <th>Project</th>
                    <th>Reason</th>
                    <th class="text-end">Total</th>
                    <th>Drafted</th>
                    <th>Status</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $c = $chip[$r['status']] ?? ['#F1F5F9','#334155',$r['status']];
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>CreditNotes/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['cn_no']); ?></strong></a></td>
                        <td class="small"><a href="<?= PROOT; ?>Sales/view/<?= (int)$r['sale_id']; ?>"><?= htmlspecialchars((string)$r['sale_no']); ?></a></td>
                        <td class="small"><?= htmlspecialchars((string)$r['customer_name']); ?></td>
                        <td class="small text-muted"><?= htmlspecialchars((string)($r['project_no'] ?? '—')); ?></td>
                        <td class="small text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower(str_replace('_',' ', $r['reason']))); ?></td>
                        <td class="text-end small" style="font-weight:600;color:#334155;"><?= htmlspecialchars((string)$r['currency']); ?> <?= number_format((float)$r['total']); ?></td>
                        <td class="small text-muted"><?= htmlspecialchars((string)$r['drafted_on']); ?><div style="font-size:.6rem;"><?= htmlspecialchars((string)$r['drafted_by_name']); ?></div></td>
                        <td><span class="badge" style="background:<?= $c[0]; ?>;color:<?= $c[1]; ?>;border:1px solid <?= $c[1]; ?>;font-size:.62rem;text-transform:capitalize;"><?= htmlspecialchars($c[2]); ?></span></td>
                        <td class="text-end"><a href="<?= PROOT; ?>CreditNotes/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="9" class="text-center text-muted py-4">No credit notes yet.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>
