<?php $pageTitle = 'Invoices'; ?>
<div class="panel">
    <?php
        $title = 'Invoice Management';
        $eyebrow = 'Accounting';
        $searchAction = url('/invoices');
        $createUrl = url('/invoices/create');
        $createText = 'Create Invoice';
        require app_path('Views/components/search_header.php');
    ?>
    <div class="table-wrap">
        <table>
            <thead><tr><th>Invoice</th><th>Load</th><th>Broker</th><th>Due</th><th>Status</th><th>Total</th><th class="text-right">Actions</th></tr></thead>
            <tbody>
                <?php foreach ($invoices as $invoice): ?>
                    <tr>
                        <td><strong><?= e($invoice['invoice_number']) ?></strong><br><small><?= e(datetime_display($invoice['created_at'] ?? null)) ?></small></td>
                        <td><?= e($invoice['load_number'] ?? '-') ?></td>
                        <td><?= e($invoice['broker_name'] ?? '-') ?></td>
                        <td><?= e(date_display($invoice['due_date'] ?? null)) ?></td>
                        <td><span class="badge badge-<?= status_class($invoice['status']) ?>"><?= e(readable_status($invoice['status'])) ?></span></td>
                        <td><?= money($invoice['total']) ?></td>
                        <td class="text-right table-actions">
                            <a class="btn btn-light btn-sm" href="<?= url('/invoices/view?id=' . $invoice['id']) ?>">View</a>
                            <a class="btn btn-light btn-sm" href="<?= url('/invoices/edit?id=' . $invoice['id']) ?>">Edit</a>
                            <a class="btn btn-light btn-sm" href="<?= url('/invoices/pdf?id=' . $invoice['id']) ?>">PDF</a>
                            <form method="post" action="<?= url('/invoices/delete') ?>" onsubmit="return confirm('Delete this invoice?')"><?= csrf_field() ?><input type="hidden" name="id" value="<?= e((string) $invoice['id']) ?>"><button class="btn btn-danger btn-sm" type="submit">Delete</button></form>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$invoices): ?><tr><td colspan="7" class="empty">No invoices found.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
