<?php
    session_start();
    include "../connection.php";
    unset($_SESSION['username']);
    unset($_SESSION['admin']);
    unset($_SESSION['typSluzby']);
    $msg;
    if (isset($_POST['submit'])) {
        $username = mysqli_real_escape_string($conn, $_POST['username']);
        $username = $_POST['username'];
        $password = $_POST['password'];
        $sql = "SELECT * FROM $usersTable WHERE username='$username'";
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result)>0) {
            echo mysqli_num_rows($result);
            while ($row = mysqli_fetch_assoc($result)) {
                if (password_verify($password, $row['password'])) {
                    $_SESSION['username']=$row['username']; 
                    $_SESSION['admin']=$row['admin'];
                    $_SESSION['typSluzby']=$row['id_typSluzby'];
                    header("Location:index.php");

                }
                else{
                    $msg = "Špatné heslo";
                }
            }
        }
        else{
            $msg = "Špatné jméno nebo heslo";
        }

    }

?>

<!DOCTYPE html>
<html lang="en">
<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">
    <title>Document</title>
    <link rel="stylesheet" href="../styleKadernictvi.css">
    <link rel="stylesheet" href="styleAdmin.css">
</head>
<body>
    <header class="loginHeader">
        <h2 class="font-normal">IS - Kadeřnictví Hrdlořezy</h2>      
    </header>

    
    <form class="loginForm" action="#" method="post">
        <h1>PŘIHLÁŠENÍ</h1>
        
            <?php if (isset($msg)) {
                echo "<div class='msg'>".$msg."</div>"; 
            } ?>
        <div>
            <label for="username">Přihlašovací jméno</label>
            <input type="text" name="username" autocomplete="new-password">
        </div>
        <div>
            <label for="password">Heslo</label>
            <input type="password" name="password" autocomplete="new-password">
        </div>
        <input class="myBtn" type="submit" name="submit" value="Přihlásit">
    </form>

    
</body>
</html>
