<?php
    session_start();
    include("../scripts/connection.php");
    include("../scripts/functions.php");

    if($_SERVER['REQUEST_METHOD'] == "POST"){
        $user_name = $_POST['user_name'];
        $password = $_POST['password'];

        if(!empty($user_name) && !empty($password)) {
            $user_data = get_user_data_name($con, $user_name);
            if($user_data) {
                $phash = generate_password_hash($password, $user_data['salt']);
                if($user_data['password'] === $phash) {
                    $_SESSION['user_id'] = $user_data['user_id'];
                    $_SESSION['user_group'] = $user_data['user_group'];
                    header("Location: index.php");
                    die;
                } else {
                    echo "Benutzername oder Passwort stimmen nicht";
                }
            } else {
                echo "Benutzername oder Passwort stimmen nicht";
            }

        } else {
            echo "Gib bitte gültige Daten ein!";
        }
    }
    $con = null;
?>

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/login.css">
    <title>Punktesystem-KSP</title>
</head>
<body>
    <div class="center">
        <h1>PUNKTESYSTEM-KSP</h1>
        <form method="post">
            <div class="txt_field">
                <input id="user_name" type="text" name="user_name" required/>
                <span></span>
                <label>Benutzername</label>
            </div>
            <div class="txt_field">
                <input id="password" type="password" name="password" autocomplete="off" required>
                <span></span>
                <label>Passwort</label>
            </div>
            <input class="button" id="button" type="submit" value="Login"><br><br>
        </form>
    </div>    
</body>
</html>