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

    filename:    foodaddupdate.php
    description: add food to foods database table or updates food in foods database
    inputs:      foodId
    outputs:     foodId and associated parameters
    actions:     add, update, cancel
    redirection: foodaddupdateprocess.php

********************************************************************************
-->
<html>
    <head>
        <?php include 'head.html' ?>
        <script src = "navigation.js"></script>
    </head>
    <body>
        <?php include 'navigationmanagefoodspitsprobes.html' ?>
        <?php
            $action = $_GET["action"];
            if ($action == "add") {
                echo "<h3>ADD FOOD</h3>";
            } elseif ($action == "update") {
                echo "<h3>UPDATE FOOD</h3>";
            }
        ?>
        <div id="content">
            <?php
/*
            ... connect to database
*/
            include 'functions.php';
            $conn = connectToDB();
/*
            ... initialize food parameters
*/
            $foodId = 0;
            $food = "";
            $doneness = "";
            $externalTemp = 0;
            $externalHighTemp = 0;
            $externalLowTemp = 0;
            $internalTemp = 0;
            $internalHighTemp = 0;
            $internalLowTemp = 0;
            $comments = "";
            $donenesses = array("Extra Rare", "Rare", "Medium Rare", "Medium", "Medium Well", "Well Done", "Overcooked");
/*
            ... get action
*/
            $action = $_GET["action"];
            if ($action == "add") {
            } elseif ($action == "update") {
                $foodId = $_GET["foodId"];
                if ($foodId == "") {
                    echo "no food selected<br>";
                } else {
/*
            ... Get food
*/
                    $sql = "SELECT food, doneness, externalTemp, externalHighTemp, externalLowTemp,
                       internalTemp, internalHighTemp, internalLowTemp, comments FROM foods 
                       WHERE foodId = $foodId";
                    $result = mysqli_query($conn, $sql);
                    if (mysqli_num_rows($result) > 0) {
                        $row = mysqli_fetch_assoc($result);
                        $food = $row[food];
                        $doneness = $row[doneness];
                        $externalTemp = $row[externalTemp];
                        $externalHighTemp = $row[externalHighTemp];
                        $externalLowTemp = $row[externalLowTemp];
                        $internalTemp = $row[internalTemp];
                        $internalHighTemp = $row[internalHighTemp];
                        $internalLowTemp = $row[internalLowTemp];
                        $comments = $row[comments];
                    }
                }
           }
/*
           ... display form
*/
           echo '<form action="foodaddupdateprocess.php?foodId=' . $foodId . '" method="post">';
                echo "<table>";
                echo "<tr>";
                echo "<th>Food</th>";
                echo "<th>";
                echo '<input type="text" name="food" size="30" value="' . $food . '"><br>';
                echo "</th>";
                echo "</tr>";
                echo "<tr>";
                echo "<th>Doneness</th>";
                echo "<th>";
                echo '<select name="doneness">';
                foreach ($donenesses as $d) {
                    if ($d == $doneness) {
                        echo '<option value="' . $d . '" selected>' . $d . '</option>';
                    } else {
                        echo '<option value="' . $d . '">' . $d . '</option>';
                    }
                }
                echo '</select>';
                echo "</th>";
                echo "</tr>";
                echo "<tr>";
                echo "<th>External Temp</th>";
                echo "<th>";
                echo '<select name="externalTemp">';
                    echo '<option value="0">0</option>';
                    for($i = 200; $i <=700; $i+=5) {
                        if ($i == $externalTemp) {
                            echo '<option value=' . $i . ' selected>' . $i . '</option>';
                        } else {
                           echo '<option value=' . $i . '>' . $i . '</option>';
                        }
                    }
                echo '</select>'; 
                echo "</th>";
                echo "</tr>";
                echo "<tr>";
                echo "<th>External HighTemp</th>";
                echo "<th>";
                echo '<select name="externalHighTemp">';
                    echo '<option value="0">0</option>';
                    for($i = 200; $i <=700; $i+=5) {
                        if ($i == $externalHighTemp) {
                            echo '<option value=' . $i . ' selected>' . $i . '</option>';
                        } else {
                            echo '<option value=' . $i . '>' . $i . '</option>';
                        }
                    }
                echo '</select>'; 
                echo "</th>";
                echo "</tr>";
                echo "<tr>";
                echo "<th>External Low Temp</th>";
                echo "<th>";
                echo '<select name="externalLowTemp">';
                    echo '<option value="0">0</option>';
                for($i = 200; $i <=700; $i+=5) {
                    if ($i == $externalLowTemp) {
                        echo '<option value=' . $i . ' selected>' . $i . '</option>';
                    } else {
                        echo '<option value=' . $i . '>' . $i . '</option>';
                    }
                }
                echo '</select>'; 
                echo "</th>";
                echo "</tr>";
                echo "<tr>";
                echo "<th>Internal Temp</th>";
                echo "<th>";
                echo '<select name="internalTemp">';
                    echo '<option value="0">0</option>';
                for($i = 125; $i <=165; $i+=5) {
                    if ($i == $internalTemp) {
                        echo '<option value=' . $i . ' selected>' . $i . '</option>';
                    } else {
                        echo '<option value=' . $i . '>' . $i . '</option>';
                    }
                }
                echo '</select>'; 
                echo "</th>";
                echo "</tr>";
                echo "<tr>";
                echo "<th>Internal High Temp</th>";
                echo "<th>";
                echo '<select name="internalHighTemp">';
                    echo '<option value="0">0</option>';
                for($i = 125; $i <=165; $i+=5) {
                    if ($i == $internalHighTemp) {
                        echo '<option value=' . $i . ' selected>' . $i . '</option>';
                    } else {
                        echo '<option value=' . $i . '>' . $i . '</option>';
                    }
                }
                echo '</select>'; 
                echo "</th>";
                echo "</tr>";
                echo "<tr>";
                echo "<th>Internal Low Temp</th>";
                echo "<th>";
                echo '<select name="internalLowTemp">';
                    echo '<option value="0">0</option>';
                for($i = 125; $i <=165; $i+=5) {
                    if ($i == $internalLowTemp) {
                        echo '<option value=' . $i . ' selected>' . $i . '</option>';
                    } else {
                        echo '<option value=' . $i . '>' . $i . '</option>';
                    }
                }
                echo '</select>'; 
                echo "</th>";
                echo "</tr>";
                echo "<tr>";
                echo "<th>Comments</th>";
                echo "<th>";
                echo '<input type="text" name="comments" size="30" value="' . $comments . '"><br>';
                echo "</th>";
                echo "</tr>";
                echo "</table>";
/*
                ... configure buttons
*/
                if ($action == "add") {
                    echo '<input type="submit" name="add" value="Add">';
                } elseif ($action == "update" && $foodId != "") {
                    echo '<input type="submit" name="update" value="Update">';
                }
                echo '<input type="submit" name="cancel" value="Cancel"><br>';
            echo '</form>';
/*
            ... close database connection
*/
            mysqli_close($conn);
        ?>
        </div>
    </body>
</html>