<?php

include ("connect.php");
//include ("session.php");
include('logging.php');

session_start();


// define variables and set to empty values
$username = $password = "";


if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $username = mysqli_real_escape_string($db,$_POST['loginuser']);
  $password = mysqli_real_escape_string($db,$_POST['loginpass']);
  $selectedcompany = mysqli_real_escape_string($db,$_POST['companyselect']);

  $password = md5($password);

  $sql = "SELECT * FROM users WHERE username = '$username' and password = '$password' and enabled = '1'";
  $result = mysqli_query($db,$sql);

  $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
  $active = $row['enabled'];
  $role = $row['role'];
  $userid = $row['userid'];

  $count = mysqli_num_rows($result);

  if($count == 1) {
         //session_register("username");
         $_SESSION['login_user'] = $username;
         $_SESSION['role'] = $role;
         $_SESSION['userid'] = $userid;
         $_SESSION['companyselect'] = $selectedcompany;
         logEvent($db,"Login","User Logged in.");
         header("location: dashboard2.php");

         echo "Welcome ",$username ;
      }else {
         $error = "Your Login Name or Password is invalid";
      }
}
?>


<!doctype html>
<html lang="en">
  <head>

    <!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet" >


    <style>
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
        }
      }
    </style>
    <!-- Custom styles for this template -->
    <link href="css/floating-labels.css" rel="stylesheet">
    <title>ePay Solutions</title>
  </head>
  <body>
      <form class="form-signin" method = "POST" action = "">
  <div class="text-center mb-4">
   <!-- <h1 class="h3 mb-3 font-weight-normal">Demo Company LTD</h1> -->
   <h1 class="h3 mb-3 font-weight-normal">ePay Solutions</h1>
   <!--<p>Payroll Management System</a></p>-->
  </div>
  
  <?php if(isset($error)){

    echo "<div class=\"alert alert-danger\" >";
    echo $error; 
    echo "</div>";

  }?>

  <div class="form-label-group">
    <input type="text" name="loginuser" class="form-control" placeholder="Username" required autofocus>
    <label for="loginuser">Username</label>
  </div>

  <div class="form-label-group">
    <input type="password" name="loginpass" class="form-control" placeholder="Password" required>
    <label for="loginpass">Password</label>
  </div>


  <?php

  $sqlGetCompanies = "SELECT companyid,companyName FROM companyfile ORDER BY companyName;";

  $resultCompanies = mysqli_query($db,$sqlGetCompanies);

  $countCompanies = mysqli_num_rows($resultCompanies);

    if ($countCompanies !== 0) { //companies count start here
      
      echo "<p>Company";
      echo "<select name=\"companyselect\" id=\"companyselect\" required class=\"form-control\">";

      while ($rowCompany = mysqli_fetch_array($resultCompanies)) { 
        echo "<option value=\"" . $rowCompany['companyid'] . "\">" . $rowCompany['companyName'] . "</option>";
      }

      echo "</select> 
      </p>";

    } else {
      echo "<p>No company files available</p>";
    }


  ?>
  
  <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>

  <br>

  <a href = "createCompany.php">Create New Company</a>

  

  <p class="mt-5 mb-3 text-muted text-center">&copy; V2.0 - 2022</p>
</form>
</body>
</html>


