<?php get_header(); ?>

<div id="contents">
    <div id="main_content">
        <h2 id="mainimg">
            <span class="main_text">
                全国臨床糖尿病医会
                <span><br>
                        <span class="main_text_en">
                        Japan Clinicians Diabetes Associtation
                        </span>
                        <p>糖尿病診療の進歩のために</p>
                        </span>
            </span>
        </h2>

        <article id="main">
            <div class="section_wrapper">
                <h2>お知らせ</h2>
                <div class="tab_menu postList_tab" data-width-pc="25" data-width-sp="50">
<!--                     <ul class="tab_List" role="tablist">
                        <?php
                        // タブの設定
                        $tabs = array(
                            array('slug' => 'all', 'label' => '新着記事', 'taxonomy_slug' => ''),
                            array('slug' => 'news', 'label' => 'お知らせ', 'taxonomy_slug' => 'category'),
                            array('slug' => 'report', 'label' => '活動報告', 'taxonomy_slug' => 'category'),
                            array('slug' => 'important', 'label' => '重要', 'taxonomy_slug' => 'category'),
                        );

                        foreach ($tabs as $index => $tab) {
                            $tab_slug = $tab['slug'];
                            $tab_label = $tab['label'];
                            $taxonomy_slug = $tab['taxonomy_slug'];

                            $args = array(
                                'post_type' => 'post',
                                'posts_per_page' => 5,
                                'orderby' => 'date',
                                'order' => 'DESC',
                            );

                            if (!empty($taxonomy_slug) && $tab_slug !== 'all') {
                                $args['tax_query'] = array(
                                    array(
                                        'taxonomy' => $taxonomy_slug,
                                        'field' => 'slug',
                                        'terms' => $tab_slug,
                                    ),
                                );
                            }

                            $query = new WP_Query($args);

                            if ($query->have_posts()) {
                                $active_class = ($index === 0) ? ' active' : '';
                        ?>
                                <li class="tab_List_item<?php echo $active_class; ?>">
                                    <button class="tabList_button<?php echo $active_class; ?>" role="tab" aria-controls="post_list_<?php echo $tab_slug; ?>" aria-selected="<?php echo ($index === 0) ? 'true' : 'false'; ?>" data-taxonomy-slug="<?php echo $tab_slug; ?>"><?php echo $tab_label; ?></button>
                                </li>
                        <?php
                            }
                            wp_reset_postdata();
                        }
                        ?>
                    </ul> -->
                </div>

                <div class="section_container">
                    <section class="sectionLv01">
                        <div class="wrap-tab-content">
                            <?php
                            foreach ($tabs as $index => $tab) {
                                $tab_slug = $tab['slug'];
                                $args = array(
                                    'post_type' => 'post',
                                    'posts_per_page' => 5,
                                    'orderby' => 'date',
                                    'order' => 'DESC',
                                );

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

                                $query = new WP_Query($args);

                                if ($query->have_posts()) {
                                    $active_class = ($index === 0) ? ' active' : '';
                                    echo '<ul id="post_list_' . $tab_slug . '" class="tab-content' . $active_class . '">';
                                    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
                                    }
                                    echo '</ul>';
                                }
                                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>
    </div>
</div>

<?php get_footer(); ?>