<?php
/** single-journal.php（CPT: journal 用） */
include('header-member.php');
?>

<div id="contents">
  <div id="main_content">

    <div class="title_container">
      <h1>学術雑誌まとめ</h1>
      <h2>journal</h2>
    </div>

    <div class="section_wrapper">
      <div class="common_list_container">

        <?php if (have_posts()) : while (have_posts()) : the_post();
          $toc = get_post_meta(get_the_ID(), '_journal_toc', true);
          if (!is_array($toc)) $toc = [];
        ?>
          <main id="primary" class="site-main">
            <article id="post-<?php the_ID(); ?>" <?php post_class('journal-article'); ?>>

              <header class="entry-header">
                <h2 class="entry-title"><?php the_title(); ?></h2>

                <div class="journal-hero">
                  <div class="journal-container">
                    <div class="journal-cover">
                      <?php if (has_post_thumbnail()) the_post_thumbnail('journal-cover'); // なければ 'medium' ?>
                    </div>

                    <div class="journal-actions" style="margin:8px 0 14px;">
                      <a class="btn_primary btn_download" href="<?php echo esc_url( add_query_arg('download', 'zip', get_permalink()) ); ?>">
                        PDFを一括ダウンロード
                      </a>
                    </div>
                    <p>※一括ダウンロードは時間がかかる場合があります。</p>
                  </div>


                  <div class="journal-toc-wrap">
                    <table class="journal-toc">
                      <thead><tr><th colspan="2">目次</th></tr></thead>
                      <tbody>
                        <?php if ($toc) :
                          foreach ($toc as $row) :
                            $title = isset($row['title']) ? $row['title'] : '';
                            $aid   = isset($row['att_id']) ? (int)$row['att_id'] : 0;
                            $pages = isset($row['pages']) ? $row['pages'] : '';
                            $url   = $aid ? wp_get_attachment_url($aid) : '';
                        ?>
                          <tr>
                            <td class="toc-title">
                              <?php if ($url): ?>
                                <a class="toc-link" href="<?php echo esc_url($url); ?>" target="_blank" rel="noopener">
                                  <span class="pdf-icon" aria-hidden="true"></span>
                                  <span class="toc-text"><?php echo nl2br( esc_html( $title !== '' ? $title : get_the_title($aid) ) ); ?></span>
                                </a>
                              <?php else: ?>
                                <span class="toc-text"><?php echo nl2br( esc_html( $title !== '' ? $title : '（未設定）' ) ); ?></span>
                              <?php endif; ?>
                            </td>
                            <td class="toc-page"><?php echo esc_html($pages); ?></td>
                          </tr>
                        <?php endforeach; else: ?>
                          <tr><td colspan="2">目次は準備中です。</td></tr>
                        <?php endif; ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </header>

              <div class="entry-content">
                <?php
                  $raw = get_post_field('post_content', get_the_ID());
                  if ( get_post_meta(get_the_ID(), '_journal_enable_sc', true) ) {
                    // ★ショートコードON：必要なときだけ通す
                    // 1) まずショートコードを展開
                    $html = do_shortcode( $raw );
                    // 2) 許可タグでサニタイズ（標準の投稿許可タグ）
                    echo wp_kses( wpautop($html), wp_kses_allowed_html('post') );
                  } else {
                    // 通常はセーフモード（ショートコードOFF）
                    echo wp_kses_post( wpautop( $raw ) );
                  }
                ?>
              </div>

            </article>
          </main>
        <?php endwhile; endif; ?>

      </div>
    </div>

  </div>
</div>

<?php get_footer(); ?>