<?php $pageTitle = 'Documents'; ?>
<div class="panel">
    <?php
        $title = 'Document Center';
        $eyebrow = 'Files';
        $searchAction = url('/documents');
        $createUrl = in_array(current_role(), ['driver','carrier'], true) ? null : url('/documents/create');
        $createText = 'Upload Document';
        $secondaryUrl = can('ai.use') ? url('/ai#document-reader') : null;
        $secondaryText = 'AI Document Reader';
        require app_path('Views/components/search_header.php');
    ?>
    <div class="table-wrap">
        <table>
            <thead><tr><th>Document</th><th>Related To</th><th>Type</th><th>Expires</th><th>Uploaded</th><th class="text-right">Actions</th></tr></thead>
            <tbody>
                <?php foreach ($documents as $document): ?>
                    <tr>
                        <td><strong><?= e($document['file_name']) ?></strong><br><small><?= e(number_format(((int) ($document['file_size'] ?? 0)) / 1024, 1)) ?> KB</small></td>
                        <td><?= e(relation_label($document['related_type'])) ?> #<?= e((string) $document['related_id']) ?></td>
                        <td><?= e($document['document_type']) ?><?php if (!empty($document['location_address'])): ?><br><small><?= e($document['location_address']) ?></small><?php endif; ?></td>
                        <td><?= e(date_display($document['expires_at'] ?? null)) ?></td>
                        <td><?= e(datetime_display($document['created_at'] ?? null)) ?><br><small><?= e($document['uploaded_by_name'] ?? '-') ?></small></td>
                        <td class="text-right table-actions">
                            <a class="btn btn-light btn-sm" href="<?= url('/documents/download?id=' . $document['id']) ?>">Download</a>
                            <?php if (!in_array(current_role(), ['driver','carrier'], true)): ?><form method="post" action="<?= url('/documents/delete') ?>" onsubmit="return confirm('Delete this document?')"><?= csrf_field() ?><input type="hidden" name="id" value="<?= e((string) $document['id']) ?>"><button class="btn btn-danger btn-sm" type="submit">Delete</button></form><?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (!$documents): ?><tr><td colspan="6" class="empty">No documents uploaded yet.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
