<?php
$pageName  = "Login";
include($_SERVER['DOCUMENT_ROOT'] . "/auth/header.php");

if (@$_SESSION['acct_email']) {
    header("Location:./account/dashboard.php");
}


if (isset($_POST['emaillogin'])) {
    $acct_email = inputValidation($_POST['acct_email']);
    $acct_password = inputValidation($_POST['acct_password']);
    $log = "SELECT * FROM users WHERE acct_email =:acct_email";
    $stmt = $conn->prepare($log);
    $stmt->execute([
        'acct_email' => $acct_email
    ]);

    $user = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($stmt->rowCount() === 0) {
        toast_alert("error", "Invalid login details");
    } else {
        $validPassword = password_verify($acct_password, $user['acct_password']);

        if ($validPassword === false) {

            toast_alert("error", "Invalid login details");
        } else {

            if ($user['acct_status'] === 'hold') {
                toast_alert("error", "Account on Hold, Kindly contact support to activate your account");
            } else {

                if (true) {

                    $acct_otp = substr(number_format(time() * rand(), 0, '', ''), 0, 6);

                    $sql =  "UPDATE users SET acct_otp=:acct_otp WHERE acct_email =:acct_email";
                    $stmt = $conn->prepare($sql);
                    $stmt->execute([
                        'acct_otp' => $acct_otp,
                        'acct_email' => $acct_email
                    ]);
                    
                    
                         //IP LOGIN DETAILS
            
            $device = $_SERVER['HTTP_USER_AGENT'];
            $ipAddress = $_SERVER['REMOTE_ADDR'];
            $nowDate = date('Y-m-d H:i:s');
            
            
            $stmt = $conn->prepare("INSERT INTO audit_logs (user_email,device,ipAddress,datenow) VALUES(:user_email,:device,:ipAddress,:datenow)");
            $stmt->execute([
                'user_email'=>$acct_email,
                'device'=>$device,
                'ipAddress'=>$ipAddress,
                'datenow'=>$nowDate
                ]);



                    $full_name = $user['acct_fullname'];
                    $APP_NAME = $page['url_name'];
                    $APP_URL = WEB_URL;
                    $SITE_ADDRESS = $page['url_address'];
                    $APP_NUMBER = $page['url_tel'];
                    $APP_EMAIL = $page['url_email'];
                    $user_email = $user['acct_email'];
                    $message = $sendMail->OTPloginMsg($full_name, $acct_otp, $ipAddress, $APP_NAME, $APP_NUMBER, $APP_EMAIL, $APP_URL, $SITE_ADDRESS);
                    // User Email
                    $subject = "OTP Verification Code";
                    $email_message->send_mail($user_email, $message, $subject);
                    
                    $_SESSION['login'] = $user['acct_email'];
                    header("Location:./pin.php");
                    exit;
                }
            }
        }
    }
}



?>
<div class="container-fluid h-100 awrp anuphan">
    <form class="bg-sm-none px-lg-10 mt-5 p-0 mx-auto shadow-none anuphan post-form mx-auto border-none" method="POST" enctype="multipart/form-data" action="">
        <h3 class="anuphan-3 mt-4">
            Log In
        </h3>
        <div class="form-wrapper">
            <p class="m-0 xsmall grey-color-2 mb-1">Email address</p>
            <input type="email" name="acct_email" placeholder="Enter Email" required>
        </div>
        <p class="m-0 xsmall grey-color-2 mb-1">Password</p>
        <div class="form-wrapper m-0">
            
            <input type="password" name="acct_password" id="id_password" placeholder="********" class="has-icon" autocomplete="new-password" required>
                <span class="input-icon-wrapper pr-3 pointer psw-c-icon" onclick="passwordViewToggle()">
                    <i class="bi bi-eye-slash grey-color p-t-st input-icon"></i>
                </span>
        </div>
        <button type="submit" class="bold-text mt-4 py-3 anuphan-2" name="emaillogin">
            Next
        </button>
        <div class="loader justify-content-center lpb mt-4 w-100 btn-grey add-radius-sm" style="height: 52px; display: none;">
            <span class="bar" style="border-radius: 0px;"></span>
            <span class="bar" style="border-radius: 0px;"></span>
            <span class="bar" style="border-radius: 0px;"></span>
            <span class="bar" style="border-radius: 0px;"></span>
        </div>

        <p class="m-0 mt-4">
            <a href="./reset.php" class="primary-color anuphan-2">Forgot password?</a>
        </p>
        
    </form>
</div>


<?php

include_once($_SERVER['DOCUMENT_ROOT'] . "/auth/footer.php");

?>