<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Certificate documents';
$activeNav = 'certifications';

$sql = "SELECT d.id, d.cert_id, d.document_name, d.document_type, d.is_public, d.uploaded_at,
               c.certificate_number,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS uploaded_by_name
        FROM certificate_documents d
        INNER JOIN certifications c ON d.cert_id = c.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>Certificate documents</h1>
        <p>Files attached to certificates (PDFs, lab reports, correspondence).</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="certifications.php"><i class="ri-shield-star-line"></i> Certifications</a>
            <a class="qa-btn" href="certification_history.php"><i class="ri-git-commit-line"></i> History</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>Certificate</th>
                        <th>Document</th>
                        <th>Type</th>
                        <th>Uploaded by</th>
                        <th>Public</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): ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['certificate_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><?= !empty($r['is_public']) ? '<span class="badge bg-info-subtle text-primary" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></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();
