<?php
require_once("db/functions.php");
require_once("db/db-const.php");

session_start();
if (logged_in() == true) {
    redirect_to("index.php");
}
?>

<!DOCTYPE html>
<html>

<head>
    <title>Connexion | Apexvet</title>
    <meta charset="utf-8">
    <!-- Google Fonts -->
    <link href='https://fonts.googleapis.com/css?family=Roboto+Slab:400,100,300,700|Lato:400,100,300,700,900' rel='stylesheet' type='text/css'>

    <link rel="stylesheet" href="css/animate.css">
    <!-- Custom Stylesheet -->
    <link rel="stylesheet" href="css/login.css">


    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

    <script src="js/iziToast.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="css/iziToast.min.css">
    <!--//webfonts-->
</head>
<body>

<div class="container">
    <div class="top">
        <h1 id="title" class="hidden"><span id="logo"><span>Apexvet</span></span></h1>
    </div>
        <div class="login-box animated">
            <div class="box-header">
                <h2>Connexion</h2>
            </div>
            <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
                <label for="username">Nom d'utilisateur</label>
                <br/>
                <input type="text" id="username" name="username">
                <br/>
                <label for="password">Mot de passe</label>
                <br/>
                <input type="password" id="password" name="password">
                <br/>
                <input type="checkbox" name="remember" id="remember"/> <label for="remember">Rester connecté</label>
                <br/>
                <button type="submit" name="submit">Connexion</button>
                <br/>
            </form>
        </div>

</div>
</div>
<?php

//Verification du nom d'utilisateur et mot de pass
if (isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    // processing remember me option and setting cookie with long expiry date
    if (isset($_POST['remember'])) {
        session_set_cookie_params('604800'); //one week (value in seconds)
        session_regenerate_id(true);
    }

    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }

    $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1";
    $result = $mysqli->query($sql);

    if ($result->num_rows != 1) {
        redirect_to("login.php?msg=1");
    } else {
        // Authenticated, set session variables
        $user = $result->fetch_array();
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['username'] = $user['username'];

        // update status to online
        $timestamp = time();
        $sql = "UPDATE users SET status={$timestamp} WHERE id={$_SESSION['user_id']}";
        $result = $mysqli->query($sql);

        redirect_to("index.php?msg=1");
        // do stuffs
    }
}
//Fin Verification du nom d'utilisateur et mot de pass

//Affichage mot de passs incorrecte
if (isset($_GET['msg'])) {

    ?>
    <script type='text/javascript'>
        iziToast.error({
            title: 'Erreur',
            message: "Nom d'utilisateur ou mot de passe incorrect",
            position: 'topCenter',
            timeout: 5000,
            transitionOut:'flipOutX'
        });
    </script>
    <?php
}
//Fin Affichage mot de passs incorrecte

?>
</body>

<script>

    $('#username').focus(function() {
        $('label[for="username"]').addClass('selected');
    });
    $('#username').blur(function() {
        $('label[for="username"]').removeClass('selected');
    });
    $('#password').focus(function() {
        $('label[for="password"]').addClass('selected');
    });
    $('#password').blur(function() {
        $('label[for="password"]').removeClass('selected');
    });
</script>

</html>