<!DOCTYPE html>
<!--
********************************************************************************

    filename:     newburn.php
    descripton:
    inputs:       none
    outputs:      pitId, numberProbes, foodId, comments
    actions:      next, cancel
    redirections: newburnprocess.php

********************************************************************************
-->
<html>
    <head>
        <?php include 'head.html' ?>
        <script src = "navigation.js"></script>
    </head>
    <body>
        <?php include 'navigationmanageburns.html' ?>
        <div id="content">
        <h3>NEW BURN</h3>
        <?php
/*
            ... Connect to burnbot database
*/
            include 'functions.php';
            initCurrentProbes();
            $conn = connectToDB();
/*
            ... Get pits
*/
            $sql = "SELECT pitId, manufacturer, model FROM pits";
            $pitsresult = mysqli_query($conn, $sql);
            $pitIds = array("");
            $pitManufacturers = array("");
            $pitModels = array("");
            if (mysqli_num_rows($pitsresult) > 0) {
                while($row = mysqli_fetch_assoc($pitsresult)) {
                    array_push($pitIds, $row["pitId"]);
                    array_push($pitManufacturers, $row["manufacturer"]);
                    array_push($pitModels, $row["model"]);
                }
            } else {
                echo "No pits defined<br>";
            }
/*
            ... Get foods
*/
            $sql = "SELECT foodId, food, doneness FROM foods";
            $foodsresult = mysqli_query($conn, $sql);
            $foodIds = array("");
            $foods = array("");
            $donenesses = array("");
            if (mysqli_num_rows($foodsresult) > 0) {
                while($row = mysqli_fetch_assoc($foodsresult)) {
                    array_push($foodIds, $row["foodId"]);
                    array_push($foods, $row["food"]);
                    array_push($donenesses, $row["doneness"]);
                }
            } else {
                echo "No foods defined<br>";
            }
/*
        ... get current burn
*/
            $sql = "SELECT numberProbes, pitId FROM currentburn WHERE currentBurnId=1";
            $result = mysqli_query($conn, $sql);            
            $row = mysqli_fetch_assoc($result);
            $numberProbes = $row[numberProbes];
            if ($numberProbes == null) {
                $numberProbes = 2;
            }
            $pitId = $row[pitId];

            mysqli_close($conn);
/*
            ... determine if pit not selected
*/
            $pitRequired = "*";
            if ($_GET['pitRequired'] == 'true') {
                $pitRequired = '* Required';
            }
/*
            ... generate new burn input form
*/
            echo '<form action="newburnprocess.php" method="post">';
                echo "<table>";
                    echo "<tr>";
                        echo "<th>Pit</th>";
                        echo "<th>";
                            echo '<select name="pitId">';
                            for($i = 0; $i < count($pitIds); $i++) {
                                if ($pitIds[$i] == $pitId) {
                                    echo '<option value="' . $pitIds[$i] . '" selected>' . $pitManufacturers[$i] . ' ' . $pitModels[$i] . '</option>';
                                } else {
                                    echo '<option value="' . $pitIds[$i] . '">' . $pitManufacturers[$i] . ' ' . $pitModels[$i] . '</option>';
                                }
                            }
                            echo '</select>';   
                            echo '<span class="error">' . $pitRequired . '</span>';
                        echo '</th>';
                    echo "</tr>";
                    echo "<tr>";
                        echo "<th>Number Probes</th>";
                        echo "<th>";
                            echo '<select name="numberProbes">';
                            for($i = 1; $i <=8; $i++) {
                                if ($i == $numberProbes) {
                                    echo '<option value=' . $i . ' selected>' . $i . '</option>';
                                } else {
                                    echo '<option value=' . $i . '>' . $i . '</option>';
                                }
                            }
                            echo '</select>';   
                        echo '</th>';
                    echo "</tr>"; 
                    echo "<tr>";
                        echo "<th>Food/Doneness</th>";
                        echo "<th>";
                            echo '<select name="foodId">';
                            for($i = 0; $i < count($foodIds); $i++) {
                                echo '<option value="' . $foodIds[$i] . '">' . $foods[$i] . ' ' . $donenesses[$i] . '</option>';
                            }
                            echo '</select>';   
                        echo '</th>';
                    echo "</tr>";
                    echo "<tr>";
                        echo "<th>Comments</th>";
                        echo '<th>';
                        echo '<input type="text" name="comments" value="">';
                        echo '</th>';
                    echo '</tr>';
                echo "</table>";
                echo '<input type="submit" name="next" value="Next">';
                echo '<input type="submit" name="cancel" value="Cancel">';
            echo '</form>';
        ?>
    </div>
</body>
</html>