<?php
/**
 * Dynamic client / stats section — data from DB (admin controlled).
 * Requires config already loaded.
 */
if (!function_exists('getDB')) return;
$pdo = getDB();

$headline = getSetting('client_section_headline', 'A ONE STOP SOLUTION FOR WORLD CLASS QUALITY PRINTED PACKAGING PRODUCTS');
$subtext = getSetting('client_section_subtext', 'We create value with our clients, deliver consistently with the single aim to reach higher by making smart investments in technology for smart innovations that push the envelope.');
$imagePath = getSetting('client_section_image', '');
$assetBase = (defined('BASE_URL') && BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';

$strip = [];
try {
    $stmt = $pdo->prepare("SELECT image, caption FROM section_images WHERE section_key = 'client_section' AND is_active = 1 ORDER BY sort_order, id");
    $stmt->execute();
    $strip = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) { $strip = []; }

$stats = [];
try {
    $stmt = $pdo->query("SELECT value, label FROM stats_counters WHERE is_active = 1 ORDER BY sort_order, id");
    $stats = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    // Table missing — show defaults
    $stats = [
        ['value' => '6+', 'label' => 'Year of Excellence'],
        ['value' => '50+', 'label' => 'Employee'],
        ['value' => '1000+', 'label' => 'Projects Done'],
        ['value' => '30+', 'label' => 'Happy Customers'],
    ];
}
if (empty($stats)) {
    $stats = [
        ['value' => '6+', 'label' => 'Year of Excellence'],
        ['value' => '50+', 'label' => 'Employee'],
    ];
}
$imgUrl = '';
if ($imagePath && is_file(BASE_PATH . '/' . UPLOAD_URL . 'home/' . $imagePath)) {
    $imgUrl = $assetBase . UPLOAD_URL . 'home/' . $imagePath;
} elseif ($imagePath && is_file(BASE_PATH . '/uploads/home/' . $imagePath)) {
    $imgUrl = $assetBase . 'uploads/home/' . $imagePath;
}
?>
<section class="client-section" aria-labelledby="client-section-title">
    <div class="container client-section-inner">
        <?php if (!empty($strip)): ?>
            <div class="page-image-strip" style="max-width: 960px; margin-left:auto; margin-right:auto;">
                <div class="image-scroller<?php echo count($strip) === 1 ? ' image-scroller--center' : ''; ?>" aria-label="Client section images">
                    <?php foreach ($strip as $img):
                        $path = BASE_PATH . '/uploads/home/' . ($img['image'] ?? '');
                        if (empty($img['image']) || !is_file($path)) continue;
                        $src = $assetBase . 'uploads/home/' . rawurlencode($img['image']);
                    ?>
                        <figure class="image-scroller-item">
                            <img src="<?php echo htmlspecialchars($src); ?>" alt="">
                            <?php if (!empty($img['caption'])): ?><figcaption><?php echo htmlspecialchars($img['caption']); ?></figcaption><?php endif; ?>
                        </figure>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>
        <?php if ($imgUrl): ?>
        <div class="client-section-image">
            <img src="<?php echo htmlspecialchars($imgUrl); ?>" alt="">
        </div>
        <?php endif; ?>
        <h2 id="client-section-title" class="client-section-headline"><?php echo nl2br(htmlspecialchars($headline)); ?></h2>
        <?php if ($subtext): ?>
        <p class="client-section-subtext"><?php echo nl2br(htmlspecialchars($subtext)); ?></p>
        <?php endif; ?>
        <?php if (!empty($stats)): ?>
        <div class="client-stats">
            <?php foreach ($stats as $row): ?>
            <div class="client-stat">
                <span class="client-stat-value js-countup"><?php echo htmlspecialchars($row['value']); ?></span>
                <span class="client-stat-label"><?php echo htmlspecialchars($row['label']); ?></span>
            </div>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
</section>
