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

    include "../connection.php";
    include "../function.php";

    if (isset($_GET["deleteID"])){
        $deleteID = $_GET["deleteID"];

        $email = "";
        $sql = "SELECT *  FROM $rezervaceTable WHERE id=$deleteID";
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result)>0) {
            while ($row = mysqli_fetch_assoc($result)) {
                    $email=$row['email']; 
            }
        }
        if ($email!="") {
            sendMail("","","","","",$email,"delete");
        }

        $delete=mysqli_query($conn, "DELETE FROM $rezervaceTable WHERE id=$deleteID");
    }
        $todayDate = (date("Y-m-d"));
        $sql = "SELECT *  FROM $rezervaceTable INNER JOIN $sluzbyTable ON $rezervaceTable.id_sluzby=$sluzbyTable.id_sluzby WHERE DATE(`datum`) >= DATE(NOW()) AND $sluzbyTable.id_typSluzby = 4 ORDER BY datum";
        $result = resultWithPagination(10, $sql);

        if (isset($_GET['vybraneDatum'])) 
        { 
            $vybraneDatum = ($_GET['vybraneDatum']);
        }

    
?>















<!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 active" href="pedikura.php">PEDIKÚRA</a>
        <?php
            if ($_SESSION["admin"]==1) {
                ?>
                    <a id="uzivateleBtn" class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-kontakt-phone border-b border-gray-300 font-bold"  href="users.php">UŽIVATELÉ</a>
                <?php
            }
        ?>
        
    </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 active" href="pedikura.php">PEDIKÚRA</a>
            <?php
                if ($_SESSION["admin"]==1) {
                    ?>
                        <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-kontakt-phone border-gray-300 font-bold" href="users.php">UŽIVATELÉ</a>
                    <?php
                }
            ?>       
            <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>Pedikúra</h1>
    </header>


    
        <section class="mt-20 lg:mt-40">
            <div class="formFilterDiv">
                <form id="dateForm" method="post">
                    <input id="sluzbaTyp" hidden readonly type="text" value="pedikúra">
                    <input id="dateFormSubmit" name="dateFormSubmit" type="date" value='<?php echo $vybraneDatum ?>'>
                    <button id="deleteDateButton" class="ml-8 text-red-500 underline font-bold lg:mt-3">smazat filtr</button>            
                </form>

                <form id="searchForm" class="searchForm" method="post">
                        <div>
                            <input class="rounded-r-none mr-0 w-10/12 lg:w-4/12" type="text" name="search" placeholder="Vyhledávání">
                            <input id="searchFormSubmit" class="rounded-l-none -ml-2 px-2" type="submit" name="submit" value="🔍">
                        </div>
                </form>
            </div>
            <table class="rezervaceTable">
                <tr>
                    <th>Jmeno</th>
                    <th>Služba</td>
                    <th>Datum</th>
                    <th>Akce</th>               
                </tr>

                <?php
                    if (isset($_POST['submit'])) {
                        $search = $_POST['search'];

                        if (isset($_GET['vybraneDatum'])) {
                            $sql = "SELECT * FROM $rezervaceTable INNER JOIN $sluzbyTable ON $rezervaceTable.id_sluzby=$sluzbyTable.id_sluzby WHERE (id='$search' OR jmeno LIKE '%$search%' OR nazev LIKE '%$search%') AND ($sluzbyTable.id_typSluzby = 4) AND datum='$vybraneDatum' ORDER BY datum";
                        }
                        else{
                            $sql = "SELECT * FROM $rezervaceTable INNER JOIN $sluzbyTable ON $rezervaceTable.id_sluzby=$sluzbyTable.id_sluzby WHERE (id='$search' OR jmeno LIKE '%$search%' OR nazev LIKE '%$search%') AND DATE(`datum`) >= DATE(NOW()) AND $sluzbyTable.id_typSluzby = 4 ORDER BY datum";
                        }
                        
                        $searchResult = mysqli_query($conn,$sql);

                        if($searchResult){
                            $rowCount = mysqli_num_rows($searchResult);

                            if ($rowCount > 0){
                                while($row = mysqli_fetch_array($searchResult)) 
                                {
                                    $date = date("d-m-Y", strtotime($row["datum"]));
                                    echo "
                                    <tr>
                                    <td class='w-1/4'>".$row["jmeno"] ."</td>
                                    <td class='w-1/4'>".$row["nazev"] ."</td>
                                    <td class='w-1/4'>".$date."</td>
                                    <td class='buttons w-1/4'>"?>
                                        <?php if ($_SESSION['typSluzby']==4 || $_SESSION['admin']==1) {      
                                        ?>
                                            <a onclick="return confirm('Opravdu smazat?')" class='delete' href='pedikura.php?deleteID=<?php echo $row['id'];?>'>X</a>
                                            <a class='update' target="_blank" href='update.php?updateID=<?php echo $row['id'];?>&sluzbaTyp=pedikúra&cas=<?php echo $row['cas_zacatek'];?>'>></a>
                                        <?php
                                        } ?>

                                        <?php "
                                    </td>
                                    
                                    </tr>
                                    
                                    "; 
                                }

                                
                            }
                            else{
                                echo "<tr class='w-full' ><td colspan='4' class='text-center my-16  text-lg py-10 w-full'>- žádné rezervace -<td><tr>";
                            }
                        }

                    }
                    else{
                        if (isset($_GET['vybraneDatum'])) {
                            $sql = "SELECT *  FROM $rezervaceTable INNER JOIN $sluzbyTable ON $rezervaceTable.id_sluzby=$sluzbyTable.id_sluzby WHERE ($sluzbyTable.id_typSluzby = 4) AND datum='$vybraneDatum' ORDER BY datum";
                            $result = mysqli_query($conn, $sql);
                            if (mysqli_num_rows($result) > 0) {
                                while($row = mysqli_fetch_array($result)) 
                                {
                                    $date = date("d-m-Y", strtotime($row["datum"]));
                                    echo "
                                        <tr>
                                            <td>".$row["jmeno"] ."</td>
                                            <td>".$row["nazev"] ."</td>
                                            <td>".$date."</td>
                                            <td class='buttons w-1/4'>"?>
                                                <?php if ($_SESSION['typSluzby']==4 || $_SESSION['admin']==1) {      
                                                ?>
                                                    <a onclick="return confirm('Opravdu smazat?')" class='delete' href='pedikura.php?deleteID=<?php echo $row['id'];?>'>X</a>
                                                    <a class='update' target="_blank" href='update.php?updateID=<?php echo $row['id'];?>&sluzbaTyp=pedikúra&cas=<?php echo $row['cas_zacatek'];?>'>></a>
                                                <?php
                                                } ?>
                                                <?php "
                                            </td>
                                            
                                        </tr>
                                    ";
                                }
                            }
                            else{
                                echo "<tr class='w-full' ><td colspan='4' class='text-center my-16  text-lg py-10 w-full'>- žádné rezervace -<td><tr>";
                            }
                        }
                        else{
                            if (mysqli_num_rows($result) > 0) {
                                while($row = mysqli_fetch_array($result)) 
                                {
                                    $date = date("d-m-Y", strtotime($row["datum"]));
                                    echo "
                                        <tr>
                                            <td>".$row["jmeno"] ."</td>
                                            <td>".$row["nazev"] ."</td>
                                            <td>".$date."</td>
                                            <td class='buttons w-1/4'>"?>
                                                <?php if ($_SESSION['typSluzby']==4 || $_SESSION['admin']==1) {      
                                                ?>
                                                    <a onclick="return confirm('Opravdu smazat?')" class='delete' href='pedikura.php?deleteID=<?php echo $row['id'];?>'>X</a>
                                                    <a class='update' target="_blank" href='update.php?updateID=<?php echo $row['id'];?>&sluzbaTyp=pedikúra&cas=<?php echo $row['cas_zacatek'];?>'>></a>
                                                <?php
                                                } ?>
                                                <?php "
                                            </td>
                                            
                                        </tr>
                                    ";
                                }
                            }
                            else{
                                echo "<tr class='w-full' ><td colspan='4' class='text-center my-16  text-lg py-10 w-full'>- žádné rezervace -<td><tr>";
                            }
                        }
                    }
                ?>
            </table>
            <?php createPaginationMenu(10, "pedikura.php", $sql); ?>
        </section>





    <script src="../scriptKadernictvi.js"></script>
    <script src="scriptAdmin.js"></script>
</body>
</html>