<?php
session_start();

$host = 'localhost';
$user = 'ahmeuesz_dino';
$pwd  = 'X39IW2ho3^5h6d_5';
$db   = 'ahmeuesz_myapp';
$conn = mysqli_connect($host, $user, $pwd, $db);

$msg = '';
if (isset($_GET['registered'])) {
    $msg = "<div class='success'>✅ Registration successful. Please log in now.</div>";
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);

    if ($email === '' || $password === '') {
        $msg = "<div class='error'>⚠️ Please fill in all fields.</div>";
    } else {
        $sql = "SELECT * FROM image_table WHERE email='$email' AND password='$password' LIMIT 1";
        $res = mysqli_query($conn, $sql);
        if ($res && mysqli_num_rows($res) === 1) {
            $user = mysqli_fetch_assoc($res);
            // store only safe items in session
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['user_name'] = $user['name'];
            $_SESSION['user_email'] = $user['email'];
            $_SESSION['user_profile'] = $user['profile_image'];
            header('Location: projects');
            exit();
        } else {
            $msg = "<div class='error'>❌ Invalid email or password.</div>";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Login</title>
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="container">
    <h1 class="page-title">LOGIN</h1>

    <?php if ($msg) echo $msg; ?>

    <form class="reg-form" method="post" action="">
      <div class="input-group">
        <label>Email</label>
        <input type="email" name="email" placeholder="you@example.com" value="<?php echo isset($email) ? htmlspecialchars($email) : ''; ?>">
      </div>

      <div class="input-group">
        <label>Password</label>
        <input type="password" name="password" placeholder="Your password">
      </div>

      <div style="display:flex;justify-content:center;">
        <input type="submit" class="btn btn-submit" value="Login">
      </div>
    </form>

    <div class="controls" style="margin-top:18px;text-align:center;">
      <a class="showcode-btn" href="registration.php">Need an account? Register</a>
    </div>
  </div>
</body>
</html>
