<?php 
 ob_start();
 session_start();
include('dbconnect.php');

function loginLogs($conn, $user){
    $time_stamp=date('Y-m-d H:i');
    $conn->query("INSERT INTO `login_logs`(`empid`, `time_stamp`) VALUES ('$user','$time_stamp') ");
}

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

    $uname = trim($_POST['username']);
    $uname = strip_tags($uname);
    $uname = htmlspecialchars($uname);
    $uname = mysqli_real_escape_string($conn, $uname);
  
  
    $psw = trim($_POST['password']);
    $psw = strip_tags($psw);
    $psw = htmlspecialchars($psw);
    $psw = mysqli_real_escape_string($conn, $psw);
  
    $password = hash('sha256', $psw); // password hashing using SHA256
  
    $sql = "SELECT * FROM account WHERE userid='$uname' AND status='active'";
    $result = mysqli_query($conn, $sql);
    $count = mysqli_num_rows($result);
    $row = mysqli_fetch_assoc($result);


    if( $count == 1 && $row['password']==$password && $row['level'] == 'super admin') {
        $_SESSION['user'] = $row['userid'];
        $_SESSION['userlevel'] = $row['level'];
        loginLogs($conn, $uname);
        header("Location: super admin/superaddashboard.php");
    } else if( $count == 1 && $row['password']==$password && $row['level'] == 'admin'){
        $_SESSION['user'] = $row['userid'];
        $_SESSION['userlevel'] = $row['level'];
        loginLogs($conn, $uname);
        header("Location: admin/dashboard.php");
    
    } else if( $count == 1 && $row['password']==$password && $row['level'] == 'user'){
        $_SESSION['user'] = $row['userid'];
        $_SESSION['userlevel'] = $row['level'];
        loginLogs($conn, $uname);
        header("Location: user/dashboard.php");
    
    } else if( $count == 1 && $row['password']==$password && $row['level'] == 'team leader'){
        $_SESSION['user'] = $row['userid'];
        $_SESSION['userlevel'] = $row['level'];
        loginLogs($conn, $uname);
        header("Location: team leader/dashboard.php");
    
    } else if( $count == 1 && $row['password']==$password && $row['level'] == 'Senior Manager'){
        $_SESSION['user'] = $row['userid'];
        $_SESSION['userlevel'] = $row['level'];
        loginLogs($conn, $uname);
        header("Location: srm/dashboard.php");
    
    } else if( $count == 1 && $row['password']==$password && ($row['level'] == 'Senior Human Resource' || $row['level'] == 'Manager' )){
        $_SESSION['user'] = $row['userid'];
        $_SESSION['userlevel'] = $row['level'];
        loginLogs($conn, $uname);
        header("Location: shr/dashboard.php");
    
    } else if( $count == 1 && $row['password']==$password && ($row['level'] == 'Tech' || $row['level'] == 'Manager' )){
        $_SESSION['user'] = $row['userid'];
        $_SESSION['userlevel'] = $row['level'];
        loginLogs($conn, $uname);
        header("Location: tech/dashboard.php");
    
    } else{
        $alert = '<div class="alert alert-danger">
        <strong>Incorrect Login Credentials </strong>
        </div>';
    }
}

if( isset($_SESSION['user']) ) {
    $user = $_SESSION['user'];
    $userlevel = $_SESSION['userlevel'];
    if($userlevel == 'super admin'){
        header("Location: super admin/superaddashboard.php");
    } else if($userlevel == 'admin'){
        header("Location: admin/dashboard.php");
    } else if($userlevel == 'user'){
        header("Location: user/dashboard.php");
    } else if($userlevel == 'team leader'){
        header("Location: team leader/dashboard.php");
    } else if($userlevel == 'Senior Manager'){
        header("Location: srm/dashboard.php");
    } else if($userlevel == 'Senior Human Resource' || $userlevel == 'Manager'  ){
        header("Location: shr/dashboard.php");
    }
    
    exit;
 }

?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <meta name="mobile-web-app-capable" content="yes">

    <title>ATV Login</title>

    <!-- Bootstrap core CSS-->
    <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">

    <!-- Custom styles for this template-->
    <link href="css/sb-admin.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link rel="icon" href="images/logo.jpg" type="image/gif" sizes="16x16">


</head>

<body>
    <div class="bg">
        <div class="container">
            <div class="card card-login mx-auto mt-5">
                <div class="card-header">Login</div>
                <div class="card-body">
                    <?php echo $alert ?>
                    <div class="text-center">
                        <img src="images/logo.jpg" class="rounded" alt="..." style="height: 170px; width: 370px;">

                    </div>
                    <br />



                    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">

                        <div class="form-group">
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <span class="input-group-text" id="basic-addon1"><i class="	fas fa-fw fa-user"></i></span>
                                </div>
                                <input type="text" class="form-control" name="username" placeholder="Username"
                                    aria-label="Username" aria-describedby="basic-addon1" required>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <span class="input-group-text" id="basic-addon1"><i class="	fas fa-fw fa-lock"></i></span>
                                </div>
                                <input type="password" class="form-control" name="password" placeholder="Password"
                                    aria-label="Password" aria-describedby="basic-addon1" required>
                            </div>
                        </div>

                        <button name="login" class="btn btn-primary btn-block" href="index.html"><i class="fa fa-sign-in-alt">
                            </i> Login </button>
                    </form>

                </div>
            </div>
        </div>

        <!-- Bootstrap core JavaScript-->
        <script src="vendor/jquery/jquery.min.js"></script>
        <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

        <!-- Core plugin JavaScript-->
        <script src="vendor/jquery-easing/jquery.easing.min.js"></script>
    </div>
        <script language="javascript" type="text/javascript">
            $(document).ready(function(){
            $("form").on("submit", function(){
                $("#pageloader").fadeIn();
            });//submit

             $('#preloader').fadeOut();
            });//document ready



        </script>
</body>

</html>