<?php
/**
 * File Path: templates/admin/metabox-classification-codes.php
 *
 * Template: Classification Metabox — Codes section
 *
 * Rendered by MBS_Metabox_Classification::render_codes() via mbs_get_template_part().
 *
 * Variables injected via compact():
 *
 * @var int      $tier_max          Active tier code maximum.
 * @var bool     $is_free           True when neither PRO nor PUB.
 * @var bool     $is_pub            True when PUB tier.
 * @var string[] $onix_genre_saved  Saved ONIX industry genre classification slugs (scheme 'onix_genre').
 * @var string[] $bisac_active      Active BISAC codes (within tier limit).
 * @var string[] $bisac_overflow    Overflow BISAC codes (beyond tier limit).
 * @var array    $bisac_descs       Code => description map for active BISAC codes.
 * @var string[] $thema_active      Active Thema codes (within tier limit).
 * @var string[] $thema_overflow    Overflow Thema codes (beyond tier limit).
 * @var array    $thema_descs       Code => description map for active Thema codes.
 * @var string   $show_genre        '1' if genre visible on front end.
 * @var string   $show_bisac        '1' if BISAC visible on front end.
 * @var string   $show_thema        '1' if Thema visible on front end.
 * @var string   $bowker_primary    Saved Bowker primary code.
 * @var string   $bowker_secondary  Saved Bowker secondary code.
 * @var array    $bowker_all_codes  All Bowker codes from data/bowker-codes.json.
 * @var string   $lccn              Library of Congress Control Number.
 *
 * @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

wp_nonce_field( 'mbs_classification_save', 'mbs_classification_nonce' );
?>

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

<?php /* -- Section 8: Bowker Subject Codes ------------------------------------ */ ?>
<div class="mbs-class-section mbs-class-section--bowker">
	<h4 class="mbs-class-section-title"><?php esc_html_e( 'Bowker Subject Codes', 'my-book-showroom' ); ?></h4>
	<p class="description" style="margin-bottom:8px;"><?php esc_html_e( 'Used for ISBN registration and Books In Print. Assign a primary and optional secondary subject (max 2).', 'my-book-showroom' ); ?></p>

	<label for="mbs_bowker_primary" style="display:block;margin-bottom:3px;font-weight:600;">
		<?php esc_html_e( 'Primary Subject', 'my-book-showroom' ); ?>
	</label>
	<select id="mbs_bowker_primary" name="mbs_bowker_primary" style="max-width:100%;margin-bottom:10px;">
		<option value=""><?php esc_html_e( '— Select —', 'my-book-showroom' ); ?></option>
		<?php foreach ( $bowker_all_codes as $_bc ) : ?>
		<option value="<?php echo esc_attr( $_bc['code'] ); ?>"
		        <?php selected( $bowker_primary, $_bc['code'] ); ?>>
			<?php echo esc_html( $_bc['description'] ); ?>
		</option>
		<?php endforeach; ?>
	</select>

	<label for="mbs_bowker_secondary" style="display:block;margin-bottom:3px;font-weight:600;">
		<?php esc_html_e( 'Secondary Subject', 'my-book-showroom' ); ?>
	</label>
	<select id="mbs_bowker_secondary" name="mbs_bowker_secondary" style="max-width:100%;">
		<option value=""><?php esc_html_e( '— None —', 'my-book-showroom' ); ?></option>
		<?php foreach ( $bowker_all_codes as $_bc ) : ?>
		<option value="<?php echo esc_attr( $_bc['code'] ); ?>"
		        <?php selected( $bowker_secondary, $_bc['code'] ); ?>>
			<?php echo esc_html( $_bc['description'] ); ?>
		</option>
		<?php endforeach; ?>
	</select>
</div>


<?php /* -- Section 10: LCCN -------------------------------------------------- */ ?>
<div class="mbs-class-section mbs-class-section--lccn">
	<h4 class="mbs-class-section-title">
		<label for="mbs_lccn"><?php esc_html_e( 'LCCN', 'my-book-showroom' ); ?></label>
	</h4>
	<input type="text" id="mbs_lccn" name="mbs_lccn"
	       class="regular-text"
	       value="<?php echo esc_attr( (string) $lccn ); ?>"
	       placeholder="<?php esc_attr_e( 'e.g. 2020012345', 'my-book-showroom' ); ?>">
	<p class="description"><?php esc_html_e( 'Library of Congress Control Number.', 'my-book-showroom' ); ?></p>
</div>


