<?php
    include 'prijava/connect.php';
    define('json', TRUE);
    include 'json.php';
    if(isset($_POST['day'])) {
        $ipa = $_POST['ipa'];
        $japa = $_POST['japa'];
        $day = $_POST['day'];
        $name = $_POST['name'];
        $menu = $_POST['menu'];

        $orderChange = 0;
        $query = "SELECT * FROM orders WHERE user = '$name' AND date = '$day'";
        $result = mysqli_query($dbc, $query);
        if(mysqli_num_rows($result) == 1) {
            $orderChange = 1;
        }
        
        $orderDuplicate = 0;
        $queryOrdered = "SELECT * FROM orders WHERE user = '$name' AND date = '$day' AND menu = '$menu'";
        $resultOrdered = mysqli_query($dbc, $queryOrdered);
        if(mysqli_num_rows($resultOrdered) == 1) {
            $orderDuplicate = 1;
        } 
        
        $orderDone = 0;
        $queryDone = "SELECT * FROM complete WHERE date = '$day' AND finish = 1";
        $resultDone = mysqli_query($dbc, $queryDone);
        if(mysqli_num_rows($resultDone) == 1) {
            $orderDone = 1;
        } ?>

        <div class="modal_header">
            <div class="modal_header-img">
                <img src="<?php echo $decodedData[$ipa]["foods"][$japa]["image"]; ?>" loading="lazy" />
            </div>
            <div class="modal_header-detail">
                <h4><?php echo $decodedData[$ipa]["foods"][$japa]["title"]; ?></h4>
                <h5>MENU <?php echo $decodedData[$ipa]["foods"][$japa]["menu"]; ?></h5>
                <p> <?php echo $decodedData[$ipa]["foods"][$japa]["description"]; ?></p>
                <div class="food-info">
                    <?php
                        if($decodedData[$ipa]["foods"][$japa]["alcoholTag"] == true) {
                            echo '<div class="food-info_box">';
                            echo '<img src="./assets/alcohol.svg" />';
                            echo '<p>Sadrži alkohol</p>';
                            echo '</div>';
                        }
                        if($decodedData[$ipa]["foods"][$japa]["spicyTag"] == true) {
                            echo '<div class="food-info_box">';
                            echo '<img src="./assets/spicy.svg" />';
                            echo '<p>Blago ljuto</p>';
                            echo '</div>';
                        }
                        if($decodedData[$ipa]["foods"][$japa]["vegeTag"] == true) {
                            echo '<div class="food-info_box">';
                            echo '<img src="./assets/vege.svg" />';
                            echo '<p>Vege obrok</p>';
                            echo '</div>';
                        }
                        if($decodedData[$ipa]["foods"][$japa]["servedColdTag"] == true) {
                            echo '<div class="food-info_box">';
                            echo '<img src="./assets/cold.svg" />';
                            echo '<p>Hladno</p>';
                            echo '</div>';
                        }
                    ?>
                </div>
            </div>
        </div>
        <div class="modal_buttons">
            <button class="close-modal">ZATVORI</button>
            <?php if($orderDone == 1) { ?>
                <button class="order-done-btn">NARUČI
                <span class="tooltiptext">Narudžba je završena.</span>
                </button>
            <?php } else if($orderDuplicate == 1) { ?>
                <button onclick='menuOrderDuplicate()'>NARUČI</button>
            <?php } else if($orderChange == 0) { ?>
                <button onclick='menuOrder("<?php echo $name; ?>", "<?php echo $day; ?>", "<?php echo $decodedData[$ipa]["foods"][$japa]["menu"]; ?>")'>NARUČI</button>
            <?php } else { ?>
                <button onclick='menuOrderChange("<?php echo $name; ?>", "<?php echo $day; ?>", "<?php echo $decodedData[$ipa]["foods"][$japa]["menu"]; ?>")'>NARUČI</button>
            <?php } ?>
        </div>

        <?php }
?>

<script src="js/modal.js"></script>
<script src="js/menu-order.js"></script>