<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Certifications';
$activeNav = 'certifications';

$sql = "SELECT c.id, c.certificate_number, c.status, c.standard_applied, c.issue_date, c.expiry_date,
               c.is_renewable,
               o.org_name,
               p.product_name,
               ct.type_name,
               a.application_number,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS issued_by_name
        FROM certifications c
        INNER JOIN organizations o ON c.org_id = o.id
        LEFT JOIN products p ON c.product_id = p.id
        LEFT JOIN certificate_types ct ON c.cert_type_id = ct.id
        INNER JOIN applications a ON c.application_id = a.id
        INNER JOIN users u ON c.issued_by = u.id
        ORDER BY c.updated_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>Certifications</h1>
        <p>Certificates issued from approved applications, with status and validity.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Related registers</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="certification_history.php"><i class="ri-git-commit-line"></i> Certification history</a>
            <a class="qa-btn" href="certification_renewals.php"><i class="ri-refresh-line"></i> Renewals</a>
            <a class="qa-btn" href="certificate_documents.php"><i class="ri-attachment-2"></i> Certificate documents</a>
            <a class="qa-btn" href="certificate_types.php"><i class="ri-award-line"></i> Certificate types</a>
            <a class="qa-btn" href="import_export_permits.php"><i class="ri-ship-line"></i> Import / export permits</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-shield-star-line"></i> Register</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>Application</th>
                        <th>Organisation</th>
                        <th>Product</th>
                        <th>Type</th>
                        <th>Standard</th>
                        <th>Status</th>
                        <th>Issued by</th>
                        <th>Issue</th>
                        <th>Expiry</th>
                        <th>Renewable</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="11"><i class="ri-inbox-line me-2"></i>No certifications</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 class="iscms-mono"><small><?= iscms_h($r['application_number']) ?></small></td>
                            <td><small><?= iscms_h($r['org_name']) ?></small></td>
                            <td><small><?= iscms_val($r['product_name'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($r['type_name'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($r['standard_applied'] ?? null) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small><?= iscms_h(trim((string) ($r['issued_by_name'] ?? ''))) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['issue_date'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['expiry_date'] ?? null) ?></small></td>
                            <td><?= !empty($r['is_renewable']) ? '<span class="badge bg-success-subtle text-success" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
