<?php session_start(); ?>
<?php ob_start();?>
<?php include "includes/header.php";?>
<!-- <?php include"function.php";?> -->
<style>
.bottom-btn{
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.act-btn{
    display: flex;
    align-items: center;
}
.act-btn a{
    /*width: 300px;*/
    position: relative;
    text-decoration: none;
    padding: 5px;
    outline: none;
    border:1px solid #D1E034;
    border-radius: 5px;
    color: #D1E034;
    font-size: 15px;
    font-weight: normal;
    font-family: sans-serif;
    background: transparent;
}
.act-btn a:hover{
    color: #fff;
    font-weight: bold;
}
.act-btn i{
    font-size: 13px;
    background: #D1E034;
    color: #fff;
    padding: 5px;
    border-radius: 50%;
}
.keepMe{
    display: flex;
    align-items: center; 
    gap:10px;
}
.keepMe label{
    font-size: 15px;
    font-family: sans-serif;
    cursor: pointer;
}
.keepMe input{
    width: 15px;
    height: 15px;
}
</style>
<div id="notification-containers" class="notification-containers"></div>
<div class="container-capture">
    <div class="login-page">
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="cube"></div>
        <div class="login-bg">
            <form action="" method="POST">
                <div class="login-head">
                    <img src="logo/goodness.jpg">
                    <h5 style="font-size: 16px;">
                        <i class="fa fa-user-graduate text-primary"></i>
                        Sign in to student portal
                    </h5>
                    <hr>
                </div>
                <div class="form-body-login">
                    <div class="single-field">
                        <h4>Admin. Number</h4>
                        <input type="text" placeholder="E.g: DEGA/25/001" class="" name="adminNo" autocomplete="off">
                    </div>
                    <div class="single-field">
                        <h4>Password</h4>
                        <input type="password" placeholder="Your Password" class="" name="password">
                    </div>
                    <div class="single-field">
                        <button class="info" name="login-btn">Login</button>
                    </div>
                    <div class="bottom-btn">
                        <div class="act-btn">
                            <a href="../index.php" class="btn primary mt-2">
                            <i class="fa fa-arrow-left"></i> Back home</a>
                        </div>
                        <div class="keepMe">
                            <label for="loginRem">Remember me</label>
                            <input type="checkbox" name="remember_me" id="loginRem" onclick="checkAlert()">
                            <?php include'includes/login-alert.php';?>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


<?php
if (isset($_POST['login-btn'])) {

    $adminNo    = $_POST['adminNo'];
    $password   = $_POST['password'];

    if (!empty($adminNo) && ($password)) {

        $getData = "SELECT surname, adminNo, password FROM student WHERE adminNo = ?";
        $stmt = $pdo->prepare($getData);
        $stmt->execute([$adminNo]);
        if ($stmt->rowCount() == true) {
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            $rowPassword    = $row['password'];
            $surname        = $row['surname'];
            $adminNo        = $row['adminNo']; 

            if (password_verify($password, $rowPassword)) {
                $_SESSION['message'] = "Password is correct";
                $_SESSION['type'] = "success";
                
                $_SESSION['adminNo'] = $adminNo;

                // Handle remember me
                if (isset($_POST['remember_me'])) {
                    
                    if (function_exists('random_bytes')) {
                        $token = bin2hex(random_bytes(32));
                    } else {
                        $token = bin2hex(openssl_random_pseudo_bytes(32));
                    }

                    $update = $pdo->prepare("UPDATE student SET remember_token = ? WHERE adminNo = ?");
                    $update->execute([$token, $adminNo]);
                }
                header("Location:index.php");
            }else{
                $_SESSION['message'] = "Incorrect details";
                $_SESSION['type'] = "error";
            }

        }else{
            // Username is correct but password is not
            $_SESSION['message'] = "Password is not correct";
            $_SESSION['type'] = "error";
        }
        
    }else{
        $_SESSION['message'] = "Invalid details";
        $_SESSION['type'] = "error";
    }
}

?>


<?php include "includes/footer.php";?>
<?php include'includes/notification-alert-js.php';?>