<?php
session_start();
require_once('../connection.php');

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $email = $_POST['email'];
    $password = $_POST['password'];
    $customerID = $_POST['customerID'];

  
    $stmt = $conn->prepare("SELECT customerID, password, first_name FROM customers WHERE email = ?");
    
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($user = $result->fetch_assoc()) {
        if (password_verify($password, $user['password'])) {
            $_SESSION ['userID'] = $user['customerID'];
            $_SESSION ['email'] = $email;
            $_SESSION ['first_name'] = $user['first_name'];
            echo "<script>
                alert('You have successfully logged in.');
                window.location.href = '../dashboard/dashboard.php';
              </script>";
            exit();
        } else {
            echo "<script>
                alert('You have entered an incorrect password.');
                window.location.href = 'login.html';
              </script>";
            exit();
        }
    } else {
        echo "<script>
                alert('An account does not exist with that email address.');
                window.location.href = 'login.html';
              </script>";
            exit();
    }
    
    $stmt->close();
}

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GLH | Login</title>
</head>
</html>