<?php
$host = 'localhost';
$dbname = 'Name_Password';
$user = 'pail';
$password = 'password';

try {
    $dsn = "mysql:host=$host;dbname=$dbname";
    $pdo = new PDO($dsn, $user, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // enable error mode
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
    exit(); // terminate the script on connection failure
}

// Handle form submission
if (isset($_POST['submit'])) {
    // Get the form data
    $username = $_POST['username'];
    $password = $_POST['password'];
    $ipAddress = $_SERVER['REMOTE_ADDR']; // Get the client's IP address

    // Validate the input
    $errors = array();
    if (strlen($username) < 3) {
        $errors[] = "Username must be at least 3 characters long";
    }
    if (strlen($password) < 8) {
        $errors[] = "Password must be at least 8 characters long";
    }

    if (empty($errors)) {
        try {
            // Insert the user into the database using prepared statement
            $stmt = $pdo->prepare("INSERT INTO prihlasovacie_udaje (FirstName, Password, IPAddress) VALUES (?, ?, ?)");
            $stmt->execute([$username, $password, $ipAddress]);
            header("Location: info.php");
            exit();
            // Do not display any message after successful insertion
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    } else {
        // Display validation errors
        echo "<ul>";
        foreach ($errors as $error) {
            echo "<li>$error</li>";
        }
        echo "</ul>";
    }
}

// close the database connection
$pdo = null;
?>
<!DOCTYPE html>
<html>
<head>
    <title>log in</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #F7F7F7;
        }
        h1 {
            margin: 20px;
        }
        form {
            margin: 20px auto;
            background-color: #FFFFFF;
            border: 1px solid #CCCCCC;
            padding: 20px;
            max-width: 500px;
            text-align: center;
        }
        input[type="text"], input[type="password"] {
            display: block;
            margin-bottom: 10px;
            padding: 10px;
            border: 1px solid #CCCCCC;
            width: 100%;
            box-sizing: border-box;
        }
        input[type="submit"] {
            background-color: #4CAF50;
            color: #FFFFFF;
            padding: 10px;
            border: none;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #3E8E41;
        }
    </style>
</head>
<body>

<form method="POST" action="">
<h1>Log in</h1>
    <label>Username:</label>
    <input type="text" name="username" required minlength="3"><br><br>
    <label>Password:</label>
    <input type="password" name="password" required minlength="8"><br><br>
    <input type="submit" name="submit" value="log in">
</form>
<br>

</body>
</html>