<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
$pageTitle = 'Permit documents';
$activeNav = 'import_export';

$sql = "SELECT d.id, d.permit_id, d.document_name, d.document_type, d.file_size, d.uploaded_at,
               p.permit_number,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS uploaded_by_name
        FROM permit_documents d
        INNER JOIN import_export_permits p ON d.permit_id = p.id
        INNER JOIN users u ON d.uploaded_by = u.id
        ORDER BY d.uploaded_at DESC
        LIMIT 500";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>Permit documents</h1>
        <p>Supporting files for permits (B/L, invoices, certificates of origin, lab results).</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Quick links</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="import_export_permits.php"><i class="ri-ship-line"></i> Permits</a>
            <a class="qa-btn" href="shipment_verifications.php"><i class="ri-checkbox-circle-line"></i> Verifications</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-attachment-2"></i> Documents</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>Permit</th>
                        <th>Document</th>
                        <th>Type</th>
                        <th>Uploaded by</th>
                        <th>Size</th>
                        <th>Uploaded</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="6"><i class="ri-inbox-line me-2"></i>No documents</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $bytes = (int) ($r['file_size'] ?? 0);
                            $sizeDisp = $bytes > 0 ? number_format($bytes / 1024, 1) . ' KB' : '-';
                        ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['permit_number']) ?></td>
                            <td><small><?= iscms_h($r['document_name']) ?></small></td>
                            <td><small><?= iscms_h(str_replace('_', ' ', (string) ($r['document_type'] ?? ''))) ?></small></td>
                            <td><small><?= iscms_h(trim((string) ($r['uploaded_by_name'] ?? ''))) ?></small></td>
                            <td><small class="text-muted"><?= iscms_h($sizeDisp) ?></small></td>
                            <td><small class="text-muted"><?= !empty($r['uploaded_at']) ? iscms_h(substr((string) $r['uploaded_at'], 0, 19)) : '-' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
