<?php $this->start('body');
    $stat     = $this->v('status');
    $statuses = ['DRAFT','SUBMITTED','PC_APPROVED','PM_APPROVED','ISSUED','CANCELLED'];
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-file-invoice me-2 text-muted"></i>Customer Invoices</h5>
    <a href="<?= PROOT; ?>Sales/draft" class="btn btn-sm btn-primary" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;">
        <i class="fas fa-plus me-1"></i> Draft Invoice
    </a>
</div>

<div class="mb-2 d-flex flex-wrap gap-1">
    <a href="<?= PROOT; ?>Sales" class="badge <?= $stat===''?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;">All</a>
    <?php foreach ($statuses as $s):
        $active = ($stat === $s);
        $cls    = $active ? 'bg-dark' : 'bg-light text-muted';
    ?>
        <a href="<?= PROOT; ?>Sales?status=<?= $s; ?>" class="badge <?= $cls; ?>" style="text-decoration:none;padding:.35em .6em;text-transform:capitalize;">
            <?= htmlspecialchars(strtolower(str_replace('_',' ',$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>Invoice #</th><th>Date</th><th>Project</th><th>Customer</th><th>Module</th>
                    <th class="text-end">Subtotal</th><th class="text-end">VAT</th><th class="text-end">Total</th>
                    <th>Status</th><th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($this->data['rows'] as $r):
                    $sc = [
                        'DRAFT'       => 'bg-light text-muted',
                        'SUBMITTED'   => 'bg-info text-white',
                        'PC_APPROVED' => 'bg-primary',
                        'PM_APPROVED' => 'bg-primary',
                        'ISSUED'      => 'bg-success',
                        'CANCELLED'   => 'bg-secondary',
                    ][$r['invoice_workflow_status']] ?? 'bg-light text-muted';
                    $displayNo = $r['sale_no'] ?: $r['invoice_ref'];
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>Sales/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($displayNo); ?></strong></a></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['invoice_date']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['project_no'] ?: '—'); ?></td>
                        <td><?= htmlspecialchars($r['customer_name']); ?></td>
                        <td><span class="badge bg-light text-muted" style="text-transform:capitalize;"><?= htmlspecialchars($r['source_module']); ?></span></td>
                        <td class="text-end"><?= number_format((float)$r['subtotal']); ?></td>
                        <td class="text-end text-muted"><?= number_format((float)$r['vat_amount']); ?></td>
                        <td class="text-end" style="font-weight:600;"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['total']); ?></td>
                        <td><span class="badge <?= $sc; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower(str_replace('_',' ',$r['invoice_workflow_status']))); ?></span></td>
                        <td class="text-end"><a href="<?= PROOT; ?>Sales/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($this->data['rows'])): ?>
                    <tr><td colspan="10" class="text-center text-muted py-4">No invoices yet — <a href="<?= PROOT; ?>Sales/draft">draft the first one</a>.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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