<?php
/**
 * Acceso embebido al panel público.
 *
 * @package Logistica
 *
 * @var string $context login|denied|reset
 */

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

$context   = isset( $context ) ? sanitize_key( (string) $context ) : 'login';
$is_denied = 'denied' === $context;
$is_reset  = 'reset' === $context;
$reset_key = isset( $_GET['key'] ) ? sanitize_text_field( wp_unslash( (string) $_GET['key'] ) ) : '';
$reset_login = isset( $_GET['login'] ) ? sanitize_text_field( wp_unslash( (string) $_GET['login'] ) ) : '';
$reset_valid = $is_reset && $reset_key && $reset_login && Logistica_Panel_Auth::validate_reset_token( $reset_key, $reset_login );
?>
<div class="logistica-admin logistica-frontend">
	<div class="logistica-panel-auth"<?php echo ( $is_denied || $is_reset ) ? '' : ' data-logistica-panel-login'; ?><?php echo $is_reset ? ' data-logistica-panel-reset' : ''; ?>>
		<div class="logistica-panel-auth__card">
			<p class="logistica-panel-auth__brand"><?php esc_html_e( 'JB Planeación', 'logistica' ); ?></p>

			<?php if ( $is_denied ) : ?>
				<h1 class="logistica-panel-auth__title"><?php esc_html_e( 'Sin permisos', 'logistica' ); ?></h1>
				<p class="logistica-panel-auth__intro"><?php esc_html_e( 'Tu usuario no tiene permisos para acceder a JB Planeación.', 'logistica' ); ?></p>
				<button type="button" class="logistica-header__logout logistica-header__logout--card" data-logistica-panel-logout>
					<span class="dashicons dashicons-migrate" aria-hidden="true"></span>
					<?php esc_html_e( 'Cerrar sesión', 'logistica' ); ?>
				</button>

			<?php elseif ( $is_reset ) : ?>
				<h1 class="logistica-panel-auth__title"><?php esc_html_e( 'Nueva contraseña', 'logistica' ); ?></h1>

				<?php if ( ! $reset_valid ) : ?>
					<p class="logistica-panel-auth__intro"><?php esc_html_e( 'El enlace de recuperación expiró o no es válido.', 'logistica' ); ?></p>
					<a class="logistica-panel-auth__submit logistica-panel-auth__submit--link" href="<?php echo esc_url( home_url( '/' ) ); ?>">
						<?php esc_html_e( 'Volver al acceso', 'logistica' ); ?>
					</a>
				<?php else : ?>
					<p class="logistica-panel-auth__intro">
						<?php
						printf(
							/* translators: %s: user login */
							esc_html__( 'Crea una nueva contraseña para %s.', 'logistica' ),
							esc_html( $reset_login )
						);
						?>
					</p>

					<div class="logistica-panel-auth__notice" data-logistica-panel-auth-notice hidden role="alert"></div>

					<form class="logistica-panel-auth__form" data-logistica-panel-reset-form novalidate>
						<input type="hidden" name="key" value="<?php echo esc_attr( $reset_key ); ?>">
						<input type="hidden" name="login" value="<?php echo esc_attr( $reset_login ); ?>">

						<label class="logistica-panel-auth__field">
							<span class="logistica-panel-auth__label"><?php esc_html_e( 'Nueva contraseña', 'logistica' ); ?></span>
							<input class="logistica-panel-auth__input" type="password" name="password" autocomplete="new-password" minlength="8" required>
						</label>

						<label class="logistica-panel-auth__field">
							<span class="logistica-panel-auth__label"><?php esc_html_e( 'Confirmar contraseña', 'logistica' ); ?></span>
							<input class="logistica-panel-auth__input" type="password" name="password_confirm" autocomplete="new-password" minlength="8" required>
						</label>

						<button type="submit" class="logistica-panel-auth__submit">
							<?php esc_html_e( 'Guardar contraseña', 'logistica' ); ?>
						</button>
					</form>
				<?php endif; ?>

			<?php else : ?>
				<div data-logistica-panel-view="login">
					<h1 class="logistica-panel-auth__title"><?php esc_html_e( 'Acceso', 'logistica' ); ?></h1>
					<p class="logistica-panel-auth__intro"><?php esc_html_e( 'Inicia sesión para ver el panel de inventarios.', 'logistica' ); ?></p>

					<div class="logistica-panel-auth__notice" data-logistica-panel-auth-notice hidden role="alert"></div>

					<form class="logistica-panel-auth__form" data-logistica-panel-auth-form novalidate>
						<label class="logistica-panel-auth__field">
							<span class="logistica-panel-auth__label"><?php esc_html_e( 'Usuario o correo', 'logistica' ); ?></span>
							<input class="logistica-panel-auth__input" type="text" name="username" autocomplete="username" required>
						</label>

						<label class="logistica-panel-auth__field">
							<span class="logistica-panel-auth__label"><?php esc_html_e( 'Contraseña', 'logistica' ); ?></span>
							<input class="logistica-panel-auth__input" type="password" name="password" autocomplete="current-password" required>
						</label>

						<div class="logistica-panel-auth__row">
							<label class="logistica-panel-auth__remember">
								<input type="checkbox" name="remember" value="1">
								<span><?php esc_html_e( 'Recordarme', 'logistica' ); ?></span>
							</label>
							<button type="button" class="logistica-panel-auth__forgot" data-logistica-panel-show-forgot>
								<?php esc_html_e( '¿Olvidaste tu contraseña?', 'logistica' ); ?>
							</button>
						</div>

						<button type="submit" class="logistica-panel-auth__submit">
							<?php esc_html_e( 'Iniciar sesión', 'logistica' ); ?>
						</button>
					</form>
				</div>

				<div data-logistica-panel-view="forgot" hidden>
					<h1 class="logistica-panel-auth__title"><?php esc_html_e( 'Recuperar acceso', 'logistica' ); ?></h1>
					<p class="logistica-panel-auth__intro"><?php esc_html_e( 'Indica tu usuario o correo y te enviaremos un enlace para restablecer la contraseña.', 'logistica' ); ?></p>

					<div class="logistica-panel-auth__notice" data-logistica-panel-forgot-notice hidden role="alert"></div>
					<div class="logistica-panel-auth__success" data-logistica-panel-forgot-success hidden></div>

					<form class="logistica-panel-auth__form" data-logistica-panel-forgot-form novalidate>
						<label class="logistica-panel-auth__field">
							<span class="logistica-panel-auth__label"><?php esc_html_e( 'Usuario o correo', 'logistica' ); ?></span>
							<input class="logistica-panel-auth__input" type="text" name="identity" autocomplete="username" required>
						</label>

						<button type="submit" class="logistica-panel-auth__submit">
							<?php esc_html_e( 'Enviar enlace', 'logistica' ); ?>
						</button>
						<button type="button" class="logistica-panel-auth__submit logistica-panel-auth__submit--ghost" data-logistica-panel-show-login>
							<?php esc_html_e( 'Volver al acceso', 'logistica' ); ?>
						</button>
					</form>
				</div>
			<?php endif; ?>
		</div>
	</div>
</div>
