<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Certification renewals';
$activeNav = 'certifications';

$sql = "SELECT r.id, r.status, r.renewal_request_date, r.approval_date,
               LEFT(r.remarks, 100) AS remarks_snip,
               co.certificate_number AS original_number,
               cn.certificate_number AS new_number,
               a.application_number,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS requested_by_name
        FROM certification_renewals r
        INNER JOIN certifications co ON r.original_cert_id = co.id
        LEFT JOIN certifications cn ON r.new_cert_id = cn.id
        LEFT JOIN applications a ON r.renewal_application_id = a.id
        INNER JOIN users u ON r.requested_by = u.id
        ORDER BY r.created_at DESC
        LIMIT 300";
$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>Certification renewals</h1>
        <p>Renewal requests linked to original and replacement certificates.</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="applications.php"><i class="ri-file-list-3-line"></i> Applications</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-refresh-line"></i> Renewals</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>Original certificate</th>
                        <th>New certificate</th>
                        <th>Application</th>
                        <th>Requested by</th>
                        <th>Status</th>
                        <th>Request date</th>
                        <th>Approval</th>
                        <th>Remarks (excerpt)</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="8"><i class="ri-inbox-line me-2"></i>No renewal records</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['original_number']) ?></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['new_number'] ?? null) ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['application_number'] ?? null) ?></small></td>
                            <td><small><?= iscms_h(trim((string) ($r['requested_by_name'] ?? ''))) ?></small></td>
                            <td><?= iscms_status_badge((string) ($r['status'] ?? '')) ?></td>
                            <td><small class="text-muted"><?= iscms_val($r['renewal_request_date'] ?? null) ?></small></td>
                            <td><small class="text-muted"><?= iscms_val($r['approval_date'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($r['remarks_snip'] ?? null) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
