<?php include('../includes/connection.php');
@session_start();

if(isset($_SESSION['admin_login'])) {
  header("Location: index.php");
  }

if(isset($_POST['submit'])) {
  $query = mysqli_query($DB_CONN,"SELECT * from preferences WHERE admin_email = '{$_POST['username']}' and admin_pass = '{$_POST['password']}'");

  $num = mysqli_num_rows($query);
  if($num > 0) {
    $rowfetch = mysqli_fetch_assoc($query);
    $_SESSION['admin_login'] = $_POST['username'];
    $_SESSION['id'] = $rowfetch['id'];

    echo "<script> window.open('index.php','_self') </script>";

    } else {
      $message = 1;
      }
  }

if ((isset($_GET['logout'])) &&($_GET['logout']=="true")){
  
  $_SESSION['admin_login'] = NULL;
  unset($_SESSION['admin_login']);
  session_destroy();
  $message = 0;
}
?>


<!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="">

    <title> Login Page </title>
     <!-- Favicon -->
    <link rel="icon" href="../images/favicon.ico" type="image/x-icon" />
    <link rel="shortcut icon" type="image/x-icon" href="../images/favicon.png" />
    <!-- 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">

  </head>

  <body style="background-color:#1f8a3e">


    <div class="container">
      <div class="card card-login mx-auto mt-5" style="border:1px solid #1f8a3e">
        <div class="card-header"> <center>  Admin Login </center> </div>
        <div class="card-body">
          <?php if (isset($message)) {
        if($message == 0) {
          echo "<div class=\"alert alert-success\" style=\"cursor:default;\">You have Successfully Logged Out!</div>";
          } elseif($message == 1) {
            echo "<div class=\"alert alert-danger\" style=\"cursor:default;\">Credentials Does not Match!</div> ";
            }} ?>   
            
           <form action="" class="form-signin" role="form" method="post" >
            <div class="form-group">         
                <input name="username" type="email" required class="form-control" id="username" placeholder="Email address" maxlength="100">
            </div>
            <div class="form-group">
                <input name="password" type="password" required class="form-control" id="password" placeholder="Password" maxlength="50">
            </div>

            <button class="btn btn-lg btn-success btn-block" type="submit" name="submit">Sign in</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>

  </body>

</html>