<?php
 session_start();

    if (!isset($_SESSION['username'])){
        echo ("<script>location.href = 'login.php';</script>");
    }
    if ($_SESSION['admin']!=1){
        echo ("<script>location.href = 'index.php';</script>");
    }

    include "../connection.php";

    if (isset($_GET["deleteID"])){
        $deleteID = $_GET["deleteID"];
        $delete=mysqli_query($conn, "DELETE FROM $usersTable WHERE id=$deleteID");
    }

    if (isset($_POST['submitAddUser'])) {
            $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
            $username = $_POST['username'];
            $id_typSluzby = $_POST['typSluzby'];
            $sql = ("SELECT * FROM $usersTable WHERE username='$username'");
            $result = mysqli_query($conn, $sql);
            

            if (mysqli_num_rows($result)==0) {
                $stmt = $conn->prepare("INSERT INTO $usersTable(username, password, id_typSluzby) VALUES (?,?,?)");
                $stmt->bind_param('sss',$username, $password, $id_typSluzby);
                $stmt->execute();
                $msgGood = "Uživatel vytvořen";
                $stmt->close();
            }
            else{
                $msgBad = "Tento uživatel již existuje";
            }

            
    }


?>



<!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>Kadeřnictví Hrdlořezy - Admin</title>
    <link rel="stylesheet" href="../styleKadernictvi.css">
    <link rel="stylesheet" href="styleAdmin.css">
</head>
<body>

    <button><img class="menuIcon lg:hidden" src="../img/menu_icon.png" alt=""></button>

    <nav class="mainMenu">           
        <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-sluzby-phone font-bold " href="index.php">DNEŠNÍ TERMÍNY</a>
        <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-sluzby-phone font-bold" href="kadernictvi.php">KADEŘNICTVÍ</a>
        <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-foto-phone font-bold " href="kosmetika.php">KOSMETIKA</a>
        <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-onas-phone font-bold" href="manikura.php">MANIKÚRA</a>             
        <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-kontakt-phone border-gray-300 font-bold" href="pedikura.php">PEDIKÚRA</a>
        <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-kontakt-phone border-b border-gray-300 font-bold active"  href="users.php">UŽIVATELÉ</a>
        
    </nav>
        <div class="logoutDiv hidden lg:visible">
            <p>uživatel: <b><?php echo $_SESSION['username']; ?></b></p>
            <span>|</span>
            <a class="logoutBtn" href="logout.php">ODHLÁSIT</a>
        </div>
    <nav>
        <div class="mainMenuPhone justify-center justify-items-center text-center z-50  p-0 mt-0 lg:hidden">
            <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-sluzby-phone font-bold " href="index.php">DNEŠNÍ TERMÍNY</a>
            <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-sluzby-phone font-bold" href="kadernictvi.php">KADEŘNICTVÍ</a>
            <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-foto-phone font-bold " href="kosmetika.php">KOSMETIKA</a>
            <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-onas-phone font-bold" href="manikura.php">MANIKÚRA</a>             
            <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-kontakt-phone border-gray-300 font-bold" href="pedikura.php">PEDIKÚRA</a>
            <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-kontakt-phone border-gray-300 font-bold active" href="users.php">UŽIVATELÉ</a>
            <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-kontakt-phone border-b border-gray-300 font-bold logoutBtn" href="logout.php">ODHLÁSIT</a>
        </div>
    </nav>

    <header class="updateHeader lg:hidden">
        <h1>Uživatelé</h1>
    </header>
    <div class="usersContainer mt-20 lg:mt-40">
        <div class="usersForm text-center">
            <table class="usersTable">
                <?php 
                    $sql = "SELECT * FROM $usersTable";
                    $searchResult = mysqli_query($conn,$sql);
                    if($searchResult){
                        $rowCount = mysqli_num_rows($searchResult);

                        if ($rowCount > 0){
                            while($row = mysqli_fetch_array($searchResult)) 
                            {
                                echo "
                                <tr>
                                    <td class=''>".$row["username"] ."</td> 
                                    <td>"?>
                                        <a class="delete" href="users.php?deleteID=<?php echo $row['id']; ?>" onclick="return confirm('Opravdu smazat?')">smazat</a>
                                        <?php "
                                    </td>            
                                </tr>
                                
                                "; 
                            }      
                        }
                        else{
                            echo "<div class='text-center mb-24 mt-10 text-xl font-bold'>- žádní uživatelé -<span>";
                        }
                    }
                ?>
                
            </table>
        </div>

        <div class="addUsers mb-20 lg:mb-0">
            <form name="addUserForm" method="post" action="">
                <h2>Přidat uživatele</h2>

                <?php
                if(isset($msgGood)) { echo "<div class='msgGood'>".$msgGood."</div>";} 
                if(isset($msgBad)) { echo "<div class='msgBad'>".$msgBad."</div>";} 
                ?>
                <div class="text-left w-min mx-auto mt-4 addUserInput">
                    <label required for="username">Uživatelské jméno</label><br>
                    <input required type="text" name="username"><br>
                </div>

                <div class="text-left w-min mx-auto mt-4 addUserInput">
                    <label required for="username">Heslo</label><br>
                    <input required type="password" name="password" autocomplete="new-password"><br>
                </div>

                <div class="text-left w-min mx-auto mt-4 addUserInput">
                    <label required for="username">Typ služby</label><br>
                    <select required name="typSluzby" class="w-full">
                        <option value="1">Kadeřnictví</option>
                        <option value="2">Kosmetika</option>
                        <option value="3">Manikúra</option>
                        <option value="4">Pedikúra</option>
                    <select>
                    <br>
                </div>

                <input type="submit" name="submitAddUser" value="Přidat" class="mt-8 myBtn btnGreen">
            </form>
        </div>
           

        
    </div>

    <script src="../scriptKadernictvi.js"></script>

</body>
</html>