<?php
/*
Template Name: members-page
*/

    include('header-member.php');

?>
<div id="contents">

	<div id="main_content">
    <div class="members_container">
      <img class="left_image" src="<?php bloginfo('stylesheet_directory'); ?>/img/members/doctor_men.png" alt="Doctor Men"></a></p>
      <div class="members_title_item">
        <h1><?php the_title(); ?></h1>
        <h2><?php echo get_queried_object()->post_name; ?></h2>
      </div>
      <img class="right_image" src="<?php bloginfo('stylesheet_directory'); ?>//img/members/doctor_lady.png" alt="Doctor Lady"></a></p>
    </div>
    <?php if( is_user_logged_in() ) : ?>
      <article id="main">
      <div class="members_menu_box">
        <div class="members_menu_item">
          <h3>会員メニュー</h3>
          <ul>
            <li>
              <a class="members_menu_btn" href="<?php echo get_permalink(get_page_by_path('members/schedule/')); ?>">
                <div class="menu_btn_container">
                    <div class="menu_btn_item">
                      <h4>会の予告（一覧）</h4>
                      <img src="<?php bloginfo('stylesheet_directory'); ?>/img/members/notice.svg" alt="会の予告のアイコン"></p>
                    </div>
                </div>
              </a>
            </li>
            <li class="center_btn">
            <a class="members_menu_btn" href="<?php echo get_permalink(get_page_by_path('/members/program/protocol/')); ?>">
                <div class="menu_btn_container">
                  <div class="menu_btn_item">
                    <h4>プログラム確認</h4>
                    <img src="<?php bloginfo('stylesheet_directory'); ?>/img/members/program.svg" alt="プログラム確認のアイコン"></p>
                  </div>
                </div>
              </a>
            </li>
            <li>
              <a class="members_menu_btn" href="<?php echo get_permalink(get_page_by_path('members/download/')); ?>">
                <div class="menu_btn_container">
                  <div class="menu_btn_item">
                    <h4>資料ダウンロード</h4>
                    <img src="<?php bloginfo('stylesheet_directory'); ?>/img/members/download.svg" alt="資料ダウンロードのアイコン"></p>
                  </div>
                </div>
              </a>
            </li>
            <li>
              <a class="members_menu_btn" href="<?php echo home_url('members/journals/'); ?>">
                <div class="menu_btn_container">
                  <div class="menu_btn_item">
                    <h4>学術雑誌まとめ</h4>
                    <img src="<?php bloginfo('stylesheet_directory'); ?>/img/members/book.svg" alt="学術雑誌まとめのアイコン"></p>
                  </div>
                </div>
              </a>
            </li>
          </ul>
        </div>
      </div>
      
			<div class="section_wrapper">
      <h2>会の予告（全臨糖学術集会）</h2>
      <div class="section_container">
          <section class="sectionLv03">
            <ul id="post_list">
              <?php
                $args = array(
                  'post_type' => 'schedule',
                  'posts_per_page' => 3, // 全ての投稿を表示する
                );
                $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="btn_container">
              <a class="btn_primary" href="<?php echo get_permalink(get_page_by_path('members/schedule/')); ?>">一覧を見る</a>
            </div>

          </section>
				</div>
        <h2>お知らせ</h2>
<!--         <div class="tab_menu postList_tab" data-width-pc="25" data-width-sp="50">
          <ul class="tab_List" role="tablist">
            <li class="active tab_List_item"><button class="tabList_button active" role="tab" aria-controls="post_list_tab_1" aria-selected="true" data-taxonomy-slug="all">新着記事</button></li>
            <li class="tab_List_item"><button class="tabList_button" role="tab" aria-controls="post_list_tab_2" aria-selected="false" data-taxonomy-slug="news">お知らせ</button></li>
            <li class="tab_List_item"><button class="tabList_button" role="tab" aria-controls="post_list_tab_3" aria-selected="false" data-taxonomy-slug="report">活動報告</button></li>
            <li class="tab_List_item"><button class="tabList_button" role="tab" aria-controls="post_list_tab_4" aria-selected="false" data-taxonomy-slug="important">重要</button></li>
          </ul>
        </div> -->

				<div class="section_container">
        <section class="sectionLv01">
            <div class="wrap-tab-content">
              <?php
              // 「新着記事」タブのための条件を設定
              $show_all_posts = false;

              $tabs = array(
                array(
                  'slug' => 'news',
                  'taxonomy_slug' => 'category',
                ),
                array(
                  'slug' => 'report',
                  'taxonomy_slug' => 'category',
                ),
                array(
                  'slug' => 'important',
                  'taxonomy_slug' => 'category',
                ),
                array(
                  'slug' => 'all', // 全カテゴリーを表示するための追加
                ),
              );

              foreach ($tabs as $index => $tab) {
                if ($tab['slug'] === 'all' && $show_all_posts) {
                  $args = array(
                    'post_type' => 'post',
                    'posts_per_page' => 5,
                    'orderby' => 'date',
                    'order' => 'DESC',
                  );
                } else {
                  $args = array(
                    'post_type' => 'post',
                    'posts_per_page' => 5,
                    'tax_query' => array(),
                  );

                  if ($tab['slug'] !== 'all') {
                    $args['tax_query'][] = array(
                      'field' => 'slug',
                      'terms' => $tab['slug'],
                      'posts_per_page' => 5,
                    );
                  }
                }

                $query = new WP_Query($args);

                if ($query->have_posts()) {
                  $active_class = ($index === 0) ? ' active' : ''; // 最初のタブに active クラスを追加 ?>
                  <ul id="post_list_<?php echo $tab['slug']; ?>" class="tab-content<?php echo $active_class; ?>">
                    <?php while ($query->have_posts()) {
                      $query->the_post();
                      $categories = get_the_terms(get_the_ID(), 'category');
                      $category_classes = array();

                      if ($categories) {
                        foreach ($categories as $category) {
                          $category_classes[] = $category->slug;
                        }
                      } ?>

                      <li class="post <?php echo implode(' ', $category_classes); ?>">
                        <div class="meta">
                          <div class="date_container">
                            <span class="post-date"><?php echo get_the_date('Y.n.j'); ?></span>
                          </div>
                          <span class="categories">
                            <?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; ?>
                          </span>
                        </div>
                        <a class="news_title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                      </li>
                    <?php } ?>
                  </ul>
                <?php }

                wp_reset_postdata();
              }
              ?>
            </div>
            <div class="btn_container">
              <a class="btn_primary" href="<?php echo get_permalink(get_page_by_path('all_news')); ?>">一覧を見る</a>
            </div>
          </section>
				</div>
			</div>
		</article>
    <?php endif; ?>
    <?php
    $login_page = get_page_by_path('login'); // パーマリンクが"login"の固定ページを取得
    if ($login_page) {
        $login_page_content = $login_page->post_content; // "login"ページのコンテンツを取得
        echo apply_filters('the_content', $login_page_content); // "login"ページのコンテンツを表示
    }
    ?>


	</div>
</div>
<?php get_footer(); ?>