<?php
include "../connection.php";
include "../function.php";
include "../casoveSloty.php";

    $sluzba = getSluzba();
    $sluzbaTyp = getSluzbaTyp();
    $id_typSluzby = getTypSluzbaId();


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



    if (isset($_GET['date'])) {
        $datum = $_GET['date'];
        $stmt = $conn->prepare('select * from '.$rezervaceTable.' inner join '.$sluzbyTable.' on '.$rezervaceTable.'.id_sluzby='.$sluzbyTable.'.id_sluzby where datum = ? AND '.$sluzbyTable.'.id_typSluzby ='.$id_typSluzby);
        $stmt->bind_param('s',$datum);
        $rezervace = array();
        if($stmt->execute()){
            $result = $stmt->get_result();
            if($result->num_rows>0){
                while($row = $result->fetch_assoc()){
                    $rezervace[] = $row['casove_sloty'];
                }

                $stmt->close();
            }
        }
    }

    if(isset($_POST['submit'])){
        
        $name = $_POST['name'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $cas = $_POST['cas'];
        $cas_zacatek = substr($cas, 0, 5);
        $cas_konec = $_POST['cas_konec'];

        $stmt = $conn->prepare('select * from '.$rezervaceTable.' inner join '.$sluzbyTable.' on '.$rezervaceTable.'.id_sluzby='.$sluzbyTable.'.id_sluzby where datum = ? AND casove_sloty = ? AND '.$sluzbyTable.'.id_typSluzby ='.$id_typSluzby);
        $stmt->bind_param('ss',$datum, $cas);
        if($stmt->execute()){
            $result = $stmt->get_result();
            if($result->num_rows>0){
                $msg = "<div class='alert alert-danger'> Již zarezervováno</div>";
            }else{
                $stmt = $conn->prepare("INSERT INTO ".$rezervaceTable."(jmeno, email, telefon, datum, casove_sloty, cas_zacatek, id_sluzby) VALUES (?,?,?,?,?,?,?)");
                $stmt->bind_param('sssssss', $name, $email, $phone, $datum, $cas, $cas_zacatek, $sluzbaId);
                $stmt->execute();
                $msg = "<div class='alert alert-success'> Úspěšně zarezervováno</div>";
                $rezervace[]=$cas;
                $stmt->close();
                
                $dateCz=date("d. m. Y",strtotime($datum));
                sendMail($name,$dateCz,$cas_zacatek,$sluzbaTyp,$sluzba,$email,"confirm");
                
            }
        }

        
    }

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

            $stmt->close();
        }
    }

    $casSluzby;
    $counter = 0;
    foreach ($sluzbyNazev as $sluzbaNazev) {
        if($sluzbaNazev == $sluzba){
            $casSluzby = $sluzbyCas[$counter];           
        }

        $counter++;
        $sluzbaNazev;
    }

    $slotySluzby = ($casSluzby / $dobaTrvani);
    $pocetHodin = ($slotySluzby / 2);
    $dobaTrvaniHod;
    if($pocetHodin==0.5) { $dobaTrvaniHod = "30 minut";}
    elseif($pocetHodin==1) { $dobaTrvaniHod = $pocetHodin." hodina";}
    elseif($pocetHodin==2 OR $pocetHodin==3 OR $pocetHodin==4) { $dobaTrvaniHod = $pocetHodin." hodiny";}
    else{ $dobaTrvaniHod = $pocetHodin." hodin";}

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

    $rezervaceExplode=array();
    $counter=0;
    $rezervaceExplode=$rezervace;

    foreach ($rezervace as $jednaRezervace) {

            $x = explode(", ", $jednaRezervace);

            if (array_key_exists(0, $x))
            {
                $rezervaceExplode[$counter] = $x[0];
                $counter++;
            }
            
            if (array_key_exists(1, $x))
            {
                $rezervaceExplode[$counter] = $x[1];
                $counter++;
            }
            if (array_key_exists(2, $x))
            {
                $rezervaceExplode[$counter] = $x[2];
                $counter++;
            }
            if (array_key_exists(3, $x))
            {
                $rezervaceExplode[$counter] = $x[3];
                $counter++;
            }
            if (array_key_exists(4, $x))
            {
                $rezervaceExplode[$counter] = $x[4];
                $counter++;
            }
            
            if (array_key_exists(5, $x))
            {
                $rezervaceExplode[$counter] = $x[5];
                $counter++;
            }
            if (array_key_exists(6, $x))
            {
                $rezervaceExplode[$counter] = $x[6];
                $counter++;
            }
            if (array_key_exists(7, $x))
            {
                $rezervaceExplode[$counter] = $x[7];
                $counter++;
            }
        
    }

    $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">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="../styleKadernictvi.css">
    <link rel="stylesheet" href="styleRezervace.css">
    <title>Document</title>
