<?php
/**
 * Login / lost password view — shortcode [avision_custom_login].
 *
 * Variables: $avision_auth_login_error, $avision_auth_lost_error, $avision_auth_lost_success, $avision_auth_show_lost, $avision_auth_login_url, $avision_auth_reset_page_url.
 *
 * @package Avision
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="avision-auth avision-auth--login">
	<a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="avision-auth__logo-link" style="margin-bottom: 2rem; display: inline-block;">
		<img src="<?php echo esc_url( 'https://avision.org/wp-content/uploads/2026/03/avision-logo-png-70x70-1.png' ); ?>" alt="<?php echo esc_attr__( 'Avision', 'avision-dashboard' ); ?>" width="70" height="70" style="object-fit: contain;">
	</a>
	<div class="avision-auth__card">
		<?php if ( ! empty( $avision_auth_show_lost ) ) : ?>
			<a class="avision-auth__back" href="<?php echo esc_url( $avision_auth_login_url ); ?>"><?php echo esc_html__( '← Back to log in', 'avision-dashboard' ); ?></a>
			<h2 class="avision-auth__title"><?php echo esc_html__( 'Forgot password', 'avision-dashboard' ); ?></h2>
			<p class="avision-auth__intro"><?php echo esc_html__( 'Enter your account email address. You will receive a link to set a new password.', 'avision-dashboard' ); ?></p>
			<?php if ( ! empty( $avision_auth_lost_error ) ) : ?>
				<div class="avision-auth__notice avision-auth__notice--error" role="alert"><?php echo esc_html( $avision_auth_lost_error ); ?></div>
			<?php endif; ?>
			<?php if ( ! empty( $avision_auth_lost_success ) ) : ?>
				<div class="avision-auth__notice avision-auth__notice--success" role="status"><?php echo esc_html( $avision_auth_lost_success ); ?></div>
			<?php endif; ?>
			<form method="post" action="<?php echo esc_url( home_url( '/login/?action=lostpassword' ) ); ?>" class="avision-auth__form">
				<?php wp_nonce_field( 'avision_lost_password', 'avision_lost_password_nonce' ); ?>
				<div class="avision-auth__field">
					<label for="avision_user_login"><?php echo esc_html__( 'Email or username', 'avision-dashboard' ); ?></label>
					<input type="text" name="user_login" id="avision_user_login" required autocomplete="username" value="<?php echo isset( $_POST['user_login'] ) ? esc_attr( sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) ) : ''; ?>">
				</div>
				<button type="submit" name="avision_do_lost_password" class="avision-auth__submit" value="1"><?php echo esc_html__( 'Send reset link', 'avision-dashboard' ); ?></button>
			</form>
		<?php else : ?>
			<h2 class="avision-auth__title"><?php echo esc_html__( 'Log in', 'avision-dashboard' ); ?></h2>
			<?php if ( ! empty( $avision_auth_login_error ) ) : ?>
				<div class="avision-auth__notice avision-auth__notice--error" role="alert"><?php echo esc_html( $avision_auth_login_error ); ?></div>
			<?php endif; ?>
			<form method="post" action="<?php echo esc_url( $avision_auth_login_url ); ?>" class="avision-auth__form">
				<?php wp_nonce_field( 'avision_custom_login', 'avision_login_nonce' ); ?>
				<input type="hidden" name="avision_login_submit" value="1">
				<?php
				$avision_auth_redirect_to = '';
				if ( ! empty( $avision_auth_flash_redirect_to ) && function_exists( 'wp_validate_redirect' ) && function_exists( 'esc_url_raw' ) ) {
					$avision_auth_redirect_to = wp_validate_redirect( esc_url_raw( (string) $avision_auth_flash_redirect_to ), '' );
				}
				if ( $avision_auth_redirect_to === '' && ! empty( $_GET['redirect_to'] ) && function_exists( 'wp_validate_redirect' ) ) {
					$avision_auth_redirect_to = wp_validate_redirect( esc_url_raw( wp_unslash( (string) $_GET['redirect_to'] ) ), '' );
				}
				if ( $avision_auth_redirect_to !== '' ) :
					?>
					<input type="hidden" name="redirect_to" value="<?php echo esc_url( $avision_auth_redirect_to ); ?>">
				<?php endif; ?>
				<?php
				$avision_log_value = '';
				if ( isset( $avision_auth_prefill_log ) && $avision_auth_prefill_log !== '' ) {
					$avision_log_value = $avision_auth_prefill_log;
				} elseif ( isset( $_POST['log'] ) ) {
					$avision_log_value = sanitize_text_field( wp_unslash( (string) $_POST['log'] ) );
				}
				?>
				<div class="avision-auth__field">
					<label for="avision_log"><?php echo esc_html__( 'Email or username', 'avision-dashboard' ); ?></label>
					<input type="text" name="log" id="avision_log" required autocomplete="username" value="<?php echo esc_attr( $avision_log_value ); ?>">
				</div>
				<div class="avision-auth__field">
					<label for="avision_pwd"><?php echo esc_html__( 'Password', 'avision-dashboard' ); ?></label>
					<div class="avision-auth__password-wrap">
						<input class="avision-auth__pw-input" type="password" name="pwd" id="avision_pwd" required autocomplete="current-password">
						<button type="button" class="avision-auth__pw-toggle avision-auth__password-toggle" aria-label="<?php echo esc_attr__( 'Show password', 'avision-dashboard' ); ?>" aria-pressed="false" data-label-show="<?php echo esc_attr__( 'Show password', 'avision-dashboard' ); ?>" data-label-hide="<?php echo esc_attr__( 'Hide password', 'avision-dashboard' ); ?>"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M17.94 17.94A10.07 10.07 0 0112 20c-7 0-11-8-11-8a18.45 18.45 0 015.06-5.94M9.9 4.24A9.12 9.12 0 0112 4c7 0 11 8 11 8a18.5 18.5 0 01-2.16 3.19m-6.72-1.07a3 3 0 11-4.24-4.24"/><line x1="1" y1="1" x2="23" y2="23"/></svg></button>
					</div>
				</div>
				<label class="avision-auth__remember">
					<input type="checkbox" name="rememberme" value="forever">
					<?php echo esc_html__( 'Remember me', 'avision-dashboard' ); ?>
				</label>
				<button type="submit" name="avision_do_login" class="avision-auth__submit" value="1"><?php echo esc_html__( 'Log in', 'avision-dashboard' ); ?></button>
			</form>
			<div class="avision-auth__footer">
				<a href="<?php echo esc_url( home_url( '/login/?action=lostpassword' ) ); ?>"><?php echo esc_html__( 'Forgot password?', 'avision-dashboard' ); ?></a>
			</div>
		<?php endif; ?>
	</div>
</div>
<script>
(function () {
	var svgEyeHidden = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M17.94 17.94A10.07 10.07 0 0112 20c-7 0-11-8-11-8a18.45 18.45 0 015.06-5.94M9.9 4.24A9.12 9.12 0 0112 4c7 0 11 8 11 8a18.5 18.5 0 01-2.16 3.19m-6.72-1.07a3 3 0 11-4.24-4.24"/><line x1="1" y1="1" x2="23" y2="23"/></svg>';
	var svgEyeVisible = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>';
	function avisionAuthSyncPwToggle(btn, input) {
		var hidden = input.getAttribute('type') === 'password';
		btn.innerHTML = hidden ? svgEyeHidden : svgEyeVisible;
		btn.setAttribute('aria-pressed', hidden ? 'false' : 'true');
		var showLbl = btn.getAttribute('data-label-show') || 'Show password';
		var hideLbl = btn.getAttribute('data-label-hide') || 'Hide password';
		btn.setAttribute('aria-label', hidden ? showLbl : hideLbl);
	}
	document.querySelectorAll('.avision-auth__password-wrap').forEach(function (wrap) {
		var btn = wrap.querySelector('.avision-auth__pw-toggle');
		var input = wrap.querySelector('input.avision-auth__pw-input');
		if (!btn || !input) {
			return;
		}
		avisionAuthSyncPwToggle(btn, input);
		btn.addEventListener('click', function () {
			var isPwd = input.getAttribute('type') === 'password';
			input.setAttribute('type', isPwd ? 'text' : 'password');
			avisionAuthSyncPwToggle(btn, input);
		});
	});
})();
</script>
