<?php
/**
 * Блок с клубными картами
 */

// Получение полей ACF
$block_id = 'club-cards-' . $block['id'];
$class_name = 'club-cards-block';
if (!empty($block['className'])) {
    $class_name .= ' ' . $block['className'];
}
if (!empty($block['align'])) {
    $class_name .= ' align' . $block['align'];
}

if (!function_exists('get_club_cards_for_current_language')) {
    function get_club_cards_for_current_language() {
        $current_lang = pll_current_language();

        $args = array(
            'post_type' => 'club-card',
            'posts_per_page' => -1,
            'lang' => $current_lang,
            'post_status' => 'publish'
        );

        return get_posts($args);
    }
}

$cards = get_club_cards_for_current_language();
$heading = get_field('heading', $block['id']);
?>

<section id="<?php echo esc_attr($block_id); ?>" class="<?php echo esc_attr($class_name); ?>">
    <div class="container mx-auto mt-[24px]">
        <?php if ($heading) : ?>
            <h2 class="text-[32px] font-bold mt-[24px]"><?php echo esc_html($heading); ?></h2>
        <?php endif; ?>
        <?php if ($cards): ?>
            <div class="mt-[24px] grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-[30px] max-[768px]:gap-[20px]">
                <?php foreach ($cards as $card): ?>
                    <div class="bg-[#ffffff] border-[1px] border-[#e5e7eb] rounded-[16px] overflow-hidden shadow-lg hover:shadow-xl transition-shadow ">
                        <?php
                        $card_image = get_field('image', $card->ID);
                        if ($card_image): ?>
                            <div class="overflow-hidden max-w-[60px] max-h-[60px] mx-auto mt-[22px] rounded-full">
                                <img src="<?php echo esc_url($card_image['url']); ?>"
                                     alt="<?php echo esc_attr($card_image['alt']); ?>"
                                     class="w-full h-full object-cover" />
                            </div>
                        <?php endif; ?>

                        <div class="p-[30px] max-[768px]:p-[20px]">
                            <?php $card_heading = get_field('heading', $card->ID); ?>
                            <?php if ($card_heading): ?>
                                <h3 class="text-[24px] max-[768px]:text-[20px] font-semibold text-[#1f2937] mb-[16px] text-center">
                                    <?php echo esc_html($card_heading); ?>
                                </h3>
                            <?php endif; ?>

                            <?php $card_description = get_field('description', $card->ID); ?>
                            <?php if ($card_description): ?>
                                <div class="text-[16px] max-[768px]:text-[14px] text-[#6b7280] leading-[150%] text-center mb-[16px]">
                                    <?php echo wp_kses_post($card_description); ?>
                                </div>
                            <?php endif; ?>

                            <?php $card_price = get_field('price', $card->ID); ?>
                            <?php if ($card_price): ?>
                                <div class="text-[20px] max-[768px]:text-[18px] font-bold text-[#1f2937] text-center mb-[24px]">
                                  <span>От</span>  <?php echo esc_html($card_price); ?> Р.
                                </div>

                            <?php endif; ?>

                            <div class="text-center mt-auto">
                                <?php $popup_button = get_field('popup_button', $card->ID); ?>
                                <button data-modal="club-card"
                                        data-card-id="<?php echo $card->ID; ?>"
                                        class="inline-block cursor-pointer px-[24px] py-[12px] bg-[#3b82f6] text-white rounded-[8px] hover:bg-[#2563eb] transition-colors ">
                                    <?php echo $popup_button ? esc_html($popup_button) : 'Подробнее'; ?>
                                </button>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>