<?php
session_start();
include 'connect.php';

// Sign In logic
if (isset($_POST['signIn'])) {
    $email    = $_POST['email'];
    $password = md5($_POST['password']);

    $sql = "SELECT * FROM users WHERE email='$email' AND password='$password'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $_SESSION['email'] = $row['email'];
        $_SESSION['role']  = $row['role'];

        if ($row['role'] === 'admin') {
            echo "<script>
                    alert('Login Successful!');
                    window.location.href = 'add_product.php';
                  </script>";
        } else {
            echo "<script>
                    alert('Login Successful!');
                    window.location.href = 'homepage.php';
                  </script>";
        }
        exit();
    } else {
        echo "<script>alert('Incorrect Email or Password');</script>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Login Page</title>
  <style>
    body {
      margin: 0;
      font-family: Arial, sans-serif;
      background: url('img/logbg.jpg') no-repeat center center/cover;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }
    .form-container {
      background: rgba(0,0,0,0.7);
      padding: 40px;
      border-radius: 10px;
      color: white;
      width: 350px;
      text-align: center;
    }
    .form-container h2 {
      margin-bottom: 20px;
      font-size: 22px;
    }
    .form-container input {
      width: 100%;
      padding: 10px;
      margin: 10px 0;
      border: none;
      border-radius: 8px;
      background: #f0f0f0;
      font-size: 14px;
    }
    .form-container button {
      width: 100%;
      padding: 12px;
      margin-top: 15px;
      border: none;
      border-radius: 25px;
      background: #c58a56;
      color: white;
      font-size: 16px;
      font-weight: bold;
      cursor: pointer;
    }
    .form-container button:hover {
      background: #a8713c;
    }
    .toggle {
      margin-top: 15px;
      font-size: 14px;
    }
    .toggle a {
      color: #66e223ff;
      text-decoration: none;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <div class="form-container">
    <h2>Login Page</h2>
    <form method="POST">
      <input type="email" name="email" placeholder="Email" required>
      <input type="password" name="password" placeholder="Password" required>
      <button type="submit" name="signIn">Sign in</button>
    </form>
    <div class="toggle">
      Don't have an account yet? <a href="register.php">Sign up</a>
    </div>
  </div>
</body>
</html>
