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

    filename:    pitaddupdate.php
    description: add pit to pit database table or
                 update pit in pit database table
    inputs:      pitId, add, delete
    outputs:     pitId, add, delete, pit associated parameters
    actions:     add, update, cancel
    redirection: pitaddupdateprocess.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 PIT</h3>";
            } elseif ($action == "update") {
                echo "<h3>UPDATE PIT</h3>";
            }
        ?>
        <div id="content">
            <?php
/*
                ... connect to database
*/
                include 'functions.php';
                $conn = connectToDB();
/*
            ... initialize pit parameters
*/
            $pitId = 0;
            $manufacturer = "";
            $model = "";
            $blowerP = 0;
            $blowerI = 0;
            $blowerD = 0;
            $ventP = 0;
            $ventI = 0;
            $ventD = 0;
            $comments = "";
/*
            ... get action
*/
            $action = $_GET["action"];
            if ($action == "add") {
            } elseif ($action == "update") {
                $pitId = $_GET["pitId"];
                if ($pitId == "") {
                    echo "no pit selected<br>";
                } else {
/*
            ... Get pit
*/
                    $sql = "SELECT manufacturer, model, blowerP, blowerI, blowerD,
                       ventP, ventI, ventD, comments FROM pits 
                       WHERE pitId = $pitId";
                    $result = mysqli_query($conn, $sql);
                    if (mysqli_num_rows($result) > 0) {
                        $row = mysqli_fetch_assoc($result);
                        $manufacturer = $row[manufacturer];
                        $model = $row[model];
                        $blowerP = $row[blowerP];
                        $blowerI = $row[blowerI];
                        $blowerD = $row[blowerD];
                        $ventP = $row[ventP];
                        $ventI = $row[ventI];
                        $ventD = $row[ventD];
                        $comments = $row[comments];
                    }
                }
           }
/*
                .... generate form to add/update pit
*/
                echo '<form action="pitaddupdateprocess.php?pitId=' . $pitId . '" method="post">';
                    echo "<table>";
                        echo "<tr>";
                            echo "<th>Manufacturer</th>";
                            echo '<th><input type="text" name="manufacturer" size="30" value="' . $manufacturer . '"><th>';
                        echo "</tr>";
                        echo "<tr>";
                            echo "<th>Model</th>";
                            echo '<th><input type="text" name="model" size="30" value="' . $model . '"></th>';
                        echo "</tr>";
                        echo "<tr>";
                            echo "<th>Blower P</th>";
                            echo "<th>";
                                echo '<select name="blowerP">';
                                    for($i = 0; $i <= 100; $i++) {
                                       if ($i == $blowerP) {
                                           echo '<option value=' . $i . ' selected>' . $i . '</option>';
                                       } else {
                                           echo '<option value=' . $i . '>' . $i . '</option>';
                                       }
                                    }
                                echo '</select>';   
                            echo '</th>';
                        echo "</tr>";
                        echo "<tr>";
                            echo "<th>Blower I</th>";
                            echo "<th>";
                                echo '<select name="blowerI">';
                                    for($i = 0; $i <= 100; $i++) {
                                       if ($i == $blowerI) {
                                           echo '<option value=' . $i . ' selected>' . $i . '</option>';
                                       } else {
                                           echo '<option value=' . $i . '>' . $i . '</option>';
                                       }
                                    }
                                echo '</select>';   
                            echo '</th>';
                        echo "</tr>";
                        echo "<tr>";
                            echo "<th>Blower D</th>";
                            echo "<th>";
                                echo '<select name="blowerD">';
                                    for($i = 0; $i <= 100; $i++) {
                                       if ($i == $blowerD) {
                                           echo '<option value=' . $i . ' selected>' . $i . '</option>';
                                       } else {
                                           echo '<option value=' . $i . '>' . $i . '</option>';
                                       }
                                    }
                               echo '</select>';   
                            echo '</th>';
                        echo "</tr>";
                        echo "<tr>";
                            echo "<th>Vent P</th>";
                            echo "<th>";
                                echo '<select name="ventP">';
                                    for($i = 0; $i <= 100; $i++) {
                                       if ($i == $ventP) {
                                           echo '<option value=' . $i . ' selected>' . $i . '</option>';
                                       } else {
                                           echo '<option value=' . $i . '>' . $i . '</option>';
                                       }
                                    }
                               echo '</select>';   
                            echo '</th>';
                        echo "</tr>";
                        echo "<tr>";
                            echo "<th>Vent I</th>";
                            echo "<th>";
                                echo '<select name="ventI">';
                                    for($i = 0; $i <= 100; $i++) {
                                       if ($i == $ventI) {
                                           echo '<option value=' . $i . ' selected>' . $i . '</option>';
                                       } else {
                                           echo '<option value=' . $i . '>' . $i . '</option>';
                                       }
                                    }
                            echo '</th>';
                        echo "</tr>";
                        echo "<tr>";
                            echo "<th>Vent D</th>";
                            echo "<th>";
                                echo '<select name="ventD">';
                                    for($i = 0; $i <= 100; $i++) {
                                       if ($i == $ventD) {
                                           echo '<option value=' . $i . ' selected>' . $i . '</option>';
                                       } else {
                                           echo '<option value=' . $i . '>' . $i . '</option>';
                                       }
                                    }
                            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" && $pitId != "") {
                    echo '<input type="submit" name="update" value="Update">';
                }
                    echo '<input type="submit" name="cancel" value="Cancel"><br>';
                echo '</form>';
            mysqli_close($conn);
        ?>
    </div>
    </body>
</html>