<?php
if (!defined('ABSPATH')) {
    exit;
}
//This file is loaded when the user clicks on the password reset link in the email to arrive at the password reset page.
//The following query args are expected: key=XYZ&login=ABC
$error_message  = '';
$disable        = false;
$user_login     = '';

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$key = isset($_GET['key']) ? sanitize_text_field( wp_unslash( $_GET['key'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$user_login = isset($_GET['login']) ? sanitize_text_field( wp_unslash( $_GET['login'] ) ) : '';

if ($key && $user_login) {
    $is_valid_key = check_password_reset_key($key, $user_login);
    if (is_wp_error($is_valid_key)) {
        $error_message = '<div class="chama-alert chama-alert-error">';
        $error_message = $error_message . __("Error! The password reset key is either invalid or expired. Please generate a new password reset request.", "chama");
        $error_message = $error_message . '</div>';
        $disable       = true;
    }

} else {
    $disable = true;
}

?>
<div class="chama-account-page-wrapper">
    <div class="chama-form-wrapper">
    <div class="chama-form-header">&#x1F512; Reset your password</div>
    <div class="chama-form-content">

	<?php
// show any error messages after form submission
chama_show_error_messages();
echo wp_kses($error_message, chama_allowed_html());
?>
        <?php 
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if (isset($_GET['password-reset']) && $_GET['password-reset'] == 'true') {
		?>
			<div class="chama-alert chama-alert-success">
				<span><?php esc_html_e('Your password has been successfully updated.', 'chama');?></span>
			</div>
		<?php
		$disable = true;
	 }?>
    <form id="chama-reset-password-form" class="chama-form" action="" method="POST">



	<p>
					<label for="password"><?php esc_html_e('Password', 'chama');?></label>
					<input name="chama_password" id="password" class="required" type="password" required data-msg="Please enter your password" <?php echo esc_attr($disable ? 'disabled' : '' ); ?> />
				</p>
				<p>
					<label for="password_again"><?php esc_html_e('Password Again', 'chama');?></label>
					<input name="chama_password_confirm" id="password_again" class="required" type="password" required data-msg="Please enter your password again" data-msg-equalto="Passwords must match!" <?php echo esc_attr($disable ? 'disabled' : '' ); ?> />
				</p>
				<p class="chama-hint">Hint: The password should be at least twelve characters long. To make it stronger, use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ &amp; ).</p>
				<p>
				<span id="password-strength"></span>
				</p>
                <input type="hidden" name="chama_action" value="reset-password"/>
                <input type="hidden" name="chama_user_login" value="<?php echo esc_attr($user_login); ?>"/>
					<input type="hidden" name="chama_password_reset_nonce" value="<?php echo esc_attr(wp_create_nonce('chama-password-reset-nonce')); ?>"/>
					<input type="submit" class="chama-account-submit <?php echo esc_attr($disable ? 'chama-disabled-button' : 'chama-button' ); ?>" value="<?php esc_attr_e('Save Password', 'chama');?>" <?php echo esc_attr($disable ? 'disabled' : '' ); ?>  />
				</p>

		</form>

		</div>

</div>
</div>