<?php
/**
 * Alta de usuario JB (standalone).
 *
 * @package Logistica
 */

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

require_once LOGISTICA_PLUGIN_DIR . 'admin/views/partials.php';

if ( ! is_user_logged_in() || ! Logistica_Roles::user_can_manage_config() ) {
	echo '<div class="wrap logistica-admin"><p>' . esc_html__( 'No tienes permisos para crear usuarios.', 'logistica' ) . '</p></div>';
	return;
}

$return_section = logistica_config_return_section();
$config_tab_url = logistica_config_section_url( $return_section );
$error          = isset( $_GET['logistica_error'] ) ? sanitize_key( wp_unslash( (string) $_GET['logistica_error'] ) ) : '';
$new_user       = new WP_User();
$error_messages = array(
	'login_required'    => __( 'El nombre de usuario es obligatorio.', 'logistica' ),
	'email_invalid'     => __( 'Introduce un correo electrónico válido.', 'logistica' ),
	'password_short'    => __( 'La contraseña debe tener al menos 8 caracteres.', 'logistica' ),
	'password_mismatch' => __( 'Las contraseñas no coinciden.', 'logistica' ),
	'login_exists'      => __( 'Ese nombre de usuario ya está en uso.', 'logistica' ),
	'email_exists'      => __( 'Ese correo electrónico ya está registrado.', 'logistica' ),
	'create_failed'     => __( 'No se pudo crear el usuario. Inténtalo de nuevo.', 'logistica' ),
);

