<?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 'accountant':
                 header("Location: maincreatetwo.php");
                 break;
            case 'seller':
                 header("Location: maincreatethree.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>LAMILE HARDWARE</title>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">

      <link rel="stylesheet" href="styla.css">
</head>

<body>
      
      <div class="main">
           <h1>LAMILE HARDWARE - 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>
                  <div class="password-wrapper">
                  <input type="password" id="password" name="password" placeholder="Enter your Password" required>
                  <span class="toggle-password" onclick="togglePassword()">👁</span>
                  </div>

                  <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>

      <script>
            function togglePassword() {
                  const passwordInput = document.getElementById('password');
                  const toggleIcon = document.querySelector('.toggle-password');
                  const isPassword = passwordInput.type === 'password';
                  passwordInput.type = isPassword ? 'text' : 'password';
                  toggleIcon.textContent = isPassword ? '🙈' : '👁'; // Toggle icons
            }
      </script>
</body>

</html>