<?php
    include("config.php");

    session_start();
    $profpic = "background.jpg";
    if($_SERVER["REQUEST_METHOD"] == "POST") {
        // username and password sent from form

        $myusername = mysqli_real_escape_string($db,$_POST['username']);
        $mypassword = mysqli_real_escape_string($db,$_POST['password']);
		$options = [
			'cost' => 11,
			'salt' => '#a$f^f&dlksjf0dlkjas213k',
		];
		$mypassword = password_hash($mypassword, PASSWORD_BCRYPT, $options);

        $sql = "select u.id, ur.role
                from user u left join user_role ur on ur.id = u.role
                where u.username = '$myusername' and u.password = '$mypassword' and u.active = '1'";

        $result = mysqli_query($db,$sql);
        $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
        $active = $row['active'];

        $count = mysqli_num_rows($result);

        // If result matched $myusername and $mypassword, table row must be 1 row

        if($count == 1) {
            $_SESSION['login_user'] = $myusername;
                   //$customerfirstname = $row['firstname'];
            $userrole = $row['role'];
            if($userrole == "Admin"){
                header("location: admin/home.php");
            }elseif ($userrole == "System Admin"){
                header("location: sa/home.php");
            }elseif ($userrole == "Branch Manager"){
                header("location: bm/home.php");
            }elseif ($userrole == "Regional Manager"){
                header("location: rm/home.php");
            }else {
                header("location: t/home.php");
            }
            /*
            if($userrole == "Admin"){
                header("location: admin/welcome.php");
            }elseif ($userrole == "System Admin"){
                header("location: sa/welcome.php");
            }elseif ($userrole == "Branch Manager"){
                header("location: bm/welcome.php");
            }elseif ($userrole == "Regional Manager"){
                header("location: rm/welcome.php");
            }else {
                header("location: t/welcome.php");
            }
              */
        }else {
            $error = "Your Login Name or Password is invalid";
        }
    }
?>
<html>
    <head>
        <title>Login</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <style>
            .jumbotron {
                position:fixed;
top: 50%;
left: 50%;
margin-top: -9em; /*set to a negative number 1/2 of your height*/
margin-left: -15em; /*set to a negative number 1/2 of your width*/
border: 1px solid #ccc;
background-color: white;
            }
            body {
<!--background-image: url('<?php echo $profpic;?>');-->
}
        </style>
    </head>
    <body>
        <form action="" method="post">
            <div class="content">
                <div class="container">
                   <div class="row">
                        <div class="col-md-4 col-sm-6 col-md-offset-4 col-sm-offset-3" style="margin-top:10%;">
                            <div class="card card-login card-hidden">
                                <h1 class="category text-center" style="margin-top:20px;">
                                    BRAINSCIENCE
                                </h1>
                            </div>
                            <div class="card-content">
                                <div class="form-group">
                                    <label for="usr">Username:</label>
                                    <input type = "text" name = "username" class = "form-control" id="usr" autofocus required/>
                                </div>
                                <div class="form-group">
                                    <label for="pwd">Password  :</label>
                                        <input type = "password" name = "password" class = "form-control" id="pwd" required/>
                                </div>
                            </div>
							<div class="footer text-center">
                                <button type="submit" value="Submit" class="btn btn-info btn-wd btn-lg" style="width: 100%;">Login</button>
								<div style = "font-size:15px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </body>
</html>
