<?php
/**
 * Event organizers component
 * Displays ALL organizers (not just the first one)
 */
if (!defined('ABSPATH')) exit;

// Get organizer settings - force enable if component is being rendered
$em_organizer_heading = $event_settings['em_organizer_heading'] ?? 'Organized By';
$em_organizer_name = $event_settings['em_organizer_name'] ?? array();
$em_organizer_phone = $event_settings['em_organizer_phone'] ?? array();
$em_organizer_email = $event_settings['em_organizer_email'] ?? array();
$em_organizer_website = $event_settings['em_organizer_website'] ?? array();

// Ensure arrays are actually arrays
if (!is_array($em_organizer_name)) $em_organizer_name = array($em_organizer_name);
if (!is_array($em_organizer_phone)) $em_organizer_phone = array($em_organizer_phone);
if (!is_array($em_organizer_email)) $em_organizer_email = array($em_organizer_email);
if (!is_array($em_organizer_website)) $em_organizer_website = array($em_organizer_website);

// Filter out empty names
$em_organizer_name = array_filter($em_organizer_name);

// Only show if have organizer data
if (empty($em_organizer_name)) {
    return;
}

// Check if magazine layout
$layout_preference = $event_settings['em_layout_preference'] ?? 'default';
$rounded_class = ($layout_preference === 'magazine') ? 'em-rounded-2xl' : '';

// Count organizers
$organizer_count = count($em_organizer_name);
?>
<div class="em-bg-white em-p-5 em-border em-border-slate-200 em-shadow-sm em-h-full em-flex em-flex-col <?php echo esc_attr($rounded_class); ?>">
    <h2 class="em-text-lg em-font-bold em-text-slate-900 em-mb-4"><?php echo esc_html($em_organizer_heading); ?></h2>

    <div class="em-space-y-3">
        <?php
        // Free version: Show only the FIRST organizer (upgrade to Pro for unlimited)
        $first_key = array_key_first($em_organizer_name);
        $organizer_name = $em_organizer_name[$first_key];
        $organizer_phone = isset($em_organizer_phone[$first_key]) ? $em_organizer_phone[$first_key] : '';
        $organizer_email = isset($em_organizer_email[$first_key]) ? $em_organizer_email[$first_key] : '';
        $organizer_website = isset($em_organizer_website[$first_key]) ? $em_organizer_website[$first_key] : '';

        // Generate initials for avatar
        $name_parts = explode(' ', $organizer_name);
        $initials = '';
        foreach ($name_parts as $part) {
            if (!empty($part)) {
                $initials .= strtoupper(substr($part, 0, 1));
                if (strlen($initials) >= 2) break;
            }
        }
        if (strlen($initials) < 2) {
            $initials = strtoupper(substr($organizer_name, 0, 2));
        }

        $color_class = 'em-bg-indigo-100 em-text-indigo-600';
        ?>
        <div class="em-flex em-items-center em-gap-3">
            <div class="em-w-10 em-h-10 em-rounded-full <?php echo esc_attr($color_class); ?> em-flex em-items-center em-justify-center em-font-bold em-text-sm em-flex-shrink-0">
                <?php echo esc_html($initials); ?>
            </div>
            <div class="em-flex-1 em-min-w-0">
                <h4 class="em-font-semibold em-text-slate-800 em-text-sm em-truncate">
                    <?php if (!empty($organizer_website)): ?>
                        <a href="<?php echo esc_url($organizer_website); ?>" target="_blank" class="hover:em-text-indigo-600 em-transition-colors">
                            <?php echo esc_html($organizer_name); ?>
                        </a>
                    <?php else: ?>
                        <?php echo esc_html($organizer_name); ?>
                    <?php endif; ?>
                </h4>
                <p class="em-text-xs em-text-slate-500 em-truncate">
                    <?php
                    $contact_parts = array();
                    if (!empty($organizer_phone)) $contact_parts[] = $organizer_phone;
                    if (!empty($organizer_email)) $contact_parts[] = $organizer_email;

                    if (!empty($contact_parts)) {
                        echo esc_html(implode(' | ', $contact_parts));
                    } else {
                        echo 'Event Organizer';
                    }
                    ?>
                </p>
            </div>
        </div>
    </div>
</div>
