<?php

require_once __DIR__ . '/_cms_helpers.php';
require_once BASE_PATH . '/app/helpers/media_helper.php';

$sectionConfig =
    ecms_get_section_config('sponsors');

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

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

$lang =
    $lang ?? 'en';

$currentSection =
    $currentSection ?? [];

$tierGroups = [];

foreach ($sponsors as $sx) {

    if (
        !empty($conference['id'])
        &&
        !empty($sx['conference_id'])
        &&
        (int)$sx['conference_id']
            !== (int)$conference['id']
    ) {
        continue;
    }

    $tierKey =
        strtolower(
            trim(
                (string)(
                    $sx['sponsor_tier']
                    ?? 'partner'
                )
            )
        );

    if ($tierKey === '') {
        $tierKey = 'partner';
    }

    if (!isset($tierGroups[$tierKey])) {
        $tierGroups[$tierKey] = [];
    }

    $tierGroups[$tierKey][] = $sx;
}

?>

<section

data-cms-section="sponsors"

id="sponsors"

class="
ecms-managed-section
ec-sponsors-wrap
ec-section-shell
ec-section-anchor

<?= pcb_h(
$currentSection['theme_name']
?? ''
) ?>

<?= pcb_h(
$currentSection['custom_css_class']
?? ''
) ?>
"

<?php if(!empty($currentSection['background_image'])): ?>

style="
background-image:
linear-gradient(
rgba(15,23,42,.94),
rgba(15,23,42,.94)
),
url('<?= pcb_h(
ec_media_url(
$currentSection['background_image']
)
) ?>');

background-size:cover;
background-position:center;
background-repeat:no-repeat;
"

<?php endif; ?>

>

<div class="container">

<div class="proceedings-header">

<div>

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

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

<?= pcb_h(
$lang === 'ar'
? (
    $currentSection['section_title_ar']
    ?? 'الرعاة والشركاء'
)
: (
    $currentSection['section_title_en']
    ?? 'Sponsors & Partners'
)
) ?>

</div>

<h2 class="section-title">

<?= pcb_h(
$lang === 'ar'
? (
    $currentSection['title_ar']
    ?? 'الرعاة والشركاء الأكاديميون'
)
: (
    $currentSection['title_en']
    ?? 'Academic Sponsors & Strategic Partners'
)
) ?>

</h2>

<p class="pcb-section-text mt-4">

<?= pcb_h(
$lang === 'ar'
? (
    $currentSection['subtitle_ar']
    ?? 'شراكات استراتيجية وتعاون أكاديمي دولي لدعم الابتكار والبحث العلمي.'
)
: (
    $currentSection['subtitle_en']
    ?? 'Strategic partnerships and international academic collaboration supporting innovation and scientific research.'
)
) ?>

</p>

</div>

<div class="proceeding-meta">

<span>

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

<?= count($sponsors) ?>

<?= pcb_h(
$lang === 'ar'
? 'شريك'
: 'Partners'
) ?>

</span>

<span>

<i class="fa fa-earth-americas"></i>

<?= pcb_h(
$lang === 'ar'
? 'تعاون دولي'
: 'Global Collaboration'
) ?>

</span>

</div>

</div>

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

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

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

<h4 class="fw-bold">

<?= pcb_h(
$lang === 'ar'
? 'سيتم الإعلان عن الرعاة قريباً'
: 'Sponsors will be announced soon'
) ?>

</h4>

</div>

<?php else: ?>

<?php foreach($tierGroups as $tier => $tierSponsors): ?>

<?php

if ($lang === 'ar') {

    switch($tier) {

        case 'platinum':
            $tierTitle =
                'الرعاة البلاتينيون';
        break;

        case 'gold':
            $tierTitle =
                'الرعاة الذهبيون';
        break;

        case 'silver':
            $tierTitle =
                'الرعاة الفضيون';
        break;

        case 'bronze':
            $tierTitle =
                'الرعاة البرونزيون';
        break;

        default:
            $tierTitle =
                'الشركاء الأكاديميون';
    }

} else {

    switch($tier) {

        case 'platinum':
            $tierTitle =
                'Platinum Sponsors';
        break;

        case 'gold':
            $tierTitle =
                'Gold Sponsors';
        break;

        case 'silver':
            $tierTitle =
                'Silver Sponsors';
        break;

        case 'bronze':
            $tierTitle =
                'Bronze Sponsors';
        break;

        default:
            $tierTitle =
                'Academic Partners';
    }
}

?>

<div class="ec-tier-block mb-5">

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

<?= pcb_h($tierTitle) ?>

</div>

<div class="ec-sponsor-wall">

<?php foreach($tierSponsors as $sponsor): ?>

<?php

$name =
    $lang === 'ar'
    ? (
        $sponsor['name_ar']
        ?? $sponsor['name_en']
        ?? $sponsor['sponsor_name']
        ?? ''
    )
    : (
        $sponsor['name_en']
        ?? $sponsor['name_ar']
        ?? $sponsor['sponsor_name']
        ?? ''
    );

$logo =
    trim(
        (string)(
            $sponsor['logo']
            ?? ''
        )
    );

$website =
    trim(
        (string)(
            $sponsor['website']
            ?? ''
        )
    );

if ($website === '') {
    $website = '#';
}

?>

<a
href="<?= pcb_h($website) ?>"
target="<?= $website === '#'
? '_self'
: '_blank' ?>"
class="ec-sponsor-wall-item">

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

<img
src="<?= pcb_h(
ec_media_url($logo)
) ?>"
alt="<?= pcb_h($name) ?>"
loading="lazy">

<?php else: ?>

<div class="ec-sponsor-fallback">

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

</div>

<?php endif; ?>

<div class="ec-sponsor-wall-name">

<?= pcb_h($name) ?>

</div>

</a>

<?php endforeach; ?>

</div>

</div>

<?php endforeach; ?>

<?php endif; ?>

</div>

</section>
