<?php
/**
 * Bloque de configuración de tamaño de rótulo de rollos (ancho × largo mm).
 *
 * Expects $save_action and $redirect_base in scope.
 * En el backend WP: 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_Rotulo_Rollos_Print::get_settings();

if ( ! isset( $save_action ) ) {
	$save_action = admin_url( 'admin-post.php' );
}

if ( ! isset( $redirect_base ) ) {
	$redirect_base = admin_url();
}

$label_size_badge = sprintf(
	/* translators: 1: width mm, 2: height mm */
	__( '%1$d × %2$d mm', 'logistica' ),
	(int) $print_settings[ Logistica_Rotulo_Rollos_Print::OPTION_WIDTH_MM ],
	(int) $print_settings[ Logistica_Rotulo_Rollos_Print::OPTION_HEIGHT_MM ]
);
// UI editable en wp-admin / panel ?config=1; el permiso de guardado se valida aparte al guardar.
$can_edit_settings = function_exists( 'logistica_is_rotulo_rollos_settings_view' )
	? logistica_is_rotulo_rollos_settings_view()
	: ( function_exists( 'logistica_can_edit_rotulo_rollos_settings' ) ? logistica_can_edit_rotulo_rollos_settings() : is_admin() );
?>
<div class="logistica-rotulo-rollos-print-settings<?php echo $can_edit_settings ? '' : ' logistica-label-print-settings--readonly'; ?>" data-rotulo-print-settings-root>
	<?php if ( $can_edit_settings ) : ?>
	<form class="logistica-rotulo-rollos-print-settings__form" method="post" action="<?php echo esc_url( $save_action ); ?>" data-rotulo-print-settings-form novalidate>
		<input type="hidden" name="action" value="logistica_save_rotulo_rollos_print_settings">
		<?php wp_nonce_field( 'logistica_save_rotulo_rollos_print_settings' ); ?>
		<input type="hidden" name="redirect_to" value="<?php echo esc_url( $redirect_base ); ?>">
	<?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 del rótulo', 'logistica' ); ?></strong>
				<?php if ( $can_edit_settings ) : ?>
					<p><?php esc_html_e( 'Diseño del rótulo (mm). Al imprimir se usa hoja Carta vertical: 2 media cartas por página. Márgenes «Ninguno», sin encabezados/pies y con «Gráficos de fondo».', 'logistica' ); ?></p>
				<?php else : ?>
					<p><?php esc_html_e( 'Impresión en Carta vertical: 2 rótulos (media carta) 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="60" max="250" step="1" name="rotulo_rollos_print[<?php echo esc_attr( Logistica_Rotulo_Rollos_Print::OPTION_WIDTH_MM ); ?>]" value="<?php echo esc_attr( (string) (int) $print_settings[ Logistica_Rotulo_Rollos_Print::OPTION_WIDTH_MM ] ); ?>" data-rotulo-print-width required>
				</label>
				<label><?php esc_html_e( 'Largo (mm)', 'logistica' ); ?>
					<input type="number" min="40" max="200" step="1" name="rotulo_rollos_print[<?php echo esc_attr( Logistica_Rotulo_Rollos_Print::OPTION_HEIGHT_MM ); ?>]" value="<?php echo esc_attr( (string) (int) $print_settings[ Logistica_Rotulo_Rollos_Print::OPTION_HEIGHT_MM ] ); ?>" data-rotulo-print-height required>
				</label>
			<?php endif; ?>
			<span class="logistica-rotulo-rollos-print-settings__badge" data-rotulo-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 ) : ?>
			<p class="logistica-rotulo-rollos-print-settings__status" data-rotulo-print-settings-status role="status" aria-live="polite"></p>
		<?php endif; ?>
	<?php if ( $can_edit_settings ) : ?>
	</form>
	<?php else : ?>
	</div>
	<?php endif; ?>
</div>
