<?php
/**
 * Why businesses choose us — dynamic feature cards. Admin: Home blocks.
 */
if (!function_exists('getDB')) return;
$pdo = getDB();
$assetBase = (defined('BASE_URL') && BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
$homeDir = BASE_PATH . '/uploads/home/';

$sectionTitle = getSetting('features_section_title', 'Why businesses choose us');
$sectionSub = getSetting('features_section_sub', 'Quality corrugated solutions from one reliable partner.');

$features = [];
try {
    $features = $pdo->query("SELECT id, title, description, icon_type, icon_value, color_class FROM home_features WHERE is_active = 1 ORDER BY sort_order, id")->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {}

if (empty($features)) return;
?>
<section class="section-features" aria-labelledby="features-section-title">
    <div class="container">
        <h2 id="features-section-title" class="section-title"><?php echo htmlspecialchars($sectionTitle); ?></h2>
        <?php if ($sectionSub): ?>
        <p class="section-sub"><?php echo htmlspecialchars($sectionSub); ?></p>
        <?php endif; ?>
        <div class="feature-grid">
            <?php
            $colors = ['teal', 'amber', 'violet'];
            foreach ($features as $i => $f):
                $colorClass = !empty($f['color_class']) ? $f['color_class'] : ($colors[$i % count($colors)]);
                $cardClass = 'feature-card feature-card--' . $colorClass;
            ?>
            <article class="<?php echo htmlspecialchars($cardClass); ?>">
                <div class="feature-icon">
                    <?php if (($f['icon_type'] ?? '') === 'image' && !empty($f['icon_value']) && is_file($homeDir . $f['icon_value'])): ?>
                        <img src="<?php echo htmlspecialchars($assetBase . 'uploads/home/' . rawurlencode($f['icon_value'])); ?>" alt="">
                    <?php else: ?>
                        <?php echo htmlspecialchars($f['icon_value'] ?? ''); ?>
                    <?php endif; ?>
                </div>
                <h3><?php echo htmlspecialchars($f['title']); ?></h3>
                <p><?php echo nl2br(htmlspecialchars($f['description'])); ?></p>
            </article>
            <?php endforeach; ?>
        </div>
    </div>
</section>
