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

$title = getSetting('why_choose_title', 'Why choose us?');
$lead = getSetting('why_choose_lead', '');
$listRaw = getSetting('why_choose_list', '');
$btnText = getSetting('why_choose_btn_text', 'About us');
$btnUrl = getSetting('why_choose_btn_url', 'about.php');
$imageFile = getSetting('why_choose_image', '');

$listItems = array_filter(array_map('trim', preg_split('/\r\n|\r|\n/', $listRaw)));
$imageUrl = '';
if ($imageFile && is_file($homeDir . $imageFile)) {
    $imageUrl = $assetBase . 'uploads/home/' . rawurlencode($imageFile);
}
$btnHref = $btnUrl ? (strpos($btnUrl, 'http') === 0 ? $btnUrl : $assetBase . ltrim($btnUrl, '/')) : $assetBase . 'about.php';
?>
<section class="section-split" aria-labelledby="why-choose-title">
    <div class="split-bg split-bg-left"></div>
    <div class="container split-wrap">
        <div class="split-visual">
            <div class="split-frame">
                <?php if ($imageUrl): ?>
                    <img src="<?php echo htmlspecialchars($imageUrl); ?>" alt="" class="split-frame-img">
                <?php else: ?>
                    <div class="split-gradient-box"></div>
                <?php endif; ?>
            </div>
        </div>
        <div class="split-copy">
            <h2 id="why-choose-title"><?php echo htmlspecialchars($title); ?></h2>
            <?php if ($lead): ?>
            <p><?php echo nl2br(htmlspecialchars($lead)); ?></p>
            <?php endif; ?>
            <?php if (!empty($listItems)): ?>
            <ul class="check-list">
                <?php foreach ($listItems as $item): ?>
                <li><?php echo htmlspecialchars($item); ?></li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
            <?php if ($btnText): ?>
            <a href="<?php echo htmlspecialchars($btnHref); ?>" class="btn-glow btn-accent-modern"><?php echo htmlspecialchars($btnText); ?></a>
            <?php endif; ?>
        </div>
    </div>
</section>
