<?php

require_once __DIR__ . '/_cms_helpers.php';

$sectionConfig =
    ecms_get_section_config('media');

if (empty($sectionConfig['enabled'])) {
    return;
}

$mediaItems =
    ec_safe_array($mediaItems ?? []);

$lang =
    $lang ?? 'en';

?>

<section
    data-cms-section="media"
    class="ecms-managed-section ec-gallery-wrap ec-section-shell ec-section-anchor"
    id="gallery">

    <div class="proceedings-header">

        <div>

            <div class="ec-ieee-badge mb-4">

                <i class="fa fa-images"></i>

                <?= $lang === 'ar'
                    ? 'معرض المؤتمر'
                    : 'Conference Gallery' ?>

            </div>

            <h2 class="section-title">

                <?= $lang === 'ar'
                    ? 'الصور والفعاليات الأكاديمية'
                    : 'Conference Media & Academic Moments' ?>

            </h2>

        </div>

        <div class="proceeding-meta">

            <span>

                <i class="fa fa-camera"></i>

                <?= count($mediaItems) ?>

                <?= $lang === 'ar'
                    ? 'وسائط'
                    : 'Media Items' ?>

            </span>

            <span>

                <i class="fa fa-photo-film"></i>

                <?= $lang === 'ar'
                    ? 'تغطية احترافية'
                    : 'Professional Coverage' ?>

            </span>

        </div>

    </div>

    <?php if (empty($mediaItems)): ?>

        <div class="alert alert-light border rounded-4 shadow-sm p-5 text-center">

            <i class="fa fa-images fa-3x text-primary mb-4"></i>

            <h4 class="fw-bold">

                <?= $lang === 'ar'
                    ? 'سيتم إضافة الصور قريباً'
                    : 'Gallery will be available soon' ?>

            </h4>

        </div>

    <?php else: ?>

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

            <?php foreach ($mediaItems as $index => $media): ?>

                <?php
                $title =
                    $lang === 'ar'
                    ? ($media['title_ar'] ?? $media['title_en'] ?? '')
                    : ($media['title_en'] ?? $media['title_ar'] ?? '');

                $desc =
                    $lang === 'ar'
                    ? ($media['description_ar'] ?? $media['description_en'] ?? '')
                    : ($media['description_en'] ?? $media['description_ar'] ?? '');

                $image =
                    $media['image']
                    ?? $media['photo']
                    ?? '';

                $featured =
                    !empty($media['featured']);
                ?>

                <div class="ec-gallery-card <?= $featured ? 'featured' : '' ?>">

                    <?php if (!empty($image)): ?>

                        <img
                            src="<?= pcb_h(
ec_media_url($image)
) ?>"
                            class="ec-gallery-image"
                            alt="<?= pcb_h($title) ?>">

                    <?php else: ?>

                        <div class="ec-gallery-image ec-gallery-placeholder">

                            <i class="fa fa-image"></i>

                        </div>

                    <?php endif; ?>

                    <div class="ec-gallery-overlay"></div>

                    <div class="ec-gallery-content">

                        <?php if ($featured): ?>

                            <div class="ec-gallery-badge">

                                <i class="fa fa-star"></i>

                                <?= $lang === 'ar'
                                    ? 'مميز'
                                    : 'Featured' ?>

                            </div>

                        <?php endif; ?>

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

                            <?= pcb_h($title ?: (
                                $lang === 'ar'
                                ? 'فعالية أكاديمية'
                                : 'Academic Event'
                            )) ?>

                        </div>

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

                            <?= pcb_h(
                                mb_substr(
                                    strip_tags($desc),
                                    0,
                                    180
                                )
                            ) ?>

                        </div>

                        <div class="ec-gallery-actions">

                            <a href="#"
                               class="ec-paper-btn">

                                <i class="fa fa-expand"></i>

                                <?= $lang === 'ar'
                                    ? 'عرض'
                                    : 'View' ?>

                            </a>

                        </div>

                    </div>

                </div>

            <?php endforeach; ?>

        </div>

    <?php endif; ?>

</section>
