<?php
/**
 * Bloque de configuración de tamaño de etiqueta de producción (ancho × largo mm).
 *
 * Expects $print_settings in scope (Logistica_Label_Print::get_settings()).
 * En el backend: formulario editable. En el frontend: solo lectura.
 *
 * @package Logistica
 */

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

$print_settings   = isset( $print_settings ) && is_array( $print_settings )
	? $print_settings
	: Logistica_Label_Print::get_settings();
$label_size_badge = sprintf(
	/* translators: 1: width mm, 2: height mm */
	__( '%1$d × %2$d mm', 'logistica' ),
	(int) $print_settings[ Logistica_Label_Print::OPTION_WIDTH_MM ],
	(int) $print_settings[ Logistica_Label_Print::OPTION_HEIGHT_MM ]
);
$can_edit_settings = function_exists( 'logistica_can_show_etiqueta_settings_editor' )
	? logistica_can_show_etiqueta_settings_editor()
	: ( logistica_is_etiqueta_settings_view() && logistica_can_edit_etiqueta_settings() );
?>
<div class="logistica-rotulo-rollos-print-settings logistica-label-print-settings<?php echo $can_edit_settings ? '' : ' logistica-label-print-settings--readonly'; ?>">
	<?php if ( $can_edit_settings ) : ?>
	<form class="logistica-rotulo-rollos-print-settings__form" method="post" action="#" data-label-print-settings-form novalidate>
	<?php else : ?>
	<div class="logistica-rotulo-rollos-print-settings__form logistica-rotulo-rollos-print-settings__form--readonly">
	<?php endif; ?>
		<div class="logistica-rotulo-rollos-print-settings__intro">
			<span class="logistica-rotulo-rollos-print-settings__icon" aria-hidden="true">
				<span class="dashicons dashicons-image-crop"></span>
			</span>
			<div class="logistica-rotulo-rollos-print-settings__copy">
				<strong><?php esc_html_e( 'Tamaño de la etiqueta', 'logistica' ); ?></strong>
				<?php if ( $can_edit_settings ) : ?>
					<p><?php esc_html_e( 'Ancho y largo en milímetros. Cada etiqueta se imprime en ese tamaño (una por hoja). Usa márgenes «Ninguno», sin encabezados/pies y activa «Gráficos de fondo».', 'logistica' ); ?></p>
				<?php else : ?>
					<p><?php esc_html_e( 'Cada etiqueta se imprime en el tamaño configurado (una por hoja). Usa márgenes «Ninguno», sin encabezados/pies y activa «Gráficos de fondo».', 'logistica' ); ?></p>
				<?php endif; ?>
			</div>
		</div>
		<div class="logistica-rotulo-rollos-print-settings__fields">
			<?php if ( $can_edit_settings ) : ?>
				<label><?php esc_html_e( 'Ancho (mm)', 'logistica' ); ?>
					<input type="number" min="50" max="200" step="1" name="label_print[<?php echo esc_attr( Logistica_Label_Print::OPTION_WIDTH_MM ); ?>]" value="<?php echo esc_attr( (string) (int) $print_settings[ Logistica_Label_Print::OPTION_WIDTH_MM ] ); ?>" data-label-print-width required>
				</label>
				<label><?php esc_html_e( 'Largo (mm)', 'logistica' ); ?>
					<input type="number" min="50" max="250" step="1" name="label_print[<?php echo esc_attr( Logistica_Label_Print::OPTION_HEIGHT_MM ); ?>]" value="<?php echo esc_attr( (string) (int) $print_settings[ Logistica_Label_Print::OPTION_HEIGHT_MM ] ); ?>" data-label-print-height required>
				</label>
			<?php endif; ?>
			<span class="logistica-rotulo-rollos-print-settings__badge" data-label-print-size-badge aria-live="polite">
				<?php echo esc_html( $label_size_badge ); ?>
			</span>
			<?php if ( $can_edit_settings ) : ?>
				<button type="submit" class="button button-primary"><?php esc_html_e( 'Guardar tamaño', 'logistica' ); ?></button>
			<?php endif; ?>
		</div>
	<?php if ( $can_edit_settings ) : ?>
	</form>
	<form class="logistica-label-print-settings__options" method="post" action="#" data-label-print-options-form novalidate>
		<input type="hidden" name="label_print_admin_options" value="1">
		<input type="hidden" name="label_print_save_mode" value="options">
		<label class="logistica-label-print-modal__check">
			<input type="hidden" name="label_print[<?php echo esc_attr( Logistica_Label_Print::OPTION_ALLOW_BLANK_CAJA_NUM ); ?>]" value="0">
			<input
				type="checkbox"
				name="label_print[<?php echo esc_attr( Logistica_Label_Print::OPTION_ALLOW_BLANK_CAJA_NUM ); ?>]"
				value="1"
				data-label-print-allow-blank-caja
				<?php checked( ! empty( $print_settings[ Logistica_Label_Print::OPTION_ALLOW_BLANK_CAJA_NUM ] ) ); ?>
			>
			<?php esc_html_e( 'Permitir caja inicial en blanco (numeración manual)', 'logistica' ); ?>
		</label>
		<p class="logistica-label-print-settings__hint"><?php esc_html_e( 'Si está activo, al imprimir puedes dejar vacío «Caja inicial Nº» para escribir el número a mano en la etiqueta.', 'logistica' ); ?></p>
		<div class="logistica-label-print-settings__options-actions">
			<button type="submit" class="button button-primary"><?php esc_html_e( 'Guardar opción', 'logistica' ); ?></button>
			<p class="logistica-label-print-settings__options-status" data-label-print-options-status role="status" aria-live="polite"></p>
		</div>
	</form>
	<?php else : ?>
	</div>
	<?php endif; ?>
</div>
