<?php
/**
 * Configuración: fórmulas de conversión (Calculadoras).
 *
 * @package Logistica
 */

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

if ( ! class_exists( 'Logistica_Roles', false ) || ! Logistica_Roles::user_can_manage_config() ) {
	return;
}

$formula_settings = Logistica_Conversions::get_formula_settings();
?>
<?php if ( isset( $_GET['logistica_status'] ) && 'saved' === $_GET['logistica_status'] ) : ?>
	<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Configuración guardada correctamente.', 'logistica' ); ?></p></div>
<?php endif; ?>

<section class="logistica-panel">
	<header class="logistica-page-toolbar__intro">
		<h2><?php esc_html_e( 'Calculadoras', 'logistica' ); ?></h2>
		<p class="description"><?php esc_html_e( 'Fórmulas base del motor de calculadoras. En el panel operativo solo se usan los campos de trabajo.', 'logistica' ); ?></p>
	</header>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<input type="hidden" name="action" value="logistica_save_formula_settings">
		<?php wp_nonce_field( 'logistica_save_formula_settings' ); ?>
		<div class="logistica-formula-list">
			<?php foreach ( $formula_settings as $key => $formula ) : ?>
				<div class="logistica-formula-card">
					<h3><?php echo esc_html( $formula['label'] ); ?></h3>
					<label>
						<?php esc_html_e( 'Fórmula', 'logistica' ); ?>
						<input type="text" name="formulas[<?php echo esc_attr( $key ); ?>][formula]" value="<?php echo esc_attr( $formula['formula'] ); ?>">
					</label>
					<label>
						<?php esc_html_e( 'Nota de uso', 'logistica' ); ?>
						<textarea name="formulas[<?php echo esc_attr( $key ); ?>][notes]" rows="2"><?php echo esc_textarea( $formula['notes'] ); ?></textarea>
					</label>
				</div>
			<?php endforeach; ?>
		</div>
		<p><button class="button button-primary" type="submit"><?php esc_html_e( 'Guardar fórmulas', 'logistica' ); ?></button></p>
	</form>
</section>
