<?php
session_start();
require 'db.php'; // Připojení k databázi

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt = $pdo->prepare("SELECT id, password_hash FROM users WHERE username = ?");
    $stmt->execute([$username]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($user && password_verify($password, $user['password_hash'])) {
        $_SESSION['user_id'] = $user['id'];
        header("Location: index.php");
        exit();
    } else {
        $error = "Neplatné přihlašovací údaje!";
    }
}
?>

<!DOCTYPE html>
<html lang="cs">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Přihlášení</title>
    <link rel="stylesheet" href="styles.css"> <!-- Odkaz na externí CSS -->
</head>
<body>
    <div class="container">
        <h2>Přihlášení</h2>
        <?php if (isset($error)) echo "<p class='error'>$error</p>"; ?>
        <form method="POST">
            <label for="username">Uživatelské jméno:</label>
            <input type="text" id="username" name="username" class="input-field" required>
            
            <label for="password">Heslo:</label>
            <input type="password" id="password" name="password" class="input-field" required>
            
            <button type="submit" class="btn">Přihlásit</button>
        </form>
        <p>Nemáte účet? <a href="register.php" class="link">Zaregistrujte se</a></p>
    </div>
</body>
</html>
