<?php
    date_default_timezone_set("Europe/Zagreb");
    setlocale(LC_TIME,"hr_HR");
    defined('add-order') or header('Location: .././');

    $added = new DateTime(date("Y-m-d H:i:s"));
    $added = $added->format('Y-m-d H:i:s');

    $day1 = new DateTime(date("d.m.Y."));
    $day2 = new DateTime(date("d.m.Y."));
    $day2->modify('+1 day');

    if ($day1->format('D') == "Fri") {
        $day2->modify('+2 day');
    } else if($day1->format('D') == "Sat") {
        $day1->modify('+2 day');
        $day2->modify('+2 day');
    }

    if ($day1->format('D')=="Mon") {
        $day1_day = "Ponedjeljak"; 
    } else if ($day1->format("D") == "Tue") {
        $day1_day = "Utorak"; 
    } else if ($day1->format("D") == "Wed") {
        $day1_day = "Srijeda"; 
    } else if ($day1->format("D") == "Thu") {
        $day1_day = "Četvrtak"; 
    } else if ($day1->format("D") == "Fri") {
        $day1_day = "Petak"; 
    }

    if ($day2->format('D')=="Mon") {
        $day2_day = "Ponedjeljak"; 
    } else if ($day2->format("D") == "Tue") {
        $day2_day = "Utorak"; 
    } else if ($day2->format("D") == "Wed") {
        $day2_day = "Srijeda"; 
    } else if ($day2->format("D") == "Thu") {
        $day2_day = "Četvrtak"; 
    } else if ($day2->format("D") == "Fri") {
        $day2_day = "Petak"; 
    }

    $day1_format = $day1->format('Y-m-d');
    $day1_check = 0;
    $day2_format = $day2->format('Y-m-d');
    $day2_check = 0;

    $query = "SELECT * FROM complete WHERE date = '$day1_format'";
    $result = mysqli_query($dbc, $query);
    while($row = mysqli_fetch_array($result)) {
        if($row['finish'] == '1') {
            $day1_check = 1;
        }
    }
    $query = "SELECT * FROM complete WHERE date = '$day2_format'";
    $result = mysqli_query($dbc, $query);
    while($row = mysqli_fetch_array($result)) {
        if($row['finish'] == '1') {
            $day2_check = 1;
        }
    }
?>
<div class="fader" onclick="openAddOrder()"></div>
<form class="add-order-modal" enctype="multipart/form-data" method="POST" action="">
    <h3>Dodaj narudžbu<span onclick="openAddOrder()">X</span></h3>
    <p>Odaberi korisnika</p>
    <select name="user" required>
        <option></option>
        <?php
            $query = "SELECT * FROM user";
            $result = mysqli_query($dbc,$query);
            while($row = mysqli_fetch_array($result)) {
                echo "<option value='".$row['name']."'>".$row['name']."</option>";
            }
        ?>
    </select>

    <p>Odaberi dan</p>
    <select name="day" id="day" required>
        <option value="<?php echo $day1->format('Y-m-d'); ?>"><?php echo "".$day1_day.", ".$day1->format('d.m.Y.').""; ?></option>
        <option value="<?php echo $day2->format('Y-m-d'); ?>"><?php echo "".$day2_day.", ".$day2->format('d.m.Y.').""; ?></option>
    </select>

    <p>Odaberi menu</p>
    <select name="menu" required>
        <option></option>
        <option value="A">MENU A</option>
        <option value="B">MENU B</option>
        <option value="C">MENU C</option>
        <option value="D">MENU D</option>
        <option value="E">MENU E</option>
    </select>
    <input type="submit" name="add-order-submit" value="DODAJ">
</form>

<?php
    if ($dbc && isset($_POST['add-order-submit'])) {
        $ime = $_POST['user'];
        $datum = $_POST['day'];
        $menu = $_POST['menu'];
        $completed = 1;

        $query = "SELECT * FROM orders WHERE date = '$datum' AND user = '$ime'";
        $result = mysqli_query($dbc,$query);
        if (mysqli_num_rows($result) == 0) {
            $query = "INSERT INTO orders (user, date, menu, completed, added) VALUES ('$ime', '$datum', '$menu', '$completed', '$added')";
            $result = mysqli_query($dbc,$query) or die('Error querying database.');
            echo '<script>';
            echo "Swal.fire(
                'Naručeno!',
                'Narudžba je dodana.',
                'success',
                ).then(function() {
                    window.location.href = './';
                });";
            echo '</script>';
        } else {
            $query = "UPDATE orders SET menu = '$menu' WHERE date = '$datum' AND user = '$ime'";
            $result = mysqli_query($dbc,$query) or die('Error querying database.');
            echo '<script>';
            echo "Swal.fire(
                'Ažurirano!',
                'Narudžba je ažurirana.',
                'success',
                ).then(function() {
                    window.location.href = './';
                });";
            echo '</script>';
        }
       
    }
?>