<?php
/**
 * HVAC Community Events: Custom Login Form Template
 *
 * This template provides the structure for the custom login page,
 * integrating with the Astra theme's styling.
 *
 * @version 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

// Get Astra theme layout settings if needed, e.g., container width
// $container_class = astra_get_content_layout(); // Example

?>

<div class="hvac-community-login-wrapper"> <?php // Custom wrapper for potential styling ?>
	<div class="ast-container"> <?php // Astra theme container ?>
		<div class="hvac-login-form-card"> <?php // Card styling based on design reference ?>

			<div class="hvac-login-form-header">
				<h2><?php esc_html_e('Trainer Login', 'hvac-community-events'); ?></h2>
				<p><?php esc_html_e('Sign in to access your trainer dashboard', 'hvac-community-events'); ?></p>
			</div>

			<?php
			// Display login errors if any
			if ( isset( $_GET['login'] ) && $_GET['login'] === 'failed' ) {
			    echo '<div class="login-error">Invalid username or password. Please try again.</div>';
			}
			if ( isset( $_GET['loggedout'] ) && $_GET['loggedout'] === 'true' ) {
			    echo '<div class="login-success">You have been successfully logged out.</div>';
			}
			?>

			<?php
			// Custom login form with password show/hide toggle
			$redirect_to = isset($_REQUEST['redirect_to']) ? esc_url($_REQUEST['redirect_to']) : '';
			?>
			<form name="hvac_community_loginform" id="hvac_community_loginform" action="<?php echo esc_url(site_url('wp-login.php', 'login_post')); ?>" method="post" class="hvac-login-form">
				
				<div class="hvac-login-form-group">
					<label for="user_login" class="hvac-login-form-label">
						<?php esc_html_e('Username or Email Address', 'hvac-community-events'); ?>
					</label>
					<div class="hvac-input-group">
						<input type="text" name="log" id="user_login" class="hvac-login-form-input" value="" size="20" autocapitalize="off" autocomplete="username" required />
						<span class="hvac-input-icon">👤</span>
					</div>
				</div>

				<div class="hvac-login-form-group">
					<label for="user_pass" class="hvac-login-form-label">
						<?php esc_html_e('Password', 'hvac-community-events'); ?>
					</label>
					<div class="hvac-input-group hvac-password-group">
						<input type="password" name="pwd" id="user_pass" class="hvac-login-form-input hvac-password-input" value="" size="20" autocomplete="current-password" required />
						<span class="hvac-input-icon">🔒</span>
						<button type="button" class="hvac-password-toggle" aria-label="<?php esc_attr_e('Show password', 'hvac-community-events'); ?>">
							<span class="hvac-password-toggle-icon">👁️</span>
						</button>
					</div>
				</div>

				<div class="hvac-remember-group">
					<input name="rememberme" type="checkbox" id="rememberme" value="forever" class="hvac-remember-checkbox" />
					<label for="rememberme" class="hvac-remember-label">
						<?php esc_html_e('Remember Me', 'hvac-community-events'); ?>
					</label>
				</div>

				<?php if (!empty($redirect_to)): ?>
					<input type="hidden" name="redirect_to" value="<?php echo esc_attr($redirect_to); ?>" />
				<?php endif; ?>
				
				<!-- Add hidden field to identify this as coming from custom login page -->
				<input type="hidden" name="hvac_custom_login" value="1" />

				<?php wp_nonce_field('hvac_login', 'hvac_login_nonce'); ?>

				<button type="submit" name="wp-submit" id="wp-submit" class="hvac-login-submit">
					<span class="hvac-login-submit-text"><?php esc_html_e('Log In', 'hvac-community-events'); ?></span>
					<span class="hvac-login-spinner" style="display: none;"></span>
				</button>

			</form>

			<div class="hvac-login-links">
				<?php if ( get_option( 'users_can_register' ) ) : ?>
					<a class="hvac-register-link" href="<?php echo esc_url( home_url( '/trainer/registration/' ) ); ?>">
						<?php esc_html_e( 'Register', 'hvac-community-events' ); ?>
					</a> |
				<?php endif; ?>
				<a class="hvac-lostpassword-link" href="<?php echo esc_url( wp_lostpassword_url() ); ?>">
					<?php esc_html_e( 'Lost your password?', 'hvac-community-events' ); // Task 2.4 ?>
				</a>
			</div>

		</div> <?php // .hvac-login-form-card ?>
	</div> <?php // .ast-container ?>
</div> <?php // .hvac-community-login-wrapper ?>