<?php
/**
 * Our clients — logo strip on home page. Admin: Clients.
 */
if (!function_exists('getDB')) return;
$pdo = getDB();
$assetBase = (defined('BASE_URL') && BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
$clientsDir = BASE_PATH . '/uploads/clients/';

$title = getSetting('our_clients_title', 'Our clients');
$subtext = getSetting('our_clients_subtext', 'Trusted by brands across industries');

$rows = [];
try {
    $stmt = $pdo->query("SELECT id, name, logo, website_url FROM our_clients WHERE is_active = 1 ORDER BY sort_order, id");
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) { /* table missing */ }

$clients = [];
foreach ($rows as $c) {
    if (empty($c['logo']) || !is_file($clientsDir . $c['logo'])) continue;
    $clients[] = $c;
}
if (empty($clients)) return;
$count = count($clients);
$trackClass = 'our-clients-track';
if ($count === 1) $trackClass .= ' our-clients-track--single';
elseif ($count > 4) $trackClass .= ' our-clients-track--scroll';
else $trackClass .= ' our-clients-track--grid';
?>
<section class="our-clients-section" aria-labelledby="our-clients-title">
    <div class="container">
        <h2 id="our-clients-title" class="our-clients-title"><?php echo htmlspecialchars($title); ?></h2>
        <?php if ($subtext): ?>
        <p class="our-clients-sub"><?php echo htmlspecialchars($subtext); ?></p>
        <?php endif; ?>
        <div class="our-clients-track-wrap">
        <div class="<?php echo htmlspecialchars($trackClass); ?>">
            <?php foreach ($clients as $c):
                $logoUrl = $assetBase . 'uploads/clients/' . rawurlencode($c['logo']);
                $hasUrl = !empty($c['website_url']);
            ?>
            <?php if ($hasUrl): ?>
            <a href="<?php echo htmlspecialchars($c['website_url']); ?>" class="our-client-card our-client-card--link" target="_blank" rel="noopener">
            <?php else: ?>
            <div class="our-client-card">
            <?php endif; ?>
                <span class="our-client-logo-wrap">
                    <img src="<?php echo htmlspecialchars($logoUrl); ?>" alt="<?php echo htmlspecialchars($c['name']); ?>" loading="lazy">
                </span>
                <span class="our-client-name"><?php echo htmlspecialchars($c['name']); ?></span>
            <?php echo $hasUrl ? '</a>' : '</div>'; ?>
            <?php endforeach; ?>
        </div>
        </div>
    </div>
</section>
