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



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

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


    $selected = "";
        
        function vytvorit_kalendar($mesic, $rok){
        include "../connection.php";
        
        global $id_typSluzby;

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

                $stmt->close();
            }
        }

        global $sluzba;
        global $sluzbaId;
        global $sluzbaTyp;

        $dnyVTydnu = array('Neděle','Pondělí','Úterý','Středa','Čtvrtek','Pátek','Sobota');
        $prvniDenMesice = mktime(0,0,0,$mesic,1,$rok);
        $pocetDnu = date("t",$prvniDenMesice);
        $dateComponents =  getdate($prvniDenMesice);
        $nazevMesice = $dateComponents['month'];
        $denVTydnu = $dateComponents['wday'];
        $dnesniDatum = date('Y-m-d');

        $predchoziMesic = date('m', mktime(0,0,0,$mesic-1, 1,$rok));
        $predchoziRok = date('Y', mktime(0,0,0,$mesic-1, 1,$rok));
        $nasledujiciMesic = date('m', mktime(0,0,0,$mesic+1, 1,$rok));
        $nasledujiciRok = date('Y', mktime(0,0,0,$mesic+1, 1,$rok));

        switch ($mesic) {
            case 1:
                $monthName = "Leden";
                break;
            case 2:
                $monthName = "Únor";
                break;
            case 3:
                $monthName = "Březen";
                break;
            case 4:
                $monthName = "Duben";
                break;
            case 5:
                $monthName = "Květen";
                break;
            case 6:
                $monthName = "Červen";
                break;
            case 7:
                $monthName = "Červenec";
                break;
            case 8:
                $monthName = "Srpen";
                break;
            case 9:
                $monthName = "Září";
                break;
            case 10:
                $monthName = "Říjen";
                break;
            case 11:
                $monthName = "Listopad";
                break;
            case 12:
                $monthName = "Prosinec";
                break;      
            default:
                
                break;
        }

        //TVORBA TABULKY::
        $kalendar ="
        
        <div class='headerContainer'>
        <h1>Vyberte datum</h1>
            <div class=' flex flex-row justify-center items-center gap-4'>
                <a class='myButton monthArrowButton font-bold text-lg' href='?month=".$predchoziMesic."&year=".$predchoziRok."&sluzba=".$sluzba."&sluzbaTyp=".$sluzbaTyp."&id_sluzba=".$sluzbaId."'><</a>
                <h2>$monthName $rok</h2>
                <a class='myButton monthArrowButton font-bold text-lg' href='?month=".$nasledujiciMesic."&year=".$nasledujiciRok."&sluzba=".$sluzba."&sluzbaTyp=".$sluzbaTyp."&id_sluzba=".$sluzbaId."'>></a>
            </div>
        </div>
        
        ";
        
        if(isset($_POST['sluzby']))
        {
            $selected = $_POST['sluzby'];
        }
        else{
            $selected = '';
        }


        $kalendar.= "<table>";

        $kalendar.="<tr>";

        foreach($dnyVTydnu as $den){
            $kalendar.="<th class='header py-4'>$den</th>";
        }

        $kalendar.="</tr>";
        $kalendar.="<tr>";

        $currentDay = 1;
        if($denVTydnu > 0){
            for ($k=0; $k<$denVTydnu; $k++) { 
                $kalendar.="<td class='empty'></td>";
            }
        }

        //ZÍSKÁNÍ ČÍSLA MĚSÍCE
        $mesic = str_pad($mesic, 2, "0", STR_PAD_LEFT);

        while($currentDay <= $pocetDnu){

            //Po 7. dni vytvořit nový řádek
            if($denVTydnu == 7){
                $denVTydnu = 0;
                $kalendar.="</tr><tr>";
            }
            

            $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
            $date = "$rok-$mesic-$currentDayRel";
            $nazevDne = strtolower(date('I', strtotime($date)));
            $dnes = $date==date('Y-m-d') ? 'dnes': '';
            $monthNumber = $mesic * 1;

            if($date<=date('Y-m-d')){
                $kalendar.="<td class='$dnes'><h4 class='mb-2'>$currentDay. $monthNumber.</h4> <a class=' px-2 py-1 text-sm rounded-md p-2'>---</a></td>";
            }
            else{
                $kalendar.="<td class='$dnes'><h4 class='mb-2'>$currentDay. $monthNumber.</h4> <a href='vyberCasu.php?date=$date&sluzbaTyp=$sluzbaTyp&sluzba=$sluzba&id_sluzba=$sluzbaId' class=' succesButton px-2 py-1 text-sm rounded-md'><span class='hidden lg:inline-block'>rezervovat</span><span class='lg:hidden p-2'>➜</span></a></td>";
            }

            

            


            $currentDay++;
            $denVTydnu++;


        }

        if ($denVTydnu < 7) {
            $zbyvajiciDny = 7-$denVTydnu;
            for ($i=0; $i < $zbyvajiciDny; $i++) { 
                $kalendar.="<td></td>";
            }
        }

        $kalendar.="</tr>";
        $kalendar.="</table>";

        return $kalendar;
    }

    function vytvorit_options($select){
        global $sluzbyNazev;

         $options='';

        
         foreach($sluzbyNazev as $sluzba){

            if ($select==$sluzba) {
                $options.='<option value="'.$sluzba.'" selected>'.$sluzba.'</option>';
            }
            else{
                $options.='<option value="'.$sluzba.'">'.$sluzba.'</option>';
            }
             
         }

         return $options;
    }

    if(isset($_POST['sluzby']))
    {
        $selected = $_POST['sluzby'];
    }



    $conn->close();
?>























<!DOCTYPE html>
<html lang="cs">
<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="../styleKadernictvi.css">
    <link rel="stylesheet" href="styleRezervace.css">
    <title>Document</title>
</head>
<body class="calendarBody">
    <header class="headerRezervace">
        <h2>Kadeřnictví Hrdlořezy - <b>REZERVACE</b></h2>
    </header>
    <div class="calendarContainer">
                <div>
                    <h3><?php echo($sluzbaTyp." | ".$sluzba);?></h3>
                </div>

                <?php
                    $dateComponents = getdate();

                    if(isset($_GET['month']) && isset($_GET['year'])){
                        $mesic = $_GET['month'];
                        $rok = $_GET['year'];
                    }else{
                        $mesic = $dateComponents['mon'];
                        $rok = $dateComponents['year'];
                    }
                    echo vytvorit_kalendar($mesic, $rok);
                        

                    
                ?>
                <input type="text" />

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

<script>
    $(document).ready(function(){
    $("#my_select").change(function(){

        var thisval = $(this).val();    
        $("input").val(thisval);
    });
    });
</script>

</body>
</html>