<?php $pageTitle = 'Maintenance'; ?>
<div class="panel">
    <?php
        $title = 'Maintenance Tracking';
        $eyebrow = 'Fleet';
        $searchAction = url('/maintenance');
        $createUrl = url('/maintenance/create');
        $createText = 'Add Service';
        require app_path('Views/components/search_header.php');
    ?>
    <div class="table-wrap">
        <table>
            <thead><tr><th>Equipment</th><th>Service</th><th>Date</th><th>Next Due</th><th>Mileage</th><th>Cost</th><th>Status</th><th class="text-right">Actions</th></tr></thead>
            <tbody>
                <?php foreach ($records as $record): ?>
                    <tr>
                        <td><strong><?= e($record['truck_number'] ?: $record['trailer_number'] ?: '-') ?></strong></td>
                        <td><?= e($record['service_type']) ?><br><small><?= e($record['notes'] ?? '') ?></small></td>
                        <td><?= e(date_display($record['service_date'])) ?></td>
                        <td><?= e(date_display($record['next_service_date'] ?? null)) ?></td>
                        <td><?= e((string) ($record['mileage'] ?? '-')) ?></td>
                        <td><?= money($record['cost']) ?></td>
                        <td><span class="badge badge-<?= status_class($record['status']) ?>"><?= e(readable_status($record['status'])) ?></span></td>
                        <td class="text-right table-actions">
                            <form method="post" action="<?= url('/maintenance/delete') ?>" onsubmit="return confirm('Delete this maintenance record?')"><?= csrf_field() ?><input type="hidden" name="id" value="<?= e((string) $record['id']) ?>"><button class="btn btn-danger btn-sm" type="submit">Delete</button></form>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$records): ?><tr><td colspan="8" class="empty">No maintenance records found.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