<?php /* -- Section 1: Genre Picker ---------------------------------------------- */ ?>
<div class="mbs-class-section mbs-class-section--genre">
	<h4 class="mbs-class-section-title"><?php esc_html_e( 'Genre', 'my-book-showroom' ); ?></h4>

	<?php if ( $is_free ) : ?>
	<p class="description mbs-class-free-note">
		<?php esc_html_e(
			'FREE plan: 1 BISAC and 1 Thema code maximum. To change your classification, remove your saved codes using × first, then select new genres below. Upgrade to PRO for up to 10 BISAC and Thema codes.',
			'my-book-showroom'
		); ?>
	</p>
	<?php endif; ?>

	<div class="mbs-genre-picker" id="mbs-genre-picker">
		<div class="mbs-genre-step" id="mbs-genre-step-0">
			<label class="mbs-genre-label"><?php esc_html_e( 'Category', 'my-book-showroom' ); ?></label>
			<p class="description" style="margin:0 0 6px;"><?php esc_html_e( 'Select one broad category.', 'my-book-showroom' ); ?></p>
			<div class="mbs-group-buttons">
				<?php foreach ( MBS_Genre_Engine::get_group_map() as $grp_key => $grp ) : ?>
				<button type="button" class="mbs-group-btn button-secondary"
				        data-group="<?php echo esc_attr( $grp_key ); ?>">
					<?php echo esc_html( $grp['label'] ); ?>
				</button>
				<?php endforeach; ?>
			</div>
		</div>

		<div class="mbs-genre-step" id="mbs-genre-step-1" style="display:none;">
			<label class="mbs-genre-label"><?php esc_html_e( 'Subcategory', 'my-book-showroom' ); ?></label>
			<p class="description" style="margin:0 0 6px;"><?php esc_html_e( 'Select one appropriate subcategory.', 'my-book-showroom' ); ?></p>
			<div class="mbs-l1-buttons"></div>
		</div>

		<div class="mbs-genre-step" id="mbs-genre-step-2" style="display:none;">
			<label class="mbs-genre-label"><?php esc_html_e( 'Genre', 'my-book-showroom' ); ?></label>
			<p class="description" style="margin:0 0 6px;"><?php esc_html_e( 'Select the genres that best describe the work.', 'my-book-showroom' ); ?></p>
			<div id="mbs-genre-chips" class="mbs-genre-chips mbs-genre-chips--grid"></div>
		</div>

		<div class="mbs-genre-step" id="mbs-genre-step-3" style="display:none;">
			<div class="mbs-suggestions-group">
				<label class="mbs-genre-label"><?php esc_html_e( 'Suggested BISAC Codes', 'my-book-showroom' ); ?></label>
				<div id="mbs-genre-suggestions" class="mbs-genre-suggestions"></div>
			</div>
		</div>
	</div>

	<?php /* Hidden inputs -- saved genre slugs */ ?>
	<div id="mbs-onix-genres-saved">
	<?php foreach ( $onix_genre_saved as $slug ) : ?>
		<input type="hidden" name="mbs_onix_genres[]" value="<?php echo esc_attr( $slug ); ?>">
	<?php endforeach; ?>
	</div>
</div>

<?php /* -- Section 2: BISAC Codes ---------------------------------------------- */ ?>
<div class="mbs-class-section mbs-class-section--bisac">
	<h4 class="mbs-class-section-title"><?php esc_html_e( 'BISAC Codes', 'my-book-showroom' ); ?></h4>

	<div class="mbs-class-chips" id="mbs-bisac-chips" data-scheme="bisac">
		<?php foreach ( $bisac_active as $code ) : ?>
		<span class="mbs-class-chip">
			<input type="hidden" name="mbs_bisac_codes[]" value="<?php echo esc_attr( $code ); ?>">
			<span class="mbs-class-chip-label"><?php echo esc_html( $code . ( isset( $bisac_descs[ $code ] ) && $bisac_descs[ $code ] ? ' — ' . $bisac_descs[ $code ] : '' ) ); ?></span>
			<button type="button" class="mbs-class-chip-remove" aria-label="<?php esc_attr_e( 'Remove', 'my-book-showroom' ); ?>">&times;</button>
		</span>
		<?php endforeach; ?>
	</div>

	<?php if ( ! empty( $bisac_overflow ) ) : ?>
	<p class="mbs-class-overflow-notice">
		<?php echo esc_html( sprintf(
			/* translators: %d: number of additional codes stored */
			_n(
				'%d additional BISAC code stored — upgrade your plan to edit it.',
				'%d additional BISAC codes stored — upgrade your plan to edit them.',
				count( $bisac_overflow ),
				'my-book-showroom'
			),
			count( $bisac_overflow )
		) ); ?>
	</p>
	<?php foreach ( $bisac_overflow as $overflow_code ) : ?>
	<input type="hidden" name="mbs_bisac_overflow[]" value="<?php echo esc_attr( $overflow_code ); ?>">
	<?php endforeach; ?>
	<?php endif; ?>

	<div class="mbs-class-typeahead-wrap">
		<input type="text" id="mbs-bisac-search" class="mbs-class-search regular-text"
		       data-scheme="bisac"
		       autocomplete="off"
		       placeholder="<?php esc_attr_e( 'Search BISAC codes…', 'my-book-showroom' ); ?>">
		<ul class="mbs-class-results" id="mbs-bisac-results" style="display:none;"></ul>
	</div>
	<p class="description"><?php esc_html_e( 'Type 2 or more characters to search by code or keyword (e.g. "Christian" or "FIC042").', 'my-book-showroom' ); ?></p>
	<p class="description"><?php esc_html_e( 'The first BISAC code is used as the primary subject in ONIX exports.', 'my-book-showroom' ); ?></p>

	<?php if ( $is_free ) : ?>
	<p class="description mbs-class-tier-note"><?php esc_html_e( 'FREE: 1 BISAC code maximum. Upgrade to PRO for up to 10.', 'my-book-showroom' ); ?></p>
	<?php endif; ?>
