<?php
/**
 * File Path: templates/admin/metabox-classification-target-audience.php
 *
 * Template: Classification Metabox — Target Audience section
 *
 * Rendered by MBS_Metabox_Classification::render_target_audience()
 * via mbs_get_template_part().
 *
 * Nonce is output by the Codes section; not repeated here.
 *
 * Variables injected via compact():
 *
 * @var string   $audience_code     Saved ONIX List 28 audience code.
 * @var array    $audience_options  code => label map (MBS_ONIX_Lists::audience_codes()).
 * @var string   $age_from          Saved age range from value (integer string or '').
 * @var string   $age_to            Saved age range to value (integer string or '').
 * @var string   $grade_from        Saved grade range from value (P,K,1-12 or '').
 * @var string   $grade_to          Saved grade range to value (P,K,1-12 or '').
 * @var array    $warning_terms     WP_Term[] of mbs_content_warning terms.
 * @var int[]    $assigned_warnings Term IDs assigned to this post.
 *
 * @package MyBookShowroom
 * @since   1.3.3
 */

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

<div class="mbs-classification-metabox">

<?php /* -- Section 9: Content Warnings (taxonomy) ---------------------------- */ ?>
<div class="mbs-class-section mbs-class-section--warnings">
	<button type="button"
	        class="mbs-class-section-title mbs-warnings-toggle"
	        aria-expanded="<?php echo empty( $assigned_warnings ) ? 'false' : 'true'; ?>"
	        aria-controls="mbs-warnings-list">
		<?php esc_html_e( 'Content Warnings', 'my-book-showroom' ); ?>
		<span class="mbs-warnings-toggle__chevron" aria-hidden="true"></span>
	</button>
	<ul id="mbs-warnings-list"
	    class="mbs-cat-checklist mbs-cat-checklist--3col"
	    <?php if ( empty( $assigned_warnings ) ) { echo 'hidden'; } ?>>
		<?php if ( empty( $warning_terms ) ) : ?>
		<li class="mbs-warnings-empty">
			<p class="description"><?php esc_html_e( 'No warnings defined yet.', 'my-book-showroom' ); ?></p>
		</li>
		<?php else : ?>
		<?php foreach ( $warning_terms as $term ) : ?>
		<li>
			<label>
				<input type="checkbox"
				       name="mbs_cat_warnings[]"
				       value="<?php echo esc_attr( $term->term_id ); ?>"
				       <?php checked( in_array( $term->term_id, $assigned_warnings, true ) ); ?>>
				<?php echo esc_html( $term->name ); ?>
			</label>
		</li>
		<?php endforeach; ?>
		<?php endif; ?>
	</ul>
	<?php if ( current_user_can( 'manage_options' ) ) : ?>
	<p class="mbs-warnings-manage-link" style="margin:6px 0 0;">
		<a href="<?php echo esc_url( admin_url( 'edit-tags.php?taxonomy=mbs_content_warning&post_type=mbs_book' ) ); ?>" class="button-link">
			<?php esc_html_e( 'Manage Content Warnings list', 'my-book-showroom' ); ?>
		</a>
	</p>
	<?php endif; ?>
</div>

<?php /* -- Section 5: Target Audience (ONIX List 28) ------------------------- */ ?>
<div class="mbs-class-section mbs-class-section--audience">
	<h4 class="mbs-class-section-title">
		<label for="mbs_audience_code"><?php esc_html_e( 'Target Audience', 'my-book-showroom' ); ?></label>
	</h4>
	<select id="mbs_audience_code" name="mbs_audience_code" style="max-width:100%;">
		<option value=""><?php esc_html_e( '— Select —', 'my-book-showroom' ); ?></option>
		<?php foreach ( $audience_options as $code => $label ) : ?>
		<option value="<?php echo esc_attr( $code ); ?>" <?php selected( $audience_code, $code ); ?>>
			<?php echo esc_html( $label ); ?>
		</option>
		<?php endforeach; ?>
	</select>
	<p class="description"><?php esc_html_e( 'ONIX List 28 audience code. Exported in the Audience composite (AudienceCodeType 01).', 'my-book-showroom' ); ?></p>
</div>

