<?php

require_once __DIR__ . '/_cms_helpers.php';

$lang = $lang ?? 'en';

$conferenceId =
    (int)(
        $conference['id']
        ?? $_GET['id']
        ?? 0
    );

$blocks = [];

try {

    $db = \App\Core\Database::getInstance();

    $stmt = $db->prepare("

        SELECT *
        FROM conference_homepage_blocks

        WHERE conference_id = ?
        AND is_active = 1

        ORDER BY sort_order ASC, id ASC

    ");

    $stmt->execute([$conferenceId]);

    $blocks =
        $stmt->fetchAll(\PDO::FETCH_ASSOC);

} catch (\Throwable $e) {

    error_log(
        'HOME BLOCKS ERROR: ' .
        $e->getMessage()
    );
}

?>

<section
class="
ec-home-blocks
ec-reveal
ec-hover-lift
ec-glow-hover
"
>

<div class="container">

    <div class="ec-block-grid">

        <?php if (!$blocks): ?>

            <div class="alert alert-light border rounded-4">

                <?= pcb_h(
                    $lang === 'ar'
                    ? 'لا توجد كتل محتوى منشورة حالياً.'
                    : 'No published homepage blocks.'
                ) ?>

            </div>

        <?php else: ?>

            <?php foreach ($blocks as $block): ?>

                <?php

                $title =
                    $lang === 'ar'
                    ? (
                        $block['title_ar']
                        ?: $block['title_en']
                    )
                    : (
                        $block['title_en']
                        ?: $block['title_ar']
                    );

                $body =
                    $lang === 'ar'
                    ? (
                        $block['body_ar']
                        ?: $block['body_en']
                    )
                    : (
                        $block['body_en']
                        ?: $block['body_ar']
                    );

                $buttonText =
                    $lang === 'ar'
                    ? (
                        $block['button_text_ar']
                        ?: $block['button_text_en']
                    )
                    : (
                        $block['button_text_en']
                        ?: $block['button_text_ar']
                    );

                $buttonUrl =
                    $block['button_url']
                    ?: '#';

                $type =
                    $block['block_type']
                    ?: 'panel';

                ?>

                <div
                class="
                ec-home-card
                ec-home-card-<?= pcb_h($type) ?>
                "
                >

                    <div class="ec-home-icon">

                        <?php

                        $icons = [

                            'hero_slide' => 'fa-star',
                            'feature' => 'fa-lightbulb',
                            'panel' => 'fa-layer-group',
                            'analytics' => 'fa-chart-line',
                            'important_date' => 'fa-calendar',
                            'participation' => 'fa-users',
                            'proceedings' => 'fa-book-open',
                            'virtual' => 'fa-video',
                            'travel' => 'fa-location-dot',
                            'networking' => 'fa-globe',
                            'cta' => 'fa-envelope'

                        ];

                        ?>

                        <i
                        class="
                        fa
                        <?= pcb_h(
                            $icons[$type]
                            ?? 'fa-circle-info'
                        ) ?>
                        "
                        ></i>

                    </div>

                    <div class="ec-home-title">

                        <?= pcb_h($title) ?>

                    </div>

                    <div class="ec-home-desc">

                        <?= nl2br(
                            pcb_h($body)
                        ) ?>

                    </div>

                    <div class="ec-home-chip">

                        <?= pcb_h(
                            strtoupper($type)
                        ) ?>

                    </div>

                    <?php if ($buttonText): ?>

                        <a
                            href="<?= pcb_h($buttonUrl) ?>"
                            class="ec-home-link"
                        >

                            <i class="fa fa-arrow-right"></i>

                            <?= pcb_h($buttonText) ?>

                        </a>

                    <?php endif; ?>

                </div>

            <?php endforeach; ?>

        <?php endif; ?>

    </div>

</div>

</section>
