<?php
require 'core/connection.php';

 if(isset($_POST['startapp'])){
    $username = sanitize($_POST['username']);
    $password = $_POST['password'];
    $hash = sha1(md5($password));

    if(empty($username) || empty($password)){
      $error = "All fields must be filled to login";
    }else{
      $verify = get_alias('users','username',$username,'password',$hash);
      if($verify->rowCount() > 0){
        $fetch = $verify->fetchAll(PDO::FETCH_OBJ);
        foreach($fetch as $log){
          $id = $log->id;
          $id2 = $log->iden;
          $user = $log->username;
          $priviledge = $log->priviledge;
          $chepas1 = sha1(md5('lecturer'));
          $chepas2 = sha1(md5('student'));
          $chepas3 = sha1(md5('staff'));

          if ($priviledge == 'HOD') {
           $verify2 = get_alias('department','id',$id2,'hod',$user);
           if ($verify2->rowCount() > 0) {
             $fetch2 = $verify2->fetchAll(PDO::FETCH_OBJ);
             foreach ($fetch2 as $key) {
               $dname = $key->name;
               $vhod = "HOD$dname";
               $vhod = sha1(md5($vhod));
             }
           }
          }
          if ($hash == $chepas1 || $hash == $chepas2 || $hash == $chepas3 || (isset($vhod) && $hash == $vhod)) {
            $validate = "yespls"; 
          }else{
            $validate ="nopls";
          }



          if ($priviledge == "general" || $priviledge == "staff" || $priviledge == "HOD") {
            $link = "admin/";
          }else{
            $link = "main.php";
          }

          if ($priviledge == "deactivated") {
          $error = "You have been deactivated... Hence cannot login unless activated";
          }else{
          $encode_id = md5($id);
          session_start();
          $_SESSION['is_admin'] = $encode_id; 
          $_SESSION['username'] = $username; 
          $_SESSION['admin_id'] = $id; 
          $_SESSION['admin_id2'] = $id2; 
          $_SESSION['validating'] = $validate; 
          $_SESSION['priviledge'] = $priviledge; 
          $_SESSION['hello'] = 0;

          redirect($link);
        }
      }
      }else{
        $error = "Invalid login credentials. Try again";
      }
    }
  }
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Dominican Institute, Nigeria :: E-Classroom  | Login </title>
    <link href="img/logos.png" rel="icon">
    <!-- Bootstrap -->
    <link href="admin/vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="admin/vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- NProgress -->
    <link href="admin/vendors/nprogress/nprogress.css" rel="stylesheet">
    <!-- Animate.css -->
    <link href="admin/vendors/animate.css/animate.min.css" rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="admin/build/css/custom.min.css" rel="stylesheet">
  </head>
  <body class="login">
    <div>
      <a class="hiddenanchor" id="signup"></a>
      <a class="hiddenanchor" id="signin"></a>

      <div class="login_wrapper">
        <div class="animate form login_form">
          <section class="login_content">
            <div class="panel panel-warning">
              <div class="panel-body">
            <form method="post">
              <img src="img/logos.png" width="28%">
              <p style="font-family: sans-serif; font-weight: bolder; font-size: 20px; color: black">Dominican Institute Samonda, Ibadan</p>
              <h2 style="color: black"> E-Classroom Login</h2>
                    <?php 
                      if(isset($error)){ ?>
                        <div class="alert alert-danger">
                          <strong>ERROR: </strong> <?php echo $error; ?>
                        </div>
                      <?php
                      }
                    ?>
              <div>
                <input type="text" class="form-control" name="username" autocomplete="off" placeholder="Username" required="" />
              </div>
              <div>
                <input type="password" class="form-control" required name="password" placeholder="********" />
              </div>
              <div>
                <button class="btn btn-default btn-block" type="submit" name="startapp">Log in</button>
              </div><br>
              </form>
            </div>
          </div>

              <div class="clearfix"></div>


                <div class="clearfix"></div>
                <br />

                <div>
                  <div class="copyright">
                     Copyright &copy; <?php echo date('Y'); ?> All Rights Reserved, <strong> Dominican Institute, Ibadan</strong>.
                  </div>
                  <div class="credits">
                    Designed by ICT Unit Dominican Institute
                  </div>
                </div>
            
          </section>
        </div>
      </div>
    </div>
  </body>
</html>