<?php
require_once "common/header.php";
require_once "include/db_handler.php";

$db = new DbHandler();

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

    $email = $_POST['user_email'];
    $pass = $_POST['user_pass'];
    //$userPassword = md5($pass);
    $userPassword = md5($_POST['user_pass']);

    if ($_POST['user_email'] != '' && $_POST['user_pass'] != '') {

        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

            if ($response = $db->isUserExists($email, $userPassword)) {

                if ($response["error"] == false) {

                    // set session and redirect to the dashboard page
                    $_SESSION['email'] = $email;
                    $userID = $_SESSION['email'];
                    $user = $db->getUserByUserID($userID);

                    ?>
                    <script>
                        window.location = "dashboard.php";
                    </script>
                    <?php
                }

            } else {
                $response["message"] = '<div class="alert alert-danger" style="text-align:center;">
                    <a href="login.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Sorry!</strong> Incorrect email and password combination. 
                                
                  </div>';
            }
        } else {

            $response["message"] = '<div class="alert alert-danger" style="text-align:center;">
                    <a href="login.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Sorry!</strong> Enter a valid email address. 
                                
                  </div>';
        }
    } else {
        $response["message"] = '<div class="alert alert-danger" style="text-align:center;">
                    <a href="login.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Sorry!</strong> You need to specify your email and password. 
                                
                  </div>';
    }
}

?>

<body>
<div class="container-fluid">
    <div class="row">
        <div class="right-column sisu">
            <div class="row mx-0">
                <div class="col-md-3">

                </div>
                <div class="col-md-5 bg-white mt-5 mb-5">
                    <a class="signin-logo d-sm-none d-md-block mt-5 mb-5" href="#">
                        <h2>WOMAN KENYA</h2>
                    </a>
                    <?php if (isset($response['message'])) echo $response["message"]; ?>
                    <form action="login.php" method="post">
                        <div class="form-group mt-3">
                            <label for="exampleInputEmail1">Email address</label>
                            <input type="email" name="user_email" class="form-control" id="exampleInputEmail1"
                                   aria-describedby="emailHelp" placeholder="Enter email">
                        </div>
                        <div class="form-group mt-5">
                            <label for="exampleInputPassword1">Password</label>
                            <input type="password" name="user_pass" class="form-control" id="exampleInputPassword1"
                                   placeholder="Password">
                        </div>
                        <div class="custom-control custom-checkbox mb-3">
                            <input type="checkbox" class="custom-control-input" id="keep-signed-in">
                            <label class="custom-control-label" for="keep-signed-in">Keep Me Signed In</label>
                        </div>
                        <button type="submit" name="btn_login" class="btn btn-primary btn-gradient btn-block mt-5">
                            <i class="batch-icon batch-icon-key"></i>
                            Sign In
                        </button>
                        <hr>
                        <p class="text-center">
                            Forgot Password? <a href="sisu-signup.html">Reset</a>
                        </p>
                    </form>
                </div>

                <div class="col-md-3">

                </div>

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

<?php
require_once "common/footer.php";
?>
