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

if(isset($_POST['forgetPassword']) && isset($_POST['mail']) && $_POST['mail']){
    $confirmMessages = isset($_POST['mail']) ? $user->forgotPassword($_POST['mail']) : 'ERROR';
}

?>

<div class="row">
    <div class="wiaas-title col-sm-12">
        <h1>{{ 'login.headers.TITLE' | translate }}</h1>
    </div>
</div>
<div class="row">
    <div class="col-sm-4 col-sm-offset-4">
        <form id="forget-passwd-form" action="" method="post" class="form-signin">
            <h2 class="form-signin-heading">{{ 'login.headers.FORGET_PASSWORD_TITLE' | translate }}</h2>
            <label for="mail" class="sr-only">{{ 'login.headers.MAIL' | translate }}</label>
            <input id="forgotten-mail" name="mail" type="text" class="form-control" placeholder="{{ 'login.headers.MAIL' | translate }}" required autofocus />
            <button id="login-button" type="submit" name="forgetPassword" class="btn btn-lg btn-primary btn-block">{{ 'login.buttons.SEND_RECOVER_MAIL' | translate }}</button>
        </form>
        <?php
            if(isset($confirmMessages)) {
                foreach ($confirmMessages as $key => $confirmMessage) {
                    $translatedMessage = '{{"login.messages.'.$confirmMessage.'" | translate}}';
                    $alertClass = 'alert-danger';
                    $iconClass = 'glyphicon-alert';

                    if($confirmMessage === 'SIGN_IN') {
                        $alertClass = 'alert-success';
                        $iconClass = 'glyphicon-ok';
                        $translatedMessage = '{{"login.messages.MAIL_SENT" | translate}}';
                    } else if($confirmMessage === 'NO_USER' || $confirmMessage === 'CHANGE_LATER') {
                        $alertClass = 'alert-warning';
                    }
                    $translatedMessage .= ' <a href="login">{{"login.messages.SIGN_IN" | translate}}</a>';

                    echo '<div id="confirmation-message" class="alert '.$alertClass.'">';
                        echo 'User '. ($key+1). ' : ';
                        echo '<span class="glyphicon '.$iconClass.'"></span> '.$translatedMessage;
                    echo '</div>';
                }
            }
        ?>

    </div>
</div>
