<?php

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

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

$heading = get_field('heading');
$work_time = get_field('work_time');
$address = get_field('address');
$contacts = get_field('contacts');

$blocks = parse_blocks(get_the_content());
$is_first_block = false;

if (!empty($blocks) && isset($blocks[0]['blockName']) && $blocks[0]['blockName'] === 'acf/contacts-block') {
    $is_first_block = true;
}

$section_padding = $is_first_block ? "pt-[64px] max-[768px]:pt-[40px]" : "pt-[80px] max-[768px]:pt-[64px]";

?>

<style type="text/css">
    <?php echo '#' . $id; ?> {
    }
    .ymaps-2-1-79-copyright,
    .ymaps-2-1-79-gototech,
    .ymaps-2-1-79-gototaxi,
    .ymaps-2-1-79-gotoymaps,
    .ymaps-2-1-79-map-copyrights-promo,
    .ymaps-2-1-79-copyright__wrap,
    .ymaps-2-1-79-copyright__content-cell,
    [class*="ymaps-2"][class*="copyright"],
    [class*="ymaps-2"][class*="gototech"],
    [class*="ymaps-2"][class*="gototaxi"],
    [class*="ymaps-2"][class*="gotoymaps"] {
        display: none !important;
    }
</style>

<section id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $classes ); ?> <?php echo $section_padding ?>">
    <div class="w-full relative">
        <div id="map" class="w-full h-[500px] min-[1024px]:h-[700px] bg-[#f3f4f6]"></div>

        <div class="absolute inset-0 pointer-events-none">
            <div class="container mx-auto px-[16px] h-full flex items-start pt-[40px]">
                <div class="bg-[#ffffff] border-[1px] border-[#e5e7eb] rounded-[12px] p-[30px] w-full max-w-[380px] shadow-lg pointer-events-auto">

                    <?php if ($heading) : ?>
                        <h2 class="text-[36px] max-[768px]:text-[28px] font-bold mb-[30px] text-[#1f2937]">
                            <?php echo esc_html($heading); ?>
                        </h2>
                    <?php else : ?>
                        <h2 class="text-[36px] max-[768px]:text-[28px] font-bold mb-[30px] text-[#1f2937]">Контакты</h2>
                    <?php endif; ?>

                    <div class="flex flex-col gap-[20px]">

                        <?php if ($work_time) : ?>
                            <div>
                                <?php if (!empty($work_time['heading'])) : ?>
                                    <p class="text-[14px] text-[#6b7280] mb-[8px]"><?php echo esc_html($work_time['heading']); ?></p>
                                <?php endif; ?>

                                <?php if (!empty($work_time['work_time_repeater'])) : ?>
                                    <div class="flex flex-col gap-[4px] mb-[12px]">
                                        <?php foreach ($work_time['work_time_repeater'] as $time_item) : ?>
                                            <div class="flex text-[16px] text-[#1f2937]">
                                                <span><?php echo esc_html($time_item['title']); ?></span>
                                                 
                                                <span><?php echo esc_html($time_item['time']); ?></span>
                                            </div>
                                        <?php endforeach; ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>

                        <?php if ($address) : ?>
                            <div>
                                <?php if (!empty($address['heading'])) : ?>
                                    <p class="text-[14px] text-[#6b7280] mb-[8px]"><?php echo esc_html($address['heading']); ?></p>
                                <?php endif; ?>

                                <?php if (!empty($address['address_repeater'])) : ?>
                                    <div class="flex flex-col gap-[8px] mb-[12px]">
                                        <?php foreach ($address['address_repeater'] as $addr_item) : ?>
                                            <div class="text-[16px] text-[#1f2937] leading-[140%]">
                                                <?php if (!empty($addr_item['address_title'])) : ?>
                                                    <div class="font-semibold"><?php echo esc_html($addr_item['address_title']); ?></div>
                                                <?php endif; ?>
                                                <?php if (!empty($addr_item['address_extra'])) : ?>
                                                    <div><?php echo esc_html($addr_item['address_extra']); ?></div>
                                                <?php endif; ?>
                                            </div>
                                        <?php endforeach; ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>

                        <?php if ($contacts) : ?>
                            <div>
                                <?php if (!empty($contacts['phone'])) : ?>
                                    <div class="mb-[16px]">
                                        <a href="tel:<?php echo esc_attr($contacts['phone']); ?>"
                                           class="!decoration-transparent hover:!decoration-inherit text-[20px] max-[768px]:text-[18px] font-semibold transition-colors">
                                            <?php echo esc_html($contacts['phone']); ?>
                                        </a>
                                    </div>
                                <?php endif; ?>

                                <?php if (!empty($contacts['social_repeater'])) : ?>
                                    <div>
                                        <div class="flex gap-[12px]">
                                            <?php foreach ($contacts['social_repeater'] as $social_item) : ?>
                                                <?php if (!empty($social_item['url'])) : ?>
                                                    <a href="<?php echo esc_url($social_item['url']); ?>"
                                                       target="_blank"
                                                       class="hover:[&>img]:brightness-150 transition">
                                                        <?php if (!empty($social_item['icon'])) : ?>
                                                            <img src="<?php echo esc_url($social_item['icon']['url']); ?>"
                                                                 alt="<?php echo esc_attr($social_item['icon']['alt']); ?>"
                                                                 class="object-contain" />
                                                        <?php endif; ?>
                                                    </a>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        </div>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://api-maps.yandex.ru/2.1/?lang=ru_RU&amp;apikey=03df01cd-1a4b-4cb2-9ae5-d32d62590767"
            type="text/javascript"></script>
</section>