<?php
/**
 * File Path: templates/admin/tab-onix.php
 *
 * ONIX Settings Tab -- HTML partial (R-012)
 *
 * Loaded via mbs_get_template_part( 'admin/tab-onix', [...] ).
 * Only rendered when mbs_is_pub() -- gate enforced in MBS_Tab_Onix::render().
 *
 * Variables injected by MBS_Tab_Onix::render():
 *   @var string       $sender_name     Saved ONIX sender name.
 *   @var string       $sender_contact  Saved sender contact name.
 *   @var string       $sender_email    Saved sender contact email.
 *   @var string       $default_lang    Saved default language code (ISO 639-2/B).
 *   @var string       $publisher_name  Saved publisher name.
 *   @var string       $include_drafts  '1' | '0'.
 *   @var string       $onix_version    '3.0' | '2.1' | '3.1'.
 *   @var array        $language_options All language code => label pairs (from MBS_Tab_Onix::LANGUAGE_OPTIONS).
 *   @var array|false  $import_result   Transient result array from last import, or false.
 *   @var string       $pub_address              Publisher mailing address (Copyright Page Defaults).
 *   @var string       $pub_country              Country of printing default.
 *   @var string       $pub_copyright_disclaimer Global copyright disclaimer text.
 *
 * @package    My_Book_Showroom
 * @subpackage Templates/Admin
 * @since      1.1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:disable WordPress.Security.NonceVerification -- nonce verified in parent class verify_save() / check_admin_referer() before $_POST access; PHPCS cannot trace cross-method nonce verification
?>
<div class="mbs-tab-onix">

	<?php if ( isset( $_GET['mbs_onix_settings_saved'] ) ) : ?>
	<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'ONIX settings saved.', 'my-book-showroom' ); ?></p></div>
	<?php endif; ?>

	<?php if ( false !== $import_result ) : ?>
	<div class="notice notice-info is-dismissible">
		<p><strong><?php esc_html_e( 'Import complete.', 'my-book-showroom' ); ?></strong></p>
		<ul>
			<li><?php printf( esc_html__( 'Created: %d', 'my-book-showroom' ), (int) ( $import_result['created'] ?? 0 ) ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment -- single integer count placeholder ?></li>
			<li><?php printf( esc_html__( 'Updated: %d', 'my-book-showroom' ), (int) ( $import_result['updated'] ?? 0 ) ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment -- single integer count placeholder ?></li>
			<li><?php printf( esc_html__( 'Appended: %d', 'my-book-showroom' ), (int) ( $import_result['appended'] ?? 0 ) ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment -- single integer count placeholder ?></li>
			<li><?php printf( esc_html__( 'Skipped: %d', 'my-book-showroom' ), (int) ( $import_result['skipped'] ?? 0 ) ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment -- single integer count placeholder ?></li>
		</ul>
		<?php if ( ! empty( $import_result['notices'] ) ) : ?>
		<ul class="mbs-onix-import-notices">
		<?php foreach ( $import_result['notices'] as $notice ) : // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global?>
			<li><?php echo wp_kses( $notice, array( 'a' => array( 'href' => array() ) ) ); ?></li>
		<?php endforeach; ?>
		</ul>
		<?php endif; ?>
		<?php if ( ! empty( $import_result['errors'] ) ) : ?>
		<ul class="mbs-onix-import-errors">
		<?php foreach ( $import_result['errors'] as $error ) : ?>
			<li class="error"><?php echo esc_html( $error ); ?></li>
		<?php endforeach; ?>
		</ul>
		<?php endif; ?>
	</div>
	<?php endif; ?>

	<?php /* ---- Section A: Settings ---- */ ?>
	<form method="post" action="">
	<?php wp_nonce_field( 'mbs_onix_settings_save', 'mbs_onix_settings_nonce' ); ?>
	<input type="hidden" name="mbs_onix_settings_submit" value="1">

	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'ONIX Header', 'my-book-showroom' ); ?></h2>
	<table class="form-table">
		<tr>
			<th scope="row"><label for="mbs_onix_sender_name"><?php esc_html_e( 'Sender Name', 'my-book-showroom' ); ?></label> <span class="mbs-onix-required" role="button" tabindex="0" title="<?php esc_attr_e( 'Required for ONIX 2.1, 3.0, and 3.1', 'my-book-showroom' ); ?>" aria-label="<?php esc_attr_e( 'Required', 'my-book-showroom' ); ?>">!</span></th>
			<td>
				<input type="text" id="mbs_onix_sender_name" name="mbs_onix_sender_name"
					class="regular-text" value="<?php echo esc_attr( $sender_name ); ?>"
				       autocomplete="off">
				<p class="description"><?php esc_html_e( 'Your name or organization name, as it appears in the ONIX &lt;SenderName&gt; element.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="mbs_onix_sender_contact"><?php esc_html_e( 'Contact Name', 'my-book-showroom' ); ?></label></th>
			<td>
				<input type="text" id="mbs_onix_sender_contact" name="mbs_onix_sender_contact"
					class="regular-text" value="<?php echo esc_attr( $sender_contact ); ?>"
				       autocomplete="off">
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="mbs_onix_sender_email"><?php esc_html_e( 'Contact Email', 'my-book-showroom' ); ?></label></th>
			<td>
				<input type="email" id="mbs_onix_sender_email" name="mbs_onix_sender_email"
					class="regular-text" value="<?php echo esc_attr( $sender_email ); ?>"
				       autocomplete="off">
			</td>
		</tr>
	</table>

	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'Catalog Defaults', 'my-book-showroom' ); ?></h2>
	<table class="form-table">
		<tr>
			<th scope="row"><label for="mbs_onix_default_language"><?php esc_html_e( 'Default Language', 'my-book-showroom' ); ?></label> <span class="mbs-onix-required" role="button" tabindex="0" title="<?php esc_attr_e( 'Required for ONIX 2.1, 3.0, and 3.1 — every product record must declare a language.', 'my-book-showroom' ); ?>" aria-label="<?php esc_attr_e( 'Required', 'my-book-showroom' ); ?>">!</span></th>
			<td>
				<select id="mbs_onix_default_language" name="mbs_onix_default_language" class="regular-text">
					<?php foreach ( $language_options as $code => $label ) : // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global?>
					<option value="<?php echo esc_attr( $code ); ?>"<?php selected( $default_lang, $code ); ?>><?php echo esc_html( $label . ' (' . $code . ')' ); ?></option>
					<?php endforeach; ?>
				</select>
				<p class="description"><?php esc_html_e( 'Used when a book has no per-book language set. ISO 639-2/B three-letter code.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="mbs_onix_publisher_name"><?php esc_html_e( 'Publisher Name', 'my-book-showroom' ); ?></label> <span class="mbs-onix-required" role="button" tabindex="0" title="<?php esc_attr_e( 'Required for ONIX 2.1, 3.0, and 3.1 — distributors reject records without a publisher name.', 'my-book-showroom' ); ?>" aria-label="<?php esc_attr_e( 'Required', 'my-book-showroom' ); ?>">!</span></th>
			<td>
				<input type="text" id="mbs_onix_publisher_name" name="mbs_onix_publisher_name"
					class="regular-text" value="<?php echo esc_attr( $publisher_name ); ?>">
				<p class="description"><?php esc_html_e( 'Publisher name in &lt;PublisherName&gt;. Falls back to per-book publisher, then site name.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Include draft books in export', 'my-book-showroom' ); ?></th>
			<td>
				<label>
					<input type="checkbox" name="mbs_onix_include_drafts" value="1"
						<?php checked( '1', $include_drafts ); ?>>
					<?php esc_html_e( 'Include books with draft status (exported with PublishingStatus 02 &mdash; Forthcoming)', 'my-book-showroom' ); ?>
				</label>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'ONIX Export Version', 'my-book-showroom' ); ?></th>
			<td>
				<label>
					<input type="radio" name="mbs_onix_version" value="3.0"
						<?php checked( '3.0', $onix_version ); ?>>
					<?php esc_html_e( 'ONIX 3.0 (recommended for most distributors)', 'my-book-showroom' ); ?>
				</label><br>
				<label>
					<input type="radio" name="mbs_onix_version" value="2.1"
						<?php checked( '2.1', $onix_version ); ?>>
					<?php esc_html_e( 'ONIX 2.1 (for Ingram, Baker &amp; Taylor legacy feeds)', 'my-book-showroom' ); ?>
				</label><br>
				<label>
					<input type="radio" name="mbs_onix_version" value="3.1"
						<?php checked( '3.1', $onix_version ); ?>>
					<?php esc_html_e( 'ONIX 3.1 (latest standard &mdash; for distributors requiring ONIX 3.1)', 'my-book-showroom' ); ?>
				</label>
				<p class="description"><?php esc_html_e( 'Version applies to export only. Import auto-detects 2.1, 3.0, or 3.1 from the uploaded file.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
	</table>

	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'Copyright Page Defaults', 'my-book-showroom' ); ?></h2>
	<p class="description"><?php esc_html_e( 'These values pre-fill the Copyright Page tab (Tab 7) of the Book Bible. Each book can override the disclaimer in the Publisher &amp; Imprint sidebar. Leave a field blank to omit it from the export.', 'my-book-showroom' ); ?></p>
	<table class="form-table">
		<tr>
			<th scope="row"><label for="mbs_pub_publisher_address"><?php esc_html_e( 'Publisher Address', 'my-book-showroom' ); ?></label></th>
			<td>
				<textarea id="mbs_pub_publisher_address" name="mbs_pub_publisher_address"
					rows="3" class="large-text"><?php echo esc_textarea( $pub_address ); ?></textarea>
				<p class="description"><?php esc_html_e( 'Mailing address printed on the copyright page (e.g. street, city, state, zip).', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="mbs_pub_country_of_printing"><?php esc_html_e( 'Country of Printing', 'my-book-showroom' ); ?></label></th>
			<td>
				<input type="text" id="mbs_pub_country_of_printing" name="mbs_pub_country_of_printing"
					class="regular-text" value="<?php echo esc_attr( $pub_country ); ?>">
				<p class="description"><?php esc_html_e( 'Printed as &ldquo;Printed in [country]&rdquo;. Defaults to United States of America.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
		<tr>
			<th scope="row"><label for="mbs_pub_copyright_disclaimer"><?php esc_html_e( 'Copyright Disclaimer', 'my-book-showroom' ); ?></label></th>
			<td>
				<textarea id="mbs_pub_copyright_disclaimer" name="mbs_pub_copyright_disclaimer"
					rows="4" class="large-text"><?php echo esc_textarea( $pub_copyright_disclaimer ); ?></textarea>
				<p class="description"><?php esc_html_e( 'Standard disclaimer block (rights reserved, permissions, contact). Each book can override this in the Publisher &amp; Imprint metabox.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
	</table>

	<p class="submit">
		<?php submit_button( null, 'primary', 'mbs_onix_settings_save_btn', false ); ?>
	</p>
	</form>

	<?php /* ---- Section B: Export ---- */ ?>
	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'Export', 'my-book-showroom' ); ?></h2>
	<p><?php esc_html_e( 'Generate an ONIX short-tag XML file (in the version selected above) from your entire book catalog. One record is emitted per ISBN. Multi-format books include RelatedProduct cross-links for distributor record linking.', 'my-book-showroom' ); ?></p>
	<form method="post" action="">
		<?php wp_nonce_field( 'mbs_onix_export', 'mbs_onix_export_nonce' ); ?>
		<input type="hidden" name="mbs_onix_export_submit" value="1">
		<p class="submit">
			<button type="submit" class="button button-primary">
				<?php
				printf(
					/* translators: %s: ONIX version number (e.g. 3.0 or 2.1) */
					esc_html__( 'Export ONIX %s XML', 'my-book-showroom' ),
					esc_html( $onix_version )
				);
				?>
			</button>
		</p>
	</form>

	<?php /* ---- Section C: Import ---- */ ?>
	<h2 class="mbs-settings-section-heading"><?php esc_html_e( 'Import', 'my-book-showroom' ); ?></h2>
	<p><?php esc_html_e( 'Import an ONIX 3.1, 3.0, or 2.1 XML file. Short and reference tags are both accepted. Version is auto-detected from the file. Records are matched to existing books by ISBN, then by title and author name. Unmatched records are created as drafts.', 'my-book-showroom' ); ?></p>
	<form method="post" action="" enctype="multipart/form-data">
		<?php wp_nonce_field( 'mbs_onix_import', 'mbs_onix_import_nonce' ); ?>
		<input type="hidden" name="mbs_onix_import_submit" value="1">
		<table class="form-table">
			<tr>
				<th scope="row"><label for="mbs_onix_file"><?php esc_html_e( 'ONIX XML File', 'my-book-showroom' ); ?></label></th>
				<td>
					<input type="file" id="mbs_onix_file" name="mbs_onix_file" accept=".xml">
					<p class="description"><?php esc_html_e( 'Maximum file size: 10 MB. UTF-8 or ISO 8859-1 encoding accepted.', 'my-book-showroom' ); ?></p>
				</td>
			</tr>
		</table>
		<p class="submit">
			<button type="submit" class="button button-primary">
				<?php esc_html_e( 'Import ONIX XML', 'my-book-showroom' ); ?>
			</button>
		</p>
	</form>

</div>