<?php /* -- Section 6: Age Range (ONIX List 30 qualifier 17) ------------------ */ ?>
<div class="mbs-class-section mbs-class-section--age-range">
	<h4 class="mbs-class-section-title"><?php esc_html_e( 'Age Range', 'my-book-showroom' ); ?></h4>
	<?php
	$_age_opts = array();
	for ( $y = 3; $y <= 21; $y++ ) {
		/* translators: %d: age in years */
		$_age_opts[ (string) $y ] = sprintf( __( '%d Years', 'my-book-showroom' ), $y );
	}
	?>
	<span style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;">
		<label for="mbs_age_range_from" style="white-space:nowrap;"><?php esc_html_e( 'From Age', 'my-book-showroom' ); ?></label>
		<select id="mbs_age_range_from" name="mbs_age_range_from">
			<option value=""><?php esc_html_e( '— Select —', 'my-book-showroom' ); ?></option>
			<?php foreach ( $_age_opts as $_av => $_al ) : ?>
			<option value="<?php echo esc_attr( $_av ); ?>" <?php selected( $age_from, $_av ); ?>><?php echo esc_html( $_al ); ?></option>
			<?php endforeach; ?>
		</select>
		<label for="mbs_age_range_to" style="white-space:nowrap;"><?php esc_html_e( 'To Age', 'my-book-showroom' ); ?></label>
		<select id="mbs_age_range_to" name="mbs_age_range_to">
			<option value=""><?php esc_html_e( '— Select —', 'my-book-showroom' ); ?></option>
			<?php foreach ( $_age_opts as $_av => $_al ) : ?>
			<option value="<?php echo esc_attr( $_av ); ?>" <?php selected( $age_to, $_av ); ?>><?php echo esc_html( $_al ); ?></option>
			<?php endforeach; ?>
			<option value="" <?php echo ( '' === $age_to && '' !== $age_from ) ? 'selected' : ''; ?>><?php esc_html_e( 'and Up', 'my-book-showroom' ); ?></option>
		</select>
	</span>
	<p class="description"><?php esc_html_e( 'Interest age in years (ONIX List 30 qualifier 17). Leave blank if not applicable. "and Up" as To means no upper bound.', 'my-book-showroom' ); ?></p>
</div>

<?php /* -- Section 7: Grade Range (ONIX List 30 qualifier 11 / List 77) ------ */ ?>
<div class="mbs-class-section mbs-class-section--grade-range">
	<h4 class="mbs-class-section-title"><?php esc_html_e( 'Grade Range', 'my-book-showroom' ); ?></h4>
	<?php
	$grade_values = array(
		'P'  => __( 'Pre-school (P)', 'my-book-showroom' ),
		'K'  => __( 'Kindergarten (K)', 'my-book-showroom' ),
		'1'  => __( 'Grade 1', 'my-book-showroom' ),
		'2'  => __( 'Grade 2', 'my-book-showroom' ),
		'3'  => __( 'Grade 3', 'my-book-showroom' ),
		'4'  => __( 'Grade 4', 'my-book-showroom' ),
		'5'  => __( 'Grade 5', 'my-book-showroom' ),
		'6'  => __( 'Grade 6', 'my-book-showroom' ),
		'7'  => __( 'Grade 7', 'my-book-showroom' ),
		'8'  => __( 'Grade 8', 'my-book-showroom' ),
		'9'  => __( 'Grade 9', 'my-book-showroom' ),
		'10' => __( 'Grade 10', 'my-book-showroom' ),
		'11' => __( 'Grade 11', 'my-book-showroom' ),
		'12' => __( 'Grade 12', 'my-book-showroom' ),
	);
	?>
	<span style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;">
		<label for="mbs_grade_range_from" style="white-space:nowrap;"><?php esc_html_e( 'Academic Grade From', 'my-book-showroom' ); ?></label>
		<select id="mbs_grade_range_from" name="mbs_grade_range_from">
			<option value=""><?php esc_html_e( '— Select —', 'my-book-showroom' ); ?></option>
			<?php foreach ( $grade_values as $val => $lbl ) : ?>
			<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $grade_from, $val ); ?>><?php echo esc_html( $lbl ); ?></option>
			<?php endforeach; ?>
		</select>
		<label for="mbs_grade_range_to" style="white-space:nowrap;"><?php esc_html_e( 'Academic Grade To', 'my-book-showroom' ); ?></label>
		<select id="mbs_grade_range_to" name="mbs_grade_range_to">
			<option value=""><?php esc_html_e( '— Select —', 'my-book-showroom' ); ?></option>
			<?php foreach ( $grade_values as $val => $lbl ) : ?>
			<option value="<?php echo esc_attr( $val ); ?>" <?php selected( $grade_to, $val ); ?>><?php echo esc_html( $lbl ); ?></option>
			<?php endforeach; ?>
			<option value="" <?php selected( $grade_to, '' ); ?>><?php esc_html_e( 'and Up', 'my-book-showroom' ); ?></option>
		</select>
	</span>
	<p class="description"><?php esc_html_e( 'US school grade (ONIX List 30 qualifier 11 / List 77 values). Leave blank if not applicable.', 'my-book-showroom' ); ?></p>
</div>
</div><!-- .mbs-classification-metabox -->