</div>

<?php /* -- Section 3: Thema Codes ---------------------------------------------- */ ?>
<div class="mbs-class-section mbs-class-section--thema">
	<h4 class="mbs-class-section-title"><?php esc_html_e( 'Thema Codes', 'my-book-showroom' ); ?></h4>
	<p class="description"><?php esc_html_e( 'Select a Thema Code. Type 2 or more characters to search by code or keyword (e.g. "religious" or "FW").', 'my-book-showroom' ); ?></p>

	<div class="mbs-class-chips" id="mbs-thema-chips" data-scheme="thema">
		<?php foreach ( $thema_active as $code ) : ?>
		<span class="mbs-class-chip">
			<input type="hidden" name="mbs_thema_codes[]" value="<?php echo esc_attr( $code ); ?>">
			<span class="mbs-class-chip-label"><?php echo esc_html( $code . ( isset( $thema_descs[ $code ] ) && $thema_descs[ $code ] ? ' — ' . $thema_descs[ $code ] : '' ) ); ?></span>
			<button type="button" class="mbs-class-chip-remove" aria-label="<?php esc_attr_e( 'Remove', 'my-book-showroom' ); ?>">&times;</button>
		</span>
		<?php endforeach; ?>
	</div>

	<?php if ( ! empty( $thema_overflow ) ) : ?>
	<p class="mbs-class-overflow-notice">
		<?php echo esc_html( sprintf(
			/* translators: %d: number of additional codes stored */
			_n(
				'%d additional Thema code stored — upgrade your plan to edit it.',
				'%d additional Thema codes stored — upgrade your plan to edit them.',
				count( $thema_overflow ),
				'my-book-showroom'
			),
			count( $thema_overflow )
		) ); ?>
	</p>
	<?php foreach ( $thema_overflow as $overflow_code ) : ?>
	<input type="hidden" name="mbs_thema_overflow[]" value="<?php echo esc_attr( $overflow_code ); ?>">
	<?php endforeach; ?>
	<?php endif; ?>

	<?php /* Thema suggestions derived from selected BISAC codes */ ?>
	<div class="mbs-suggestions-group" id="mbs-thema-suggestions-group" style="display:none;">
		<label class="mbs-genre-label"><?php esc_html_e( 'Suggested Thema Codes', 'my-book-showroom' ); ?></label>
		<div id="mbs-thema-suggestions" class="mbs-genre-suggestions"></div>
	</div>

	<div class="mbs-class-typeahead-wrap">
		<input type="text" id="mbs-thema-search" class="mbs-class-search regular-text"
		       data-scheme="thema"
		       autocomplete="off"
		       placeholder="<?php esc_attr_e( 'Search Thema codes…', 'my-book-showroom' ); ?>">
		<ul class="mbs-class-results" id="mbs-thema-results" style="display:none;"></ul>
	</div>

	<?php if ( $is_free ) : ?>
	<p class="description mbs-class-tier-note"><?php esc_html_e( 'FREE: 1 Thema code maximum. Upgrade to PRO for up to 10.', 'my-book-showroom' ); ?></p>
	<?php endif; ?>
</div>

<?php /* -- Section 4: Front-End Display --------------------------------------- */ ?>
<div class="mbs-class-section mbs-class-section--display">
	<h4 class="mbs-class-section-title"><?php esc_html_e( 'Front-End Display', 'my-book-showroom' ); ?></h4>
	<label>
		<input type="checkbox" name="mbs_show_genre_public" value="1" <?php checked( '1', $show_genre ); ?>>
		<?php esc_html_e( 'Show Genre on book page', 'my-book-showroom' ); ?>
	</label><br>
	<label>
		<input type="checkbox" name="mbs_show_bisac_public" value="1" <?php checked( '1', $show_bisac ); ?>>
		<?php esc_html_e( 'Show BISAC codes on book page', 'my-book-showroom' ); ?>
	</label><br>
	<label>
		<input type="checkbox" name="mbs_show_thema_public" value="1" <?php checked( '1', $show_thema ); ?>>
		<?php esc_html_e( 'Show Thema codes on book page', 'my-book-showroom' ); ?>
	</label>
</div>


</div><!-- .mbs-classification-metabox -->
