@php

  /**
   * @param   array $block The block settings and attributes.
   * @param   string $content The block inner HTML (empty).
   * @param   bool $is_preview True during AJAX preview.
   * @param   (int|string) $post_id The post ID this block is saved to.
   */

  $random = wp_rand(0, 100000000);
  $id = 'muy-events-' . $random;
  if (!empty($block['anchor'])) {
      $id = $block['anchor'];
  }

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

  $month_to_show = null;
  $view_type = get_field('view_type');
  $events_teaser_image_height = get_field('events_teaser_image_height', 'muy-height-md');
  $events_per_row = get_field('events_per_row');
  // $products = '';
  // $n = get_field('products_number');
  $title_color = get_field('title_color');
  $excerpt_color = get_field('excerpt_color');
  $cta_color = get_field('cta_color');
  $date_style = get_field('date_style');
  $cta_style = get_field('cta_style');
  $cta_bkg_color = get_field('cta_bkg_color');
  $date_bkg_color = get_field('date_bkg_color');
  $date_text_color = get_field('date_text_color');
  $btn_radius = get_field('btn_radius');
  $date_radius = get_field('date_radius');
  $events_number = get_field('events_number');

  switch ($events_per_row) {
      case '1':
          $column_class = 'w-full teaser-single-column';
          break;

      case '2':
          $column_class = 'w-full md__w-1/2 teaser-multi-column';
          break;

      case '4':
          $column_class = 'w-full md__w-1/2 lg__w-1/4 teaser-multi-column';
          break;

      default:
          $column_class = 'w-full md__w-1/2 lg__w-1/3 teaser-multi-column';

          break;
  }

  switch ($view_type) {
      case 'category':
          $events_print_category = get_field('events_print_category');
          $events = tribe_get_events([
              'posts_per_page' => 100,
              'tax_query' => [
                  [
                      'taxonomy' => 'tribe_events_cat',
                      'field' => 'term_id',
                      'terms' => $events_print_category,
                  ],
              ],
          ]);
          break;

      case 'all':
          $events = tribe_get_events([
              'posts_per_page' => 100,
          ]);
          break;

      case 'past':
          $events = tribe_get_events([
              'posts_per_page' => 100,
              'ends_before' => 'now',
          ]);
          break;

      case 'selected':
          $selected = get_field('selected_events');

          $events = tribe_get_events([
              'post__in' => $selected,
              'posts_per_page' => -1,
              'eventDisplay' => 'custom',
          ]);
          break;

      default:
          $events = tribe_get_events([
              'posts_per_page' => 100,
              'ends_after' => 'now',
          ]);
          break;
  }

@endphp

<div id="{{ esc_attr($id) }}" class="{{ esc_attr($className) }}">
  @if ($view_type == 'selected')
    <div class="">
      <div class="-mx-4 row mb-24">
        @foreach ($events as $event)
          <div class="{{ $column_class }}">
            @include('partials.teaser-event', [
                'titleColor' => $title_color,
                'excerptColor' => $excerpt_color,
                'catColor' => $cta_color,
            ])
          </div>
        @endforeach
      </div>
    </div>
  @else
    @foreach ($events as $event)
      @php
        $month = tribe_get_start_date($event, false, 'F Y');
        $show_header = $month != $month_to_show;
        $month_to_show = $month;
      @endphp



      @if ($show_header && !$loop->first)
</div>
</div><!--container-->
@endif

@if ($show_header)
  <h4 class="section-title mb-10 font-bold" style="font-size:40px;">{{ $month }}</h4>
  <!-- Aggiunto titolo per il mese -->
  <div class="container px-0">
    <div class="-mx-4 row mb-24">
@endif

<div class="{{ $column_class }}">
  @include('partials.teaser-event', [
      'titleColor' => $title_color,
      'excerptColor' => $excerpt_color,
      'ctaStyle' => $cta_style,
      'ctaBackground' => $cta_bkg_color,
      'catColor' => $cta_color,
      'dateStyle' => $date_style,
      'dateBkgColor' => $date_bkg_color,
      'dateTextColor' => $date_text_color,
      'btnRadius' => $btn_radius,
      'dateRadius' => $date_radius,
  ])
</div>

@if ($loop->last)
  </div>
  </div><!--container-->
@endif
@endforeach
@endif
</div>