</head>
<body class="userBody">

    <header class="headerRezervace">
        <h2>Kadeřnictví Hrdlořezy - <b>REZERVACE</b></h2>
    </header>
    

    <div class="casyContainer">
    <div class="potvrzeniRezervace hide">
        <div>
            <h2>Úspěšně zarezervováno</h2>
            <a href="../">Zpět</a>
        </div>
    </div>
    <div>
        <h3><?php echo($sluzbaTyp."  |  ".$sluzba."  |  ".date('d-m-Y',strtotime($_GET['date'])));?></h3>
    </div>

    <h1 class="mb-16">Vyberte čas</h1>


    <?php 

        if (isset($msg)) {
            ?>
                <script>
                    const potvrzeniRezervace = document.querySelector(".potvrzeniRezervace")
                    potvrzeniRezervace.classList.remove("hide")

                </script>
            <?php
        }

    ?>

    <div class=" px-4 grid grid-cols-2 gap-8 lg:grid-cols-5 lg:gap-8 md:grid-cols-3">
        
        <?php 
            $casy = casy($dobaTrvani, $vymazani, $zacatek, $konec);
            $counter = 0;
            $countButtons=0;
            foreach($casy as $slot){
        ?>  

            <?php 
                /* PRO 30 MIN SLUŽBY */
                if($slotySluzby==1){
                    if(array_key_exists($counter, $casy)){
                        if(in_array($slot, $rezervaceExplode)){ 

                        }
                        else{ 
                            if(in_array($casy[$counter], $rezervaceExplode)){
                            }
                            else{
                                ?>
                                <button class="casButton py-4 place-self-stretch md:w-32  text-xl rezervovat " first-data-timeslot="<?php echo $slot ?>" data-timeslot="<?php echo $slot; ?>" last-data-timeslot="<?php echo $casy[$counter+1]; ?>"><?php echo $slot; ?></button>
                                <?php
                                $countButtons++;
                            }               
                        } 
                    }
                    else{
                    }
                }
                /* PRO 1 HOD SLUŽBY */
                if($slotySluzby==2){
                    if(array_key_exists($counter+1, $casy)){
                        if(in_array($slot, $rezervaceExplode)){ 

                        }
                        else{ 
                            if(in_array($casy[$counter+1], $rezervaceExplode)){
                            }
                            else{
                                ?>
                                <button class="casButton py-4 place-self-stretch md:w-32  text-xl rezervovat " first-data-timeslot="<?php echo $slot ?>" data-timeslot="<?php echo $slot.", ".$casy[$counter+1]; ?>" last-data-timeslot="<?php echo $casy[$counter+2]; ?>"><?php echo $slot; ?></button>
                                <?php
                                $countButtons++;
                            }               
                        } 
                    }
                    else{
                    }
                }
                /* PRO 2 HOD SLUŽBY */
                if($slotySluzby==4){
                    if(array_key_exists($counter+1, $casy) AND array_key_exists($counter+2, $casy) AND array_key_exists($counter+3, $casy)){
                        if(in_array($slot, $rezervaceExplode)){ 


                        }
                        else{ 
                            if(in_array($casy[$counter+1], $rezervaceExplode) OR in_array($casy[$counter+2], $rezervaceExplode) OR in_array($casy[$counter+3], $rezervaceExplode)){
                            }
                            else{
                                ?>
                                <button class="casButton py-4 place-self-stretch md:w-32  text-xl rezervovat " first-data-timeslot="<?php echo $slot ?>" data-timeslot="<?php echo $slot.", ".$casy[$counter+1].", ".$casy[$counter+2].", ".$casy[$counter+3]; ?>" last-data-timeslot="<?php echo $casy[$counter+4]; ?>"><?php echo $slot; ?></button>
                                <?php
                                $countButtons++;
                            }               
                        } 
                    }
                    else{
                    }
                }
                /* PRO 3 HOD SLUŽBY */
                if($slotySluzby==6){
                    if(array_key_exists($counter+1, $casy) AND array_key_exists($counter+2, $casy) AND array_key_exists($counter+3, $casy) AND array_key_exists($counter+4, $casy) AND array_key_exists($counter+5, $casy)){
                        if(in_array($slot, $rezervaceExplode)){ 

                            /* ?>
                            <button disabled class="rounded-xl bg-red-400 py-4 w-36 mx-auto text-xl"><?php echo $slot;?></button>   
                            <?php  */

                        }
                        else{ 
                            if(in_array($casy[$counter+1], $rezervaceExplode) OR in_array($casy[$counter+2], $rezervaceExplode) OR in_array($casy[$counter+3], $rezervaceExplode) OR in_array($casy[$counter+4], $rezervaceExplode) OR in_array($casy[$counter+5], $rezervaceExplode)){

                            }
                            else{
                                ?>
                                <button class="casButton py-4 place-self-stretch md:w-32  text-xl rezervovat " first-data-timeslot="<?php echo $slot ?>" data-timeslot="<?php echo $slot.", ".$casy[$counter+1].", ".$casy[$counter+2].", ".$casy[$counter+3].", ".$casy[$counter+4].", ".$casy[$counter+5]; ?>" last-data-timeslot="<?php echo $casy[$counter+6]; ?>"><?php echo $slot; ?></button>
                                <?php
                                $countButtons++;
                            }               
                        } 
                    }
                    else{

                    }
                    ?>
                    <?php
                }

            ?>
        
        <?php
            $counter++;
            }


        ?>

    </div>
    <?php
        if ($countButtons==0){
            ?>
                <div class="text-center text-2xl">Není volný termín</div>
            <?php
        }
    ?>
        
    </div>



    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content rounded-3xl lg:px-8">
            <div class="modal-header flex flex-col text-xl">
                <button type="button" class="close" data-dismiss="modal">&times;</button>

                <h2 class="modal-title text-2xl font-bold w-full mb-4">Shrnutí rezervace:<span id="slot"></span></h2>
                <h3><?php echo $sluzbaTyp;?></h3>
                <?php echo $sluzba." - ".$dobaTrvaniHod; ?>
                <span><?php echo $dateCz; ?></span>  
                <input disabled id="casZacatek" type="text" class="bg-white"> 


            </div>
            <div class="modal-body">
                <div>
                    <form class="userForm text-xl" action="" method="post">
                        <div class="timeInput">
                            <input required type="text" readonly name="cas" id="cas">
                            <input type="text" readonly name="cas_konec" id="cas_konec">
                        </div>

                        <div class="mt-96">
                            <label for="">Jméno</label>
                            <input required type="text" name="name">
                        </div>

                        <div>
                            <label for="">Email</label>
                            <input required type="email" name="email">
                        </div>

                        <div>
                            <label for="">Telefon</label>
                            <input required type="tel" name="phone">
                        </div>

                        <div>
                            <button type="submit" name="submit">POTVRDIT REZERVACI</button>
                        </div>
                    </form>
                </div>
                
            

        </div>
    </div>



    </div>
    
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<script>
    $(".rezervovat").click(function(){
        var cas = $(this).attr('data-timeslot');
        var casZacatek = $(this).attr('first-data-timeslot');
        var casKonec = $(this).attr('last-data-timeslot');
        $("#slot").html();
        $("#cas").val(cas);
        $("#cas_konec").val(casKonec);
        $("#casZacatek").val(casZacatek);
        $("#myModal").modal("show");
    })

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

</body>
</html>