<?php $pageTitle = 'Driver Settlements'; ?>
<div class="panel">
    <?php
        $title = 'Driver Settlements';
        $eyebrow = 'Accounting';
        $searchAction = url('/settlements');
        $createUrl = url('/settlements/create');
        $createText = 'Create Settlement';
        require app_path('Views/components/search_header.php');
    ?>
    <div class="table-wrap">
        <table>
            <thead><tr><th>Settlement</th><th>Driver</th><th>Load</th><th>Gross</th><th>Deductions</th><th>Net</th><th>Status</th><th class="text-right">Actions</th></tr></thead>
            <tbody>
                <?php foreach ($settlements as $settlement): ?>
                    <tr>
                        <td><strong><?= e($settlement['settlement_number']) ?></strong><br><small><?= e(datetime_display($settlement['created_at'] ?? null)) ?></small></td>
                        <td><?= e($settlement['driver_name'] ?? '-') ?></td>
                        <td><?= e($settlement['load_number'] ?? '-') ?></td>
                        <td><?= money($settlement['gross_amount']) ?></td>
                        <td><?= money($settlement['deductions']) ?></td>
                        <td><strong><?= money($settlement['net_amount']) ?></strong></td>
                        <td><span class="badge badge-<?= status_class($settlement['status']) ?>"><?= e(readable_status($settlement['status'])) ?></span></td>
                        <td class="text-right table-actions">
                            <a class="btn btn-light btn-sm" href="<?= url('/settlements/pdf?id=' . $settlement['id']) ?>">PDF</a>
                            <?php if ($settlement['status'] !== 'paid'): ?>
                                <form method="post" action="<?= url('/settlements/paid') ?>"><?= csrf_field() ?><input type="hidden" name="id" value="<?= e((string) $settlement['id']) ?>"><button class="btn btn-light btn-sm" type="submit">Mark Paid</button></form>
                            <?php endif; ?>
                            <form method="post" action="<?= url('/settlements/delete') ?>" onsubmit="return confirm('Delete this settlement?')"><?= csrf_field() ?><input type="hidden" name="id" value="<?= e((string) $settlement['id']) ?>"><button class="btn btn-danger btn-sm" type="submit">Delete</button></form>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$settlements): ?><tr><td colspan="8" class="empty">No settlements found.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
