<?php
session_start();
include "config/database.php";

$message = "Enter your username and password to start your account";

if(isset($_POST['username'])){
  $username = $_POST['username'];
  $password = $_POST['password'];

  $sql = "SELECT * FROM user WHERE Username = '$username'";
  $result = mysqli_query($connection,$sql);

  $data = mysqli_fetch_assoc($result);

  if(mysqli_num_rows($result) == 0){
    $message = "<b style='color:red;'>username not found</b>";
  }else{

    if(password_verify($password,$data['password'])){
      $_SESSION['username'] = $username;
      $_SESSION['fullname'] = $data['fullname'];
      $_SESSION['role'] = $data['role'];

      echo "<script>location.href='index.php'</script>";
    }else{
      $message = "<b style='color:red;'>password not found</b>";
    }

  }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>IOT SYSTEM - login</title>

  <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
  <link rel="stylesheet" href="dist/css/adminlte.min.css">
</head>

<body class="hold-transition login-page">

<div class="login-box">

  <div class="login-logo">
    <b>Monitoring</b>system
  </div>

  <div class="card">
    <div class="card-body login-card-body">

      <!-- INI YANG BENAR (DYNAMIC MESSAGE) -->
      <p class="login-box-msg"><?= $message ?></p>

      <form method="post">

        <div class="input-group mb-3">
          <input type="text" class="form-control" name="username" placeholder="Username" required>
        </div>

        <!-- PASSWORD TETAP ADA TAPI DIABAIKAN -->
        <div class="input-group mb-3">
          <input type="password" class="form-control" name="password" placeholder="Password" required>
        </div>

        <button type="submit" class="btn btn-success btn-block">Sign In</button>

      </form>

    </div>
  </div>
</div>

</body>
</html>