<?php session_start();
    require "connect.php";
    if (isset($_SESSION['user_id'])) {
        echo "<script>window.location.href = '../dashboard/index'</script>";
    }
    $email = $password = $error = $db_email = $db_password = $db_id = "";
    if (isset($_POST['submit'])) {
        htmlspecialchars(trim($email = $_POST['email']));
        htmlspecialchars(trim($password = $_POST['password']));

        $email = mysqli_real_escape_string($connection, $email);
        $password = mysqli_real_escape_string($connection, $password);

        $st_email = strtolower($email);
        $st_password = strtolower($password);

        $sql = "SELECT * FROM users WHERE email = '$st_email' AND password  = '$st_password'";
        $result = $connection->query($sql);
        while ($row = $result->fetch_assoc()) {
            $db_email = $row['email'];
            $db_password = $row['password'];
            $db_id = $row['user_id'];
        }

        if (empty($st_email) || empty($st_password)) {
            $error = "empty";
        }else if ($db_email == $st_email || $db_password == $st_password) {
            $_SESSION['user_id'] = $db_id;
            // $error = 'success';
        echo "<script>window.location.href = '../public/session.php?id=$db_id'</script>";
        }else {
            $error = "error";
        }
    }
?>