<?php
/**
 * File Path: templates/admin/series-card.php
 *
 * Admin Template — Single Series Card
 *
 * Renders the collapsible <details>/<summary> card for one series term on
 * the Settings → Series tab. Wraps the card in its own <form> so each
 * series saves independently via the PRG pattern in MBS_Tab_Series.
 *
 * Expected variables (passed via mbs_get_template_part() $args):
 *   WP_Term  $series           The series taxonomy term.
 *   int      $term_id          Series term ID.
 *   int      $book_count       Number of books assigned to this series.
 *   string   $description      Series description (plain text).
 *   int      $banner_id        Attachment ID of the series banner image (0 if none).
 *   string   $banner_url       URL of the banner preview (empty string if none).
 *   string   $series_display_mode Per-series display mode ('' | 'order' | 'reading_order' | 'both').
 *   bool     $complete         Whether the series is marked complete.
 *   bool     $is_open          Whether this card should start expanded.
 *   string   $edit_url         URL to the WordPress term-edit screen for this series.
 *   array    $book_ids         Ordered array of book post IDs.
 *   array    $book_display_map Keyed by book ID; each entry has 'title', 'series_number', 'thumb'.
 *   bool     $has_persisted    Whether a custom reading order has been saved.
 *   string   $meta_line        Pre-formatted summary line for the card header.
 *   string   $term_layout      Saved per-term layout slug ('' = use global default).
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variables injected by mbs_get_template_part(); not global vars

$series_name_lower = function_exists( 'mb_strtolower' ) ? mb_strtolower( $series->name ) : strtolower( $series->name );
?>
<form method="post" action="" class="mbs-series-card-form mbs-entity-card-form" data-name="<?php echo esc_attr( $series_name_lower ); ?>">
	<details class="mbs-settings-card mbs-series-card mbs-entity-card"
	         data-term-id="<?php echo esc_attr( $term_id ); ?>"<?php echo $is_open ? ' open' : ''; ?>>
		<summary class="mbs-series-card-summary mbs-entity-card-summary">
			<span class="mbs-series-card-heading mbs-entity-card-heading">
				<span class="mbs-series-card-name mbs-entity-card-name"><?php echo esc_html( $series->name ); ?></span>
				<span class="mbs-series-card-meta mbs-entity-card-meta"><?php echo esc_html( $meta_line ); ?></span>
			</span>
		</summary>

		<div class="mbs-series-card-body mbs-entity-card-body">

			<div class="mbs-entity-card-nav-links">
				<a class="mbs-series-card-edit-term mbs-entity-card-edit-link" href="<?php echo esc_url( $edit_url ); ?>">
					<?php esc_html_e( 'Edit slug in WordPress →', 'my-book-showroom' ); ?>
				</a>
				<?php
				$_mbs_view_url = get_term_link( $series );
				if ( ! is_wp_error( $_mbs_view_url ) ) :
				?>
				<a class="mbs-series-card-view-term mbs-entity-card-view-link button button-secondary button-small"
				   href="<?php echo esc_url( $_mbs_view_url ); ?>"
				   target="_blank"
				   rel="noopener noreferrer">
					<?php esc_html_e( 'View Series →', 'my-book-showroom' ); ?>
				</a>
				<?php endif; ?>
			</div>

			<?php wp_nonce_field( 'mbs_series_save', 'mbs_series_nonce' ); ?>
			<input type="hidden" name="mbs_save_series_id" value="<?php echo esc_attr( $term_id ); ?>">

			<div class="mbs-settings-field-cell mbs-series-field-wide">
				<label for="mbs-series-description-<?php echo esc_attr( $term_id ); ?>">
					<?php esc_html_e( 'Description', 'my-book-showroom' ); ?>
				</label>
				<textarea id="mbs-series-description-<?php echo esc_attr( $term_id ); ?>"
				          name="mbs_series_description[<?php echo esc_attr( $term_id ); ?>]"
				          class="mbs-series-description-field"
				          rows="3"
				          placeholder="<?php esc_attr_e( 'Short series description…', 'my-book-showroom' ); ?>"><?php echo esc_textarea( $description ); ?></textarea>
			</div>

			<!-- ── Series Banner Image (F-011 — PRO display) ───────────────────── -->
			<div class="mbs-settings-field-cell mbs-series-field-banner">
				<label><?php esc_html_e( 'Series Banner Image', 'my-book-showroom' ); ?></label>
				<?php if ( mbs_is_pro() || mbs_is_pub() ) : ?>
				<div class="mbs-series-banner-wrap">
					<img id="mbs-series-banner-preview-<?php echo esc_attr( $term_id ); ?>"
					     class="mbs-series-banner-img"
					     src="<?php echo esc_url( $banner_url ); ?>"
					     alt=""
					     <?php echo $banner_url ? '' : 'hidden'; ?>>
					<input type="hidden"
					       id="mbs-series-banner-input-<?php echo esc_attr( $term_id ); ?>"
					       name="mbs_series_banner_id[<?php echo esc_attr( $term_id ); ?>]"
					       value="<?php echo esc_attr( $banner_id ?: '' ); ?>">
					<div class="mbs-series-banner-controls">
						<button type="button"
						        class="button mbs-series-banner-select"
						        data-term-id="<?php echo esc_attr( $term_id ); ?>"
						        data-preview="mbs-series-banner-preview-<?php echo esc_attr( $term_id ); ?>"
						        data-input="mbs-series-banner-input-<?php echo esc_attr( $term_id ); ?>">
							<?php echo $banner_url
								? esc_html__( 'Change', 'my-book-showroom' )
								: esc_html__( 'Add Banner', 'my-book-showroom' ); ?>
						</button>
						<button type="button"
						        class="button mbs-series-banner-remove"
						        data-term-id="<?php echo esc_attr( $term_id ); ?>"
						        data-preview="mbs-series-banner-preview-<?php echo esc_attr( $term_id ); ?>"
						        data-input="mbs-series-banner-input-<?php echo esc_attr( $term_id ); ?>"
						        <?php echo $banner_url ? '' : 'hidden'; ?>>
							<?php esc_html_e( 'Remove', 'my-book-showroom' ); ?>
						</button>
					</div>
					<p id="mbs-series-banner-hint-<?php echo esc_attr( $term_id ); ?>"
					   class="description mbs-series-banner-hint"
					   hidden>
						<?php esc_html_e( 'Image selected — click Save below to persist it.', 'my-book-showroom' ); ?>
					</p>
					<p class="description">
						<?php esc_html_e( 'Displayed above the book grid on the series archive page. Recommended: 1200 × 320 px (4:1 ratio). Taller images are cropped to 320 px on desktop, 180 px on mobile.', 'my-book-showroom' ); ?>
					</p>
				</div>
				<?php else : ?>
				<p class="description">
					<?php esc_html_e( 'Upgrade to PRO to display a full-width banner image above the book grid on the series archive page. Recommended: 1200 × 320 px (4:1 ratio).', 'my-book-showroom' ); ?>
				</p>
				<?php endif; ?>
			</div>

			<div class="mbs-series-options-grid">
				<div class="mbs-series-display-mode-cell">
					<?php $_mbs_ro_enabled = '1' === get_option( 'mbs_setting_show_series_reading_order', '0' ); ?>
					<label for="mbs-series-display-mode-<?php echo esc_attr( $term_id ); ?>">
						<strong><?php esc_html_e( 'Archive Display Mode', 'my-book-showroom' ); ?></strong>
					</label>
					<select id="mbs-series-display-mode-<?php echo esc_attr( $term_id ); ?>"
					        name="mbs_series_archive_display_mode[<?php echo esc_attr( $term_id ); ?>]">
						<option value="" <?php selected( '', $series_display_mode ); ?>><?php esc_html_e( 'Use Global Setting', 'my-book-showroom' ); ?></option>
						<option value="order" <?php selected( 'order', $series_display_mode ); ?>><?php esc_html_e( 'Series Order only', 'my-book-showroom' ); ?></option>
						<option value="reading_order" <?php selected( 'reading_order', $series_display_mode ); ?>><?php esc_html_e( 'Reading Order only', 'my-book-showroom' ); ?></option>
						<option value="both" <?php selected( 'both', $series_display_mode ); ?>><?php esc_html_e( 'Both (Series Order + Reading Order)', 'my-book-showroom' ); ?></option>
					</select>
					<?php if ( ! $_mbs_ro_enabled ) : ?>
					<p class="description">
						<?php esc_html_e( 'Enable Series Reading Order in Settings -> General -> Display Options to use Reading Order and Both options.', 'my-book-showroom' ); ?>
					</p>
					<?php endif; ?>
				</div>

				<div class="mbs-series-complete-cell">
					<label class="mbs-series-complete-label">
						<input type="checkbox"
						       name="mbs_series_complete[<?php echo esc_attr( $term_id ); ?>]"
						       value="1"
						       <?php checked( true, $complete ); ?>>
						<span>
							<strong><?php esc_html_e( 'This series is complete', 'my-book-showroom' ); ?></strong>
							<small><?php esc_html_e( 'Signals readers that no further books are planned.', 'my-book-showroom' ); ?></small>
						</span>
					</label>
				</div>
			</div>

<?php mbs_get_template_part( 'admin/partials/layout-picker', array(
				'term_id'     => $term_id,
				'term_layout' => $term_layout,
				'desc_text'   => __( 'Layout for this series\'s book grid. Grid is used when nothing is selected.', 'my-book-showroom' ),
			) ); ?>

			<section class="mbs-entity-reading-order-section">
				<h4><?php esc_html_e( 'Reading Order', 'my-book-showroom' ); ?></h4>
				<p class="description">
					<?php if ( empty( $book_display_map ) ) : ?>
						<?php esc_html_e( 'No books are assigned to this series yet. Assign books to this series on the book editor, then return here to set the reading order.', 'my-book-showroom' ); ?>
					<?php elseif ( $has_persisted ) : ?>
						<?php esc_html_e( 'Drag to reorder the books in the recommended reading sequence for this series.', 'my-book-showroom' ); ?>
					<?php else : ?>
						<?php esc_html_e( 'Showing the default order (by series number). Drag to customize and click Save to lock in a custom reading order.', 'my-book-showroom' ); ?>
					<?php endif; ?>
				</p>

				<?php if ( ! empty( $book_display_map ) ) : ?>
					<ul id="mbs-series-reading-order-list-<?php echo esc_attr( $term_id ); ?>"
					    class="mbs-series-book-list mbs-book-order-list">
						<?php foreach ( $book_display_map as $book_id => $book ) : ?>
							<li data-book-id="<?php echo esc_attr( $book_id ); ?>"
							    class="mbs-series-book-item mbs-book-order-item">
								<?php MBS_Sortable_UI::drag_handle( true ); ?>
								<?php MBS_Sortable_UI::reorder_buttons(
									__( 'Move up', 'my-book-showroom' ),
									__( 'Move down', 'my-book-showroom' )
								); ?>
								<?php if ( $book['thumb'] ) : ?>
									<img class="mbs-series-book-thumb mbs-book-order-thumb"
									     src="<?php echo esc_url( $book['thumb'] ); ?>"
									     alt="">
								<?php else : ?>
									<span class="mbs-series-book-thumb-placeholder mbs-book-order-thumb-placeholder" aria-hidden="true"></span>
								<?php endif; ?>
								<span class="mbs-series-book-title mbs-book-order-title">
									<?php echo esc_html( $book['title'] ); ?>
								</span>
								<?php if ( null !== $book['series_number'] ) : ?>
									<span class="mbs-series-book-number mbs-book-order-badge">
										<?php echo esc_html( '#' . $book['series_number'] ); ?>
									</span>
								<?php endif; ?>
							</li>
						<?php endforeach; ?>
					</ul>

					<input type="hidden"
					       id="mbs-series-reading-order-input-<?php echo esc_attr( $term_id ); ?>"
					       name="mbs_series_reading_order[<?php echo esc_attr( $term_id ); ?>]"
					       value="<?php echo esc_attr( wp_json_encode( $book_ids ) ); ?>">

				<?php endif; ?>
			</section>

			<p class="mbs-settings-actions">
				<?php submit_button( __( 'Save', 'my-book-showroom' ), 'primary', 'submit', false ); ?>
			</p>

		</div>
	</details>
</form>
