<?php
require_once '../src/config/db.php';
require_once '../src/includes/functions.php';
include '../src/includes/header.php';

// Fetch all certifications from the database, ordered by sort_order
try {
    $stmt = $pdo->query("SELECT * FROM certifications ORDER BY sort_order ASC, id DESC");
    $certifications = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    // Log error and set certifications to an empty array
    error_log($e->getMessage());
    $certifications = [];
}
?>

<section class="page-header py-5 py-lg-6">
    <div class="container text-center">
        <h1 class="display-4 fw-bolder text-white"><?php echo $t['nav_certifications']; ?></h1>
        <p class="lead text-white-50 mx-auto" style="max-width: 800px;"><?php echo $t['cert_subtitle']; ?></p>
        <div class="page-header-stat mt-4">
            <?php echo $t['total_certs_label']; ?>: <span class="count"><?php echo count($certifications); ?></span>
        </div>
    </div>
</section>

<section class="py-5 py-lg-6">
    <div class="container">
        <hr class="section-divider">
        <?php if (empty($certifications)): ?>
            <div class="text-center">
                <p><?php echo $t['cert_no_certs']; ?></p>
            </div>
        <?php else: ?>
            <div class="row g-4 justify-content-center">
                <?php
                $certKeyMap = [
                    'Certificate of Destruction (CoD)' => ['title' => 'cert_cod_title', 'desc' => 'cert_cod_desc'],
                    'ISO 27001 Compliance' => ['title' => 'cert_iso_title', 'desc' => 'cert_iso_desc'],
                    'HIPAA Compliance' => ['title' => 'cert_hipaa_title', 'desc' => 'cert_hipaa_desc'],
                    'NIST SP 800-88 Standards' => ['title' => 'cert_nist_title', 'desc' => 'cert_nist_desc'],
                ];
                foreach ($certifications as $cert):
                    $certKey = $certKeyMap[$cert['title']] ?? null;
                ?>
                    <div class="col-md-6 col-lg-4 d-flex">
                        <a href="<?php echo htmlspecialchars($cert['image_url']); ?>" 
                           data-lightbox="certifications" 
                           data-title="<?php echo htmlspecialchars($cert['title']); ?>" 
                           class="certification-card w-100 animated-card" 
                           style="background-image: url('<?php echo htmlspecialchars($cert['image_url']); ?>');">
                            
                            <div class="certification-card-content">
                                <h3 class="certification-title"><?php echo htmlspecialchars($certKey && isset($t[$certKey['title']]) ? $t[$certKey['title']] : $cert['title']); ?></h3>
                                <p class="certification-description"><?php echo htmlspecialchars(mb_strimwidth($certKey && isset($t[$certKey['desc']]) ? $t[$certKey['desc']] : $cert['description'], 0, 100, '...')); ?></p>
                            </div>
                        </a>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>

<?php include '../src/includes/footer.php'; ?>