<?php


/**
 * file: login.php
 *
 * Copyright Miller Electric Mfg. Co. as an unpublished work.
 * All rights reserved.
 *
 * The information contained herein is confidential property of
 * Miller Electric Mfg. Co.  The use of, copying, transfer or
 * disclosure of such information is prohibited except by express
 * written agreement with Miller Electric.
 */


session_start();


require_once('../lib/user_authentication.php');
require_once('../lib/utility.php');


if(!UserAuthentication::isDevicePasswordSet()) {
	navigateToSetPasswordPage();
}


UserAuthentication::logOutUser();


$isFormSubmittedWithError = false;
$errorMessage = '';
$password1 = 'password1';


/**
 * Validates the user input contained in the $_POST super global.
 */
function validatePostedInput() {
	global $errorMessage;
	global $isFormSubmittedWithError;
	global $password1;
	$recordError = true;

	$errorMessage = '';
	$isFormSubmittedWithError = empty($_POST[$password1]);

	if(!$isFormSubmittedWithError) {
		$isFormSubmittedWithError = !UserAuthentication::validatePassword($_POST[$password1]);
	} else if ($recordError) {
		$recordError = false;
		$errorMessage = "Must enter the device's password.";
	}

	if($recordError && $isFormSubmittedWithError) {
		$recordError = false;
		$errorMessage = "Password incorrect.";
	}
}


if(isset($_POST['submit'])) {
	validatePostedInput();
	if(!$isFormSubmittedWithError) {
		if(UserAuthentication::logInUser($_POST[$password1])) {
			navigateTo('/setup/setup.min.php');
		}
	}
}


?>


<html lang="en">
    <?php require('../setup/head.php'); ?>
    <body>
        <div class="container">
            <div class="panel panel-primary">
				<div class="panel-body">
					<div class="panel panel-primary">
						<div class="panel-heading">
							<h3 id="login_header" class="panel-title"><span class="glyphicon glyphicon-log-in"></span> Login</h3>
						</div>
						<div class="panel-body">
							<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" id="form">
								<div>
									<label for="<?php echo $password1; ?>">Enter Password</label>
								</div>
								<div>
									<input id="<?php echo $password1; ?>" name="<?php echo $password1; ?>" type="password" autocomplete="current-password" required>
								</div>
								<ul class="pager">
									<li class="edit">
										<a href="#" onclick="document.getElementById('submit').click();">
											Login
										</a>
									</li>
								</ul>
								<input type="submit" name="submit" id="submit" hidden />
							</form>
							<div>
								<?php
									if(isset($_POST['submit'])) {
										if($isFormSubmittedWithError) {
											echo '<script type="text/javascript">alert("' . $errorMessage . '")</script>';
										}
									}
								?>
							</div>
						</div>
                    </div>
                </div>
				<?php require('../setup/footer.php'); ?>
            </div>
        </div>
    </body>
    <?php require('../setup/js_footer.php'); ?>
</html>