<?php
/**
 * Заглавный блок с ACF полями
 */

$id = 'hero-' . $block['id'];
if (!empty($block['anchor'])) {
    $id = $block['anchor'];
}

$className = 'hero-block';
if (!empty($block['className'])) {
    $className .= ' ' . $block['className'];
}
if (!empty($block['align'])) {
    $className .= ' align' . $block['align'];
}

// Получаем данные из ACF полей
$slider_data = get_field('slider') ?: [];
$text_data = get_field('text') ?: [];


$heading = $text_data['heading'] ?? '';
$heading_text = $text_data['heading_text'] ?? '';
$address = $text_data['adress'] ?? '';
$address_extra = $text_data['adress_extra'] ?? '';

$slides = [];
if (!empty($slider_data) && is_array($slider_data)) {
    foreach ($slider_data as $index => $slide) {
        $image_url = '';
        if (!empty($slide['image']) && is_array($slide['image'])) {
            $image_url = $slide['image']['url'] ?? '';
        }

        $slides[] = [
            'number' => sprintf('%02d', $index + 1),
            'title' => $slide['text'] ?? '',
            'img' => $image_url
        ];
    }
}


?>

<section class="container mx-auto hero-block">
    <div class="flex justify-between gap-[24px]">
        <div class="flex flex-col w-full mt-[26px]">
            <?php display_icon('logo_fitness') ?>

            <h1 class="font-[500] leading-[110%] text-[48px] mt-[20px]">
                <?php if (!empty($heading)): ?>
                    <?php echo wp_kses_post($heading); ?>
                <?php endif; ?>
            </h1>

            <p class="text-[24px] font-[500] mt-[20px]">
                <?php echo !empty($heading_text) ? esc_html($heading_text) : 'Зал для тех, кто ценит комфорт и сервис'; ?>
            </p>

            <div class="flex gap-[12px] mt-[40px]">
                <a class="!no-underline text-[#f8f8f8] text-[18px] font-[600] justify-center h-[75px] w-full max-w-[281px] rounded-[90px] flex items-center gap-[12px] red-gradient-hover"
                   href="#">
                    Пробная тренировка
                    <?php display_icon('button_arrow_up') ?>
                </a>
                <a class="!no-underline text-[#f8f8f8] text-[18px] font-[600] justify-center h-[75px] w-full max-w-[210px] rounded-[90px] flex items-center gap-[12px] grey-gradient-hover"
                   href="/цены">Смотреть цены</a>
            </div>

            <div class="flex items-center gap-[12px] mt-[50px]">
                <?php display_icon('location') ?>
                <div>
                    <p>
                        <b><?php echo !empty($address) ? esc_html($address) : 'Красноармейская, 120 — 2ой этаж'; ?></b>
                    </p>
                    <p>
                        <?php echo !empty($address_extra) ? esc_html($address_extra) : 'Главный вход со стороны ул. Косарева'; ?>
                    </p>
                </div>
            </div>
        </div>

        <div class="w-full max-w-[648px] h-[575px] relative">
            <div class="swiper rounded-[25px] w-full h-full">
                <div class="swiper-wrapper">
                    <?php foreach ($slides as $index => $slide): ?>
                        <div class="swiper-slide [&>_img]:max-w-none">
                            <?php if (!empty($slide['img'])): ?>
                                <img src="<?php echo esc_url($slide['img']); ?>" alt="<?php echo esc_attr($slide['title']); ?>">
                            <?php endif; ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <div class="inverted-radius-wrapper absolute z-[1] pt-[15px] pr-[15px] rounded-[25px] rounded-bl-none rounded-br-none rounded-tl-none flex items-center gap-[4px] bg-[#f9f9f9] left-0 bottom-0">
                <button class="group custom-prev cursor-pointer">
                    <?php display_icon('slider-prev') ?>
                </button>
                <button class="group custom-next cursor-pointer">
                    <?php display_icon('slider-next') ?>
                </button>
            </div>
        </div>
    </div>

    <div class="flex gap-[24px] mt-[44px]">
        <?php foreach ($slides as $index => $slide): ?>
            <div class="pagination-item <?php echo $index === 0 ? 'flex-[2]' : 'flex-1' ?> cursor-pointer transition-all"
                 data-slide="<?php echo $index ?>">
                <div class="pagination-line <?php echo $index === 0 ? 'bg-[#222]' : 'bg-gray-[#e0e0e0]' ?> h-[2px] rounded-[30px] transition-colors"></div>
                <div class="slide-text <?php echo $index === 0 ? 'opacity-100' : 'opacity-0' ?> transition-opacity">
                    <span class="slide-number"><?php echo esc_html($slide['number']); ?>.</span>
                    <span class="slide-title"><?php echo esc_html($slide['title']); ?></span>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</section>

<?php if (is_admin()): ?>
<script>


    function initAdminSwiper() {
        const selectors = [
            '#hero-<?php echo $block['id']; ?> .swiper',
            '[data-block="<?php echo $block['id']; ?>"] .swiper',
            '.hero-block[data-slides] .swiper',
            '.swiper'
        ];

        let swiperContainer = null;

        for (let selector of selectors) {
            swiperContainer = document.querySelector(selector);
            console.log(`🔍 Checking selector "${selector}":`, swiperContainer);
            if (swiperContainer) break;
        }


        try {
            const swiper = new Swiper(swiperContainer, {
                slidesPerView: 1,
                loop: true,
                autoplay: {
                    delay: 4000,
                    disableOnInteraction: false,
                },
                navigation: {
                    nextEl: '.custom-next',
                    prevEl: '.custom-prev',
                }
            });
        } catch (error) {

        }
    }


    if (typeof wp !== 'undefined' && wp.domReady) {
        wp.domReady(function() {
            setTimeout(initAdminSwiper, 300);
        });
    }
</script>
<?php endif; ?>
