<?php
ob_start();
include('includes/db_conx.php');
$title = 'Secure Login';
include('includes/header.php');



if (isset($_POST['submit'])) {

    // getting all the values from the ajax field

    $username = $db_conx->real_escape_string(trim($_POST['username']));

    $pwrd = $db_conx->real_escape_string(trim($_POST['pwrd']));

    $sql_admin = "SELECT * FROM admin_area";

    $query_admin = $db_conx->query($sql_admin);

    // getting the id from the table

    $row_admin = mysqli_fetch_assoc($query_admin);
    $admin_uname = $row_admin['username'];
    $admin_pwrd = $row_admin['password'];



    // checking if the user is an administrator



    if ($username == $admin_uname && $pwrd == $admin_pwrd) {

        echo 'Nice try';
    } else {

        $sql_log = "SELECT * FROM confidential_table WHERE pwrd = '$pwrd' && username = '$username'";

        $query_log = $db_conx->query($sql_log);

        // getting the id from the table

        $row = mysqli_fetch_assoc($query_log);



        $user_id = $row['id'];
        $two_fa_status = $row['two_fa_status'];

        if (!$row) {

            //  echo ' <div class="alert alert-danger" role="alert">
            //           Not valid account number or password or Account is not Active. Please try again or contact to support
            //         </div>  ';

            echo "
                            <script> 
                            $(document).ready(function () {
                                Swal.fire({
                                icon: 'error',
                                title: 'ERROR!!!',
                                text: 'Invalid Login',
                                footer: '<a href=../index.php>Click here to return to start all over </a>'
                                })
                            });

                            </script>

                            ";
        } else {



            $sql_time = "UPDATE confidential_table SET last_login = '$current_time', ip_address = '$ip' WHERE id = '$user_id'";



            $query_time = $db_conx->query($sql_time);

            $sql_per = "UPDATE personal_information SET online = 1 WHERE id = '$user_id'";
            $query_per = $db_conx->query($sql_per);

            if ($query_time) {



                //getting the user ip from the global variable 



                $_SESSION['detect_login'] = $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR'];



                //getting the informations and saving them in the session variable from the databases



                $_SESSION['id'] = $row['id'];
                $_SESSION['account_no'] = $row['account_no'];



                //redirecting the page to its destination

                if ($two_fa_status == 1) {
                    header('location: twofa.php');
                } else {
                    header('location: user/dashboard.php');
                }
            } else {

                echo "error";
            } // end the last if statement 

        }
    }
}

?>



<body>



    <div class="page-wrapper">

        <?php
        include('includes/nav.php');
        ?>

        <div class="content-wrapper">

            <div class="breadcrumb-wrap bg-spring">
                <img src="assets/img/breadcrumb/br-shape-1.png" alt="Image" class="br-shape-one xs-none">
                <img src="assets/img/breadcrumb/br-shape-2.png" alt="Image" class="br-shape-two xs-none">
                <img src="assets/img/breadcrumb/br-shape-3.png" alt="Image" class="br-shape-three moveHorizontal sm-none">
                <img src="assets/img/breadcrumb/br-shape-4.png" alt="Image" class="br-shape-four moveVertical sm-none">
                <div class="container">
                    <div class="row align-items-center">
                        <div class="col-lg-7 col-md-8 col-sm-8">
                            <div class="breadcrumb-title">
                                <h2>Login</h2>
                                <ul class="breadcrumb-menu list-style">
                                    <li><a href="index">Home </a></li>
                                    <li>Login</li>
                                </ul>
                            </div>
                        </div>
                        <div class="col-lg-5 col-md-4 col-sm-4 xs-none">
                            <div class="breadcrumb-img">
                                <img src="assets/img/breadcrumb/br-shape-5.png" alt="Image" class="br-shape-five animationFramesTwo">
                                <img src="assets/img/breadcrumb/br-shape-6.png" alt="Image" class="br-shape-six bounce">
                                <img src="assets/img/breadcrumb/breadcrumb-3.png" alt="Image">
                            </div>
                        </div>
                    </div>
                </div>
            </div>


            <section class="Login-wrap ptb-100">
                <div class="container">
                    <div class="row">
                        <div class="col-xl-6 offset-xl-3 col-lg-8 offset-lg-2 col-md-10 offset-md-1">
                            <div class="login-form-wrap">
                                <div class="login-header">
                                    <h3>Login Here</h3>
                                    <p>Welcome Back!! Login To Your Account</p>
                                </div>
                                <div class="login-form">
                                    <div class="login-body">
                                        <form class="form-wrap" method="POST">
                                            <div class="row">
                                                <div class="col-lg-12">
                                                    <div class="form-group">
                                                        <input id="text" name="username" type="text" placeholder="Username" required>
                                                    </div>
                                                </div>
                                                <div class="col-lg-12">
                                                    <div class="form-group">
                                                        <input id="pwd" name="pwrd" type="password" placeholder="Password">
                                                    </div>
                                                </div>
                                                <div class="col-lg-6 col-md-6 col-sm-6 col-6">
                                                    <div class="checkbox style3">
                                                        <input type="checkbox" id="test_1">
                                                        <label for="test_1">
                                                            Remember Me</a>
                                                        </label>
                                                    </div>
                                                </div>
                                                <div class="col-lg-6 col-md-6 col-sm-6 col-6 text-end mb-20">
                                                    <a href="#" class="link style1">Forgot Password?</a>
                                                </div>
                                                <div class="col-lg-12">
                                                    <div class="form-group">
                                                        <button class="btn style1 " type='submit' name='submit'>
                                                            Login Now
                                                        </button>
                                                    </div>
                                                </div>
                                                <div class="col-md-12 text-center">
                                                    <p class="mb-0">Don’t Have an Account? <a class="link style1" href="register.php">Create One</a></p>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

        </div>


        <?php
        include('includes/footer.php');
        ?>
    </div>



</body>
<?php

ob_end_flush();
?>

</html>