<?php
  session_start();
?>
<!DOCTYPE html>
<html>

<head>
    <title>Polytical Edge | Login</title>
    <?php
  include 'css.php';
  include './assets/database/database.php';
  if (isset($_SESSION["validated"]) && $_SESSION['validated'] == 'yes') {
    echo "<script>document.location = 'index.php';</script>";
  }
  ?>
</head>

<body>
    <div class="main-wrapper">
        <div class="page-wrapper full-page">
            <div class="page-content d-flex align-items-center justify-content-center bg-gray">
                <div class="row w-100 mx-0 auth-page">
                    <div class="col-md-10 col-xl-8 mx-auto">
                        <div class="card">
                            <div class="row">
                                <div class="col-md-4 pr-md-0">
                                    <div class="auth-left-wrapper">

                                    </div>
                                </div>
                                <div class="col-md-8 pl-md-0">
                                    <div class="auth-form-wrapper px-4 py-5">
                                        <a href="index.php" class="d-block mb-4 text-dark ">
                                            <img src="assets/images/pe-logo.png" class="pe-logo">
                                        </a>
                                        <h5 class="text-muted font-weight-normal mb-4">Welcome back! Log in to your
                                            account.</h5>
                                        <form class="forms-sample" method="POST">
                                            <div class="form-group">
                                                <label for="exampleInputEmail1">Email address</label>
                                                <input type="email" name="email" class="form-control"
                                                    id="exampleInputEmail1" placeholder="Email">
                                            </div>
                                            <div class="form-group">
                                                <label for="exampleInputPassword1">Password</label>
                                                <input type="password" name="password" class="form-control"
                                                    id="exampleInputPassword1" autocomplete="current-password"
                                                    placeholder="Password">
                                            </div>
                                            <div class="form-check form-check-flat form-check-primary">
                                                <label class="form-check-label">
                                                    <input type="checkbox" class="form-check-input">
                                                    Remember me
                                                </label>
                                            </div>
                                            <div class="mt-3">
                                                <button name="submit" type="submit"
                                                    class="btn btn-primary mr-2 mb-2 mb-md-0">Login</button>
                                            </div>
                                            <!-- <a href="register.php" class="d-block mt-3 text-muted">Not a user? Sign up</a> -->
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php include 'js.php'; ?>

    <?php
  if (isset($_POST['submit'])) {
    $conn = connect_database();
    $sql = "SELECT * from User WHERE Email='" . $_POST['email'] . "' AND `Password` ='" . md5($_POST['password']) . "'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
      while ($row = mysqli_fetch_all($result, MYSQLI_ASSOC)) {
        $_SESSION["validated"] = 'yes';
        $_SESSION["email"] = $row[0]['Email'];
        $_SESSION["name"] = $row[0]['Name'];
        $_SESSION["role"] = $row[0]['Role'];
        // echo"<script>console.log('".$_SESSION['name']."')</script>";
        echo "<script>window.location.href = 'index.php';</script>";
      }
    } else {
      echo "<script> alert('not a match');</script>";
    }
    $conn->close();
  }
  ?>
</body>

</html>