<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Template Part: Press Mentions Section
 */

// Section toggle
if (! (bool) get_theme_mod('pawfect_press_mentions_enabled', true)) { return; }

// Get settings
$max_items = get_theme_mod('pawfect_press_mentions_count', 5);
$items_limit = is_array($max_items) ? (int) $max_items['slider'] : (int) $max_items;

$height_setting = get_theme_mod('pawfect_press_mentions_logo_height', 32);
$height = is_array($height_setting) ? intval($height_setting['slider']) : intval($height_setting);

$gap_setting = get_theme_mod('pawfect_press_mentions_gap', 64);
$gap = is_array($gap_setting) ? intval($gap_setting['slider']) : intval($gap_setting);

$bg_color = get_theme_mod('pawfect_press_mentions_background_color', '#313134');


$press_raw = get_theme_mod('pawfect_press_mentions_content');
$press_items = [];

if (is_string($press_raw) && !empty($press_raw)) {
    $decoded = json_decode($press_raw);
    if (is_array($decoded)) {
        $press_items = array_slice($decoded, 0, $items_limit);
    }
}
?>

<?php if (!empty($press_items)): ?>
    <!--============= Press Mentions Section Section =================-->

    <section class="pawfect-press-mentions pawfect-relative pawfect-py-20 pawfect-border-t pawfect-border-white/10">

        

        <div
            class="pawfect-press-mentions-container pawfect-flex pawfect-flex-wrap pawfect-justify-center pawfect-items-center pawfect-container-narrow pawfect-mx-auto" style="gap: <?php echo esc_attr($gap); ?>px;">
            <?php if (!empty($press_items)): ?>
                <?php foreach ($press_items as $index => $item):
                // Convert object to array if needed
                $item_data = is_object($item) ? (array) $item : $item;

                $image = !empty($item_data['image_url']) ? esc_url($item_data['image_url']) : '';
                $alt = !empty($item_data['title']) ? esc_attr($item_data['title']) : '';
                $link = !empty($item_data['link']) ? esc_url($item_data['link']) : '';
                $text = !empty($item_data['title']) ? esc_html($item_data['title']) : '';
                $open_new_tab = !empty($item_data['open_new_tab']) && $item_data['open_new_tab'] === 'yes' ? '_blank' : '_self';

                if ($image): ?>
                    <div
                        class="pawfect-press-mentions-item pawfect-flex pawfect-flex-col pawfect-items-center pawfect-justify-center pawfect-opacity-60 hover:pawfect-opacity-100 pawfect-transition-opacity pawfect-duration-300"
                        <?php echo pawfect_get_aos_attrs('fade-up', 600, 100 + ($index * 100)); ?>>
                        <?php if ($link): ?>
                            <a href="<?php echo esc_url( $link ); ?>" target="<?php echo esc_attr($open_new_tab); ?>" <?php echo $open_new_tab === '_blank' ? 'rel="noopener noreferrer"' : ''; ?> class="pawfect-flex pawfect-flex-col pawfect-items-center">
                                <img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr( $alt ); ?>"
                                    class="pawfect-grayscale pawfect-opacity-80 hover:pawfect-opacity-100 hover:pawfect-scale-105 pawfect-transition" 
                                    style="height: <?php echo esc_attr($height); ?>px;" />
                                <?php if ($text): ?>
                                    <span class="pawfect-text-xs pawfect-text-white/60 pawfect-mt-2"><?php echo esc_html( $text ); ?></span>
                                <?php endif; ?>
                            </a>
                        <?php else: ?>
                            <img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr( $alt ); ?>"
                                class="pawfect-grayscale pawfect-opacity-80 hover:pawfect-opacity-100 hover:pawfect-scale-105 pawfect-transition" 
                                style="height: <?php echo esc_attr($height); ?>px;" />
                            <?php if ($text): ?>
                                <span class="pawfect-text-xs pawfect-text-white/60 pawfect-mt-2"><?php echo esc_html( $text ); ?></span>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </section>
<?php endif; ?>