<?php
session_start();
$sms = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Include database connection file
    include('connect.php');

    // Escape user inputs for security
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);

    //$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
    

    // Query to check if the username and password exist in the database
    $query = "SELECT * FROM users WHERE email = '$username' AND password = '$password' ";
    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) == 1) {
       $row = mysqli_fetch_assoc($result);
       $role = $row['role'];
       // Valid login, start session
       $_SESSION['username'] = $username;
       //hii nimeweka kwa ajili ya kutumia switch
       // Redirect to dashboard or another authenticated page
       switch ($role) {
           case 'admin':
                 header("Location: maincreate.php");
                 break;
           case 'guest':
                 header("Location: main2.php");
                 break;
           default:
                 header("Location: guest3.php");
                 break;
       
             } 
    }
    else{
            // Invalid login credentials
            $sms = "Invalid username or password";  
      }
    }
     
    // Close database connection
  
?>

<!DOCTYPE html>
<html>

<head>
      <title>HTML Login Form</title>
      <link rel="stylesheet" href="styla.css">
</head>

<body>
      
      <div class="main">
           <h1>SHOP - MIS</h1>
           <h2><?php echo $sms; ?></h2>
            <h3>Enter your login credentials</h3>
           <form action="" method="post">
           <!--<Form name="f1" action = "authentication.php" onsubmit = "return validation()" method = "POST">-->

                  <label for="first"> Username:</label>
                  <input type="text" id="first" name="username" placeholder="Enter your Username" required>
                  <label for="password"> Password:</label>
                  <input type="password" id="password" name="password" placeholder="Enter your Password" required>

                  <div class="wrap">
                        <button type="submit" name="login"
                                onclick="solve()">
                              Submit
                        </button>
                  </div>
            </form>
            <!--<p>Not registered?
                  <a href="#"
                     style="text-decoration: none;">
                        Create an account
                  </a>
            </p>-->
      </div>
</body>

</html>