<?php
   //Start sessions
   ob_start();
   session_start();

   // link file with constants
   include_once dirname(__FILE__) . '../../includes/Constants.php';
 
   //connecting to mysql database
   $conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
   if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
   }
?>


<html lang = "en">
   
   <head>
      <meta charset="utf-8">
      <title>Security application login</title>
      <link href = "css/bootstrap.min.css" rel = "stylesheet">
      <link href="login.css" rel="stylesheet" type="text/css">
   </head>
	
   <body>
      
      <h1>Enter the ID and password displayed in the application</h1> 
      <div class = "container form-signin">
         
         <?php
            $msg = '';
            // When user which want to display a SMS has the correct login details
            if (isset($_POST['login']) && !empty($_POST['userid']) && !empty($_POST['password'])) {
               $userid = $_POST['userid'];
               $password = $_POST['password'];
               $tablename = "users";
               $sql1 = "SELECT * FROM ".$tablename." WHERE userid = '".$userid."'";
               $result1 = $conn->query($sql1);
               $row = $result1->fetch_assoc();

               if ($row !=0){
                  $dbusername = $row['userid'];
                  $dbpassword = $row['password'];
                     if ($dbusername == $userid&& password_verify($password, $dbpassword)) {
                     // Save login details to Session
                     session_regenerate_id();
                     $_SESSION['loggedin'] = TRUE;
                     $_SESSION['userid'] = $_POST['userid'];
                     // Open page with table of users SMS          
                     header("Location: http://vaklur.cz/permissions/view/view_data.php");
                     exit();
                     $conn->close();
                  }
                   else{
                  $msg = "<p style=color:red>Incorrect login details!</p>";
                  }
                  
               }

               else {
                  // When user which want to display a SMS has the incorrect login details
                  $msg = "<p style=color:red>Incorrect login details!</p>";
               }
               
            }
         ?>
      </div> <!-- /container -->
      
      <div class = "container">
      
         <form class = "form-signin" role = "form" 
            action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method = "post">
            <input type = "text" class = "form-control" 
               name = "userid" placeholder = "ID" 
               required autofocus></br>
            <input type = "password" class = "form-control"
               name = "password" placeholder = "password" required>
            <button class = "btn btn-lg btn-primary btn-block" type = "submit" 
               name = "login">Login</button>
            <h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
         </form>
      </div> 
      
   </body>
</html>