<?php
if(!defined('APPLICATION_VERSION')){
    die("Permission denied!");
}

if(isset($_POST['login'])){
    $loginMessage = $user->login($_POST['username'], $_POST['password']);

    if($loginMessage['status'] === 'success'){
        header('Location:dashboards');
    }
}
$message = (isset($_REQUEST['message'])) ? $_REQUEST['message'] : '';
?>

<div class="row">
    <div class="wiaas-title col-sm-12">
        <h1><?php echo APPLICATION_NAME;?> {{ 'login.headers.TITLE' | translate }}</h1>
    </div>
</div>
<div class="row">
    <div class="col-sm-4 col-sm-offset-4">
        <form id="login-form" action="" method="post" class="form-signin">
            <h2 class="form-signin-heading">{{ 'login.headers.SMALL_TITLE' | translate }}</h2>
            <label for="username" class="sr-only">{{ 'login.headers.USERNAME' | translate }}</label>
            <input id="username" name="username" type="text" id="username" class="form-control" placeholder="{{ 'login.headers.USERNAME' | translate }}" required autofocus>
            <label for="inputPassword" class="sr-only">{{ 'login.headers.PASSWORD' | translate }}</label>
            <input id="password" name="password" type="password" id="inputPassword" class="form-control" placeholder="{{ 'login.headers.PASSWORD' | translate }}">
            <button id="login-button" name="login" class="btn btn-lg btn-primary btn-block" type="submit">{{ 'login.buttons.SIGN_IN' | translate }}</button>
        </form>
        <?php
            if(isset($loginMessage['errorMessage'])){
                echo '<div id="login-message" class="alert alert-warning">';
                    echo '<span class="glyphicon glyphicon-warning-sign"></span> {{"login.messages.'.$loginMessage['errorMessage'].'" | translate}}';
                    if($loginMessage['errorMessage'] === 'INVALID_USERNAME_PASSWORD') {
                        echo " <a href='?generate=newToken'>{{'login.headers.FORGET_PASSWORD' | translate}}</a>";
                    }
                echo '</div>';
            }

            if($message){
                echo '<div id="login-message" class="alert alert-success">';
                    echo '<span class="glyphicon glyphicon-ok"></span> {{"validation.messages.'.$message.'" | translate}}';
                echo '</div>';
            }
        ?>
    </div>
</div>

<span id="application-version-login" style="display:none;"><?php echo APPLICATION_VERSION; ?></span>
