<?php
/**
 * Frontend login form partial.
 *
 * @package GuardDog
 * @since 1.9.39
 *
 * @var array    $atts
 * @var bool     $is_logged_in
 * @var string   $action_url
 * @var string   $redirect_to
 * @var string   $continue_url
 * @var WP_User  $current_user
 * @var string   $login_value
 * @var string[] $error_messages
 * @var string   $inline_style
 * @var string   $passkey_markup
 * @var string   $social_login_markup
 * @var string   $captcha_markup
 * @var string   $lost_password_url
 */

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

$display_name = ( $current_user instanceof WP_User ) ? $current_user->display_name : '';
?>

<div
	class="guard-dog-login-form"
	data-style="<?php echo esc_attr( $atts['style'] ); ?>"
	<?php if ( '' !== $inline_style ) : ?>
		style="<?php echo esc_attr( $inline_style ); ?>"
	<?php endif; ?>
>
	<?php if ( ! empty( $error_messages ) ) : ?>
		<div class="guard-dog-login-form__messages guard-dog-login-form__messages--error" role="alert" aria-live="polite">
			<?php foreach ( $error_messages as $message ) : ?>
				<p class="guard-dog-login-form__message"><?php echo esc_html( $message ); ?></p>
			<?php endforeach; ?>
		</div>
	<?php endif; ?>

	<?php if ( $is_logged_in ) : ?>
		<div class="guard-dog-login-form__state guard-dog-login-form__state--logged-in">
			<p class="guard-dog-login-form__state-text">
				<?php
				if ( '' !== $display_name ) {
					echo esc_html(
						sprintf(
							/* translators: %s: current user's display name. */
							__( 'You are already logged in as %s.', 'guard-dog' ),
							$display_name
						)
					);
				} else {
					esc_html_e( 'You are already logged in.', 'guard-dog' );
				}
				?>
			</p>

			<?php if ( '' !== $continue_url ) : ?>
				<p class="guard-dog-login-form__actions">
					<a class="guard-dog-login-form__link" href="<?php echo esc_url( $continue_url ); ?>">
						<?php esc_html_e( 'Continue', 'guard-dog' ); ?>
					</a>
				</p>
			<?php endif; ?>
		</div>
	<?php else : ?>
		<?php if ( '' !== $passkey_markup || '' !== $social_login_markup ) : ?>
			<div class="guard-dog-login-form__alternate">
				<?php
				if ( '' !== $passkey_markup ) {
					// Passkey markup is generated from a trusted plugin partial.
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					echo $passkey_markup;
				}
				?>

				<?php
				if ( '' !== $social_login_markup ) {
					// Social login markup is generated from a trusted plugin partial.
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					echo $social_login_markup;
				}
				?>
			</div>

			<div class="guard-dog-login-form__separator" aria-hidden="true">
				<span><?php esc_html_e( 'Or sign in with your password', 'guard-dog' ); ?></span>
			</div>
		<?php endif; ?>

		<form class="guard-dog-login-form__form" method="post" action="<?php echo esc_url( $action_url ); ?>">
			<div class="guard-dog-login-form__field">
				<label class="guard-dog-login-form__label" for="user_login">
					<?php esc_html_e( 'Username or Email Address', 'guard-dog' ); ?>
				</label>
				<input
					class="guard-dog-login-form__input"
					type="text"
					name="log"
					id="user_login"
					value="<?php echo esc_attr( $login_value ); ?>"
					autocomplete="username"
					required
				/>
			</div>

			<div class="guard-dog-login-form__field">
				<label class="guard-dog-login-form__label" for="user_pass">
					<?php esc_html_e( 'Password', 'guard-dog' ); ?>
				</label>
				<input
					class="guard-dog-login-form__input"
					type="password"
					name="pwd"
					id="user_pass"
					autocomplete="current-password"
					required
				/>
			</div>

			<?php if ( $atts['show_remember_me'] ) : ?>
				<div class="guard-dog-login-form__remember">
					<label class="guard-dog-login-form__checkbox-label" for="rememberme">
						<input type="checkbox" name="rememberme" id="rememberme" value="forever" />
						<span><?php esc_html_e( 'Remember me', 'guard-dog' ); ?></span>
					</label>
				</div>
			<?php endif; ?>

			<?php
			if ( '' !== $captcha_markup ) {
				// Plugin-generated CAPTCHA markup contains the login nonce and provider widget markup.
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				echo $captcha_markup;
			}
			?>

			<div class="guard-dog-login-form__actions">
				<button class="guard-dog-login-form__submit" type="submit">
					<?php esc_html_e( 'Log In', 'guard-dog' ); ?>
				</button>

				<?php if ( '' !== $lost_password_url ) : ?>
					<a class="guard-dog-login-form__link" href="<?php echo esc_url( $lost_password_url ); ?>">
						<?php esc_html_e( 'Lost your password?', 'guard-dog' ); ?>
					</a>
				<?php endif; ?>
			</div>

			<input type="hidden" name="<?php echo esc_attr( Guard_Dog_Login_Form::SUBMIT_ACTION ); ?>" value="1" />
			<input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
			<?php wp_nonce_field( Guard_Dog_Login_Form::NONCE_ACTION, 'guard_dog_login_form_nonce' ); ?>
		</form>
	<?php endif; ?>
</div>
