<?php
/*
Template Name: 会の予告一覧
*/
include('header-member.php');
?>
<div id="contents">
  <div id="main_content">

    <div class="title_container">
      <h1>会の予告一覧</h1>
      <h2>schedule</h2>
    </div>

    <article class="common-page-main" id="main">
      <div class="section_wrapper ">
        <div class="common_list_container">
          <?php
          if (function_exists('custom_breadcrumb')) {
            custom_breadcrumb();
          }
          ?>

      <h2>会の予告（全臨糖学術集会）</h2>
      <div class="section_container">
          <section class="sectionLv03">
            <ul id="post_list">
              <?php
                $args = array(
                  'post_type' => 'schedule',
                  'posts_per_page' => 10, // 全ての投稿を表示する
                );
                $query = new WP_Query($args);

                if ($query->have_posts()) :
                  while ($query->have_posts()) :
                    $query->the_post();
                    $categories = get_the_category(); // 投稿のカテゴリーを取得

                    if ($categories) {
                      $category_slugs = array(); // カテゴリーのスラッグを格納する配列

                      foreach ($categories as $category) {
                        // 各カテゴリーのスラッグを配列に追加
                        $category_slugs[] = $category->slug;
                      }

                      $category_classes = implode(' ', $category_slugs); // クラスをスペースで連結
                    } else {
                      $category_classes = ''; // カテゴリーがない場合は空のクラスとする
                    }
              ?>
              <?php
              $end_date = get_field('field_64af9119689e6'); // ACFで取得した終了日フィールドの値
              $end_date_timestamp = strtotime($end_date); // 終了日をタイムスタンプに変換
              $today_timestamp = current_time('timestamp'); // 現在のタイムスタンプを取得


              if ($end_date_timestamp < $today_timestamp) {
                $post_classes = 'post schedule_post expired-event';
              } else {
                $post_classes = 'post schedule_post';
              }
              ?>

              <li class="<?php echo $post_classes; ?>">
                <?php foreach ($categories as $category) : ?>
                  <a class="<?php echo $category->slug; ?>" href="<?php echo esc_url(get_category_link($category->term_id)); ?>" rel="category tag"><?php echo $category->name; ?></a>
                <?php endforeach; ?>
                <h5>
                  <a><?php the_title(); ?></a>
                  <?php
                  $start_date = get_field('field_64af90ed689e5');
                  $end_date = get_field('field_64af9119689e6');
                  if ($start_date && $end_date) {
                    $start_date_timestamp = strtotime($start_date);
                    $end_date_timestamp = strtotime($end_date);
                    $today_timestamp = time();
                    
                    if ($end_date_timestamp < $today_timestamp) {
                      echo '<span class="status-label">終了</span>';
                    } else {
                      if ($start_date_timestamp <= $today_timestamp && $end_date_timestamp >= $today_timestamp) {
                        echo '<span class="status-label">開催中</span>';
                      }
                    }
                  }
                  ?>
                </h5>
                <ul>
                  <li>会期
                    <span class="schedule_post_punctuation">:</span>
                    <?php
                    $start_date = get_field('field_64af90ed689e5');
                    $end_date = get_field('field_64af9119689e6');
                    if ($start_date && $end_date) {
                      $formatted_start_date = date('Y年 n月j日', strtotime($start_date));
                      $formatted_end_date = date('Y年 n月j日', strtotime($end_date));
                      echo $formatted_start_date . ' ～ ' . $formatted_end_date;
                    }
                    ?>
                  </li>
                  <li>会場
                    <span class="schedule_post_punctuation">:</span>
                    <?php $value = get_field('field_64af915a689e7'); if ($value) { echo $value; } ?>
                  </li>
                </ul>
              </li>
              </li>
              
              <?php
                  endwhile;
                else :
                  echo '投稿がありません。';
                endif;
                wp_reset_postdata();
              ?>
            </ul>
             <!-- ページネーションの表示 -->
             <div class="pagination">
                  <?php
                  echo paginate_links(array(
                    'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
                    'format' => '/page/%#%',
                    'current' => max(1, get_query_var('paged')),
                    'total' => $query->max_num_pages,
                  ));
                  ?>
                </div>

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

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

<?php get_footer(); ?>