<?php
/**
 * Log in.
 *
 * @package AutoOrderAccount
 */

require __DIR__ . '/_app.php';

if ( current_user() ) {
	go( '/account/' );
}

$err = '';
if ( 'POST' === ( $_SERVER['REQUEST_METHOD'] ?? '' ) ) {
	csrf_check();
	$email = strtolower( trim( $_POST['email'] ?? '' ) );
	$pw    = (string) ( $_POST['password'] ?? '' );
	$acc   = $email ? account_get( $email ) : null;
	if ( $acc && password_verify( $pw, $acc['password_hash'] ) ) {
		login_user( $acc['email'] );
		account_touch_login( $acc['email'] );
		go( '/account/' );
	}
	usleep( 350000 );
	$err = 'Incorrect email or password.';
}

head( 'Log in' );
?>
<div class="card narrow">
	<h1>Log in</h1>
	<p class="sub">Manage your Auto Order by Demfilz Pro subscription.</p>
	<?php if ( $err ) : ?><div class="err"><?php echo e( $err ); ?></div><?php endif; ?>
	<form method="post" novalidate>
		<?php echo csrf_field(); ?>
		<label>Email<input type="email" name="email" required autocomplete="email" value="<?php echo e( $_POST['email'] ?? '' ); ?>"></label>
		<label>Password<input type="password" name="password" required autocomplete="current-password"></label>
		<button class="btn" type="submit">Log in</button>
	</form>
	<p class="links"><a href="/account/forgot">Forgot password?</a> &nbsp;·&nbsp; <a href="/account/register">Create an account</a></p>
</div>
<?php
foot();