$posted_login = isset( $_POST['user_login'] ) ? sanitize_user( wp_unslash( (string) $_POST['user_login'] ), true ) : '';
$posted_name  = isset( $_POST['display_name'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['display_name'] ) ) : '';
$posted_email = isset( $_POST['user_email'] ) ? sanitize_email( wp_unslash( (string) $_POST['user_email'] ) ) : '';
?>
<div class="logistica-user-form-page">
	<div class="logistica-user-form">
		<header class="logistica-user-form__hero">
			<a class="logistica-user-form__back" href="<?php echo esc_url( $config_tab_url ); ?>">
				<span class="logistica-user-form__back-icon" aria-hidden="true">
					<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 18l-6-6 6-6"/></svg>
				</span>
				<span class="logistica-user-form__back-label"><?php esc_html_e( 'Volver a configuración', 'logistica' ); ?></span>
			</a>
			<p class="logistica-user-form__brand"><?php esc_html_e( 'JB Planeación', 'logistica' ); ?></p>
			<h1 class="logistica-user-form__title"><?php esc_html_e( 'Nuevo usuario', 'logistica' ); ?></h1>
			<p class="logistica-user-form__intro"><?php esc_html_e( 'Crea una cuenta de acceso al panel operativo o al panel público y asigna su rol JB.', 'logistica' ); ?></p>
		</header>

		<?php if ( '' !== $error && isset( $error_messages[ $error ] ) ) : ?>
			<div class="logistica-user-form__notice logistica-user-form__notice--error" role="alert">
				<span class="dashicons dashicons-warning" aria-hidden="true"></span>
				<?php echo esc_html( $error_messages[ $error ] ); ?>
			</div>
		<?php endif; ?>

		<form class="logistica-user-form__body" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" data-logistica-user-form>
			<input type="hidden" name="action" value="logistica_create_user">
			<input type="hidden" name="logistica_redirect_to" value="<?php echo esc_url( logistica_new_user_url( $return_section ) ); ?>">
			<input type="hidden" name="return_section" value="<?php echo esc_attr( $return_section ); ?>">
			<?php wp_nonce_field( 'logistica_create_user' ); ?>

			<section class="logistica-user-form__section">
				<div class="logistica-user-form__section-head">
					<h2><?php esc_html_e( 'Datos de acceso', 'logistica' ); ?></h2>
					<p><?php esc_html_e( 'Información de inicio de sesión del usuario.', 'logistica' ); ?></p>
				</div>

				<div class="logistica-user-form__fields">
					<label class="logistica-user-form__field">
						<span class="logistica-user-form__label"><?php esc_html_e( 'Usuario', 'logistica' ); ?></span>
						<input class="logistica-user-form__input" type="text" name="user_login" id="user_login" value="<?php echo esc_attr( $posted_login ); ?>" required autocomplete="off" placeholder="<?php esc_attr_e( 'ej. maria.garcia', 'logistica' ); ?>">
						<span class="logistica-user-form__hint"><?php esc_html_e( 'Solo letras, números y guiones bajos.', 'logistica' ); ?></span>
					</label>

					<label class="logistica-user-form__field">
						<span class="logistica-user-form__label"><?php esc_html_e( 'Nombre visible', 'logistica' ); ?></span>
						<input class="logistica-user-form__input" type="text" name="display_name" id="display_name" value="<?php echo esc_attr( $posted_name ); ?>" autocomplete="off" placeholder="<?php esc_attr_e( 'ej. María García', 'logistica' ); ?>">
					</label>

					<label class="logistica-user-form__field logistica-user-form__field--wide">
						<span class="logistica-user-form__label"><?php esc_html_e( 'Correo electrónico', 'logistica' ); ?></span>
						<input class="logistica-user-form__input" type="email" name="user_email" id="user_email" value="<?php echo esc_attr( $posted_email ); ?>" required autocomplete="off" placeholder="<?php esc_attr_e( 'usuario@empresa.com', 'logistica' ); ?>">
					</label>

					<label class="logistica-user-form__field">
						<span class="logistica-user-form__label"><?php esc_html_e( 'Contraseña', 'logistica' ); ?></span>
						<span class="logistica-user-form__password-wrap">
							<input class="logistica-user-form__input" type="password" name="user_pass" id="user_pass" required autocomplete="new-password" minlength="8" data-logistica-password>
							<button type="button" class="logistica-user-form__password-toggle" data-logistica-password-toggle aria-label="<?php esc_attr_e( 'Mostrar contraseña', 'logistica' ); ?>">
								<span class="dashicons dashicons-visibility" aria-hidden="true"></span>
							</button>
						</span>
						<span class="logistica-user-form__hint"><?php esc_html_e( 'Mínimo 8 caracteres.', 'logistica' ); ?></span>
					</label>

					<label class="logistica-user-form__field">
						<span class="logistica-user-form__label"><?php esc_html_e( 'Confirmar contraseña', 'logistica' ); ?></span>
						<span class="logistica-user-form__password-wrap">
							<input class="logistica-user-form__input" type="password" name="user_pass_confirm" id="user_pass_confirm" required autocomplete="new-password" minlength="8" data-logistica-password>
							<button type="button" class="logistica-user-form__password-toggle" data-logistica-password-toggle aria-label="<?php esc_attr_e( 'Mostrar contraseña', 'logistica' ); ?>">
								<span class="dashicons dashicons-visibility" aria-hidden="true"></span>
							</button>
						</span>
					</label>
				</div>
			</section>

			<?php Logistica_Roles::render_user_profile_fields( $new_user, array( 'layout' => 'cards' ) ); ?>

			<footer class="logistica-user-form__actions">
				<div class="logistica-user-form__actions-end">
					<a class="logistica-user-form__btn logistica-user-form__btn--cancel" href="<?php echo esc_url( $config_tab_url ); ?>">
						<?php esc_html_e( 'Cancelar', 'logistica' ); ?>
					</a>
					<button class="logistica-user-form__btn logistica-user-form__btn--submit" type="submit">
						<span class="logistica-user-form__btn-icon" aria-hidden="true">
							<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 5v14"/><path d="M5 12h14"/></svg>
						</span>
						<?php esc_html_e( 'Crear usuario', 'logistica' ); ?>
					</button>
				</div>
			</footer>
		</form>
	</div>
</div>
