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

    if (isset($_GET["updateID"])){
        $updateID = $_GET["updateID"];
    }
    
    $sluzbaTyp = getSluzbaTyp();


    $stmt = $conn->prepare('select * from '.$sluzbyTable);
    $sluzbyID = array();
    $sluzbyNazev = array();
    $sluzbyCas = array();
    if($stmt->execute()){
        $result = $stmt->get_result();
        if($result->num_rows>0){
            while($row = $result->fetch_assoc()){
                $sluzbyID[] = $row['id_sluzby'];
                $sluzbyNazev[] = $row['nazev'];
                $sluzbyCas[] = $row['cas'];
            }

            $stmt->close();
        }
    }



    if (count($_POST)>0){
        $jmeno = $_POST['jmeno'];
        $email = $_POST['email'];
        $telefon = $_POST['telefon'];
        $datum = $_POST['datum'];
        $sluzba = $_POST['sluzba'];
        $cas = $_POST['cas'];
        $sluzbaID;

        $counter=0;


        mysqli_query($conn,
            "UPDATE $rezervaceTable 
            set jmeno='$jmeno', email='$email', telefon='$telefon' 
            WHERE id='$updateID'");

        $dateCz=date("d. m. Y",strtotime($datum));

        $message = "<p class='bg-green-400 lg:rounded-full text-center py-2 '> Úspěšně změněno</p>";

        if (isset($_POST["changeTimeSubmit"])) {

            $sql = "SELECT *  FROM $rezervaceTable INNER JOIN $sluzbyTable ON $rezervaceTable.id_sluzby=$sluzbyTable.id_sluzby WHERE id=$updateID";
            $result = mysqli_query($conn, $sql);
        
            $row = mysqli_fetch_array($result);

            $sluzbaNazev = $row['nazev'];
            $id = $row['id'];
            $id_sluzba = $row['id_sluzby'];
            echo"<script language='javascript'>
                window.location.href = 'vyberCasu/vyberDatumu.php?id=".$id."&jmeno=".$jmeno."&email=".$email."&telefon=".$telefon."&sluzbaTyp=".$sluzbaTyp."&sluzba=".$sluzbaNazev."&id_sluzba=".$id_sluzba."';
            </script>
            ";

        }
        elseif (isset($_POST["changeServiceSubmit"])) {

            $sql = "SELECT *  FROM $rezervaceTable INNER JOIN $sluzbyTable ON $rezervaceTable.id_sluzby=$sluzbyTable.id_sluzby WHERE id=$updateID";
            $result = mysqli_query($conn, $sql);
        
            $row = mysqli_fetch_array($result);

            $sluzbaNazev = $row['nazev'];
            $id = $row['id'];
            $id_sluzba = $row['id_sluzby'];
            echo"<script language='javascript'>
                window.location.href = 'vyberCasu/vyberSluzby.php?id=".$id."&jmeno=".$jmeno."&email=".$email."&telefon=".$telefon."&sluzbaTyp=".$sluzbaTyp."&sluzba=".$sluzbaNazev."&id_sluzba=".$id_sluzba."';
            </script>
            ";

        }
        else{
            sendMail($jmeno,$dateCz,$cas,$sluzbaTyp,$sluzba,$email,"change");
        }

    }


    $sql = "SELECT *  FROM $rezervaceTable INNER JOIN $sluzbyTable ON $rezervaceTable.id_sluzby=$sluzbyTable.id_sluzby WHERE id=$updateID";
    $result = mysqli_query($conn, $sql);

    $row = mysqli_fetch_array($result);
    


 $conn->close();
?>




















<!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>Update rezervace</title>
    <link rel="stylesheet" href="../styleKadernictvi.css">
    <link rel="stylesheet" href="styleAdmin.css">
</head>
<body class="bg-white">

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


    <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="">MANIKÚRA</a>             
            <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-kontakt-phone border-gray-300 font-bold" href="">PEDIKÚRA</a>
            <a class="block text-2xl mx-auto navBtnPhone phoneNavLine nav-kontakt-phone border-b border-gray-300 font-bold" href="users.php">UŽIVATELÉ</a>
        </div>
    </nav>


<article>   
    

    <header class="updateReservationHeader">
        <h1>Upravit rezervaci</h1>
        <p>Číslo rezervace: <?php echo  $row["id"] ?></p>
    </header>

    <form class="updateForm" name="updateForm" method="post" action="">
        <?php
            if (isset($message)) {echo $message;}
        ?>

        <div>
            <label for="jmeno">Jméno a příjmení</label>
            <input required type="text" id="jmeno" name="jmeno" value="<?php echo $row['jmeno']; ?>">
        </div>

        <div>
            <label for="email">Email</label>
            <input required type="email" id="email" name="email" value="<?php echo $row['email']; ?>">
        </div>

        <div>
            <label for="telefon">Telefon</label>
            <input required type="tel" id="telefon" name="telefon"  pattern="[0-9]{9}" value="<?php echo $row['telefon']; ?>">
        </div>

        <div>
            <label for="datum">Služba</label>
            <?php $sluzba = $row['nazev']; ?>
            <input required readonly type="text" name="sluzba" id="sluzba" value="<?php echo $sluzba; ?>">
            <input class="myBtn" type="submit" name="changeServiceSubmit" value="Změnit službu">
        </div>

        <div>
            <?php $newDate = date("d-m-Y", strtotime($row["datum"])); ?>
            <label for="datum">Datum</label>
            <input required readonly type="text" name="datum" id="datum" value="<?php echo $newDate; ?>">

            <label for="cas" class="mt-4">Čas</label>
            <?php $casZacatek = $row['cas_zacatek']; ?>
            <input required readonly type="text" name="cas" id="cas" value="<?php echo $casZacatek?>">   
            <input class="myBtn" type="submit" name="changeTimeSubmit" value="Změnit čas">

        </div>
        
        

        <div class="text-left">
        <input class=" mx-auto w-auto myBtn btnGreen" type="submit" id="submit" name="submit" value="Uložit úpravy"/>
        </div>
        
    </form>       
    
</article>
    
<script src="../scriptKadernictvi.js"></script>
</body>
</html>