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

    filename:     newburnprobes.php
    description:  select number and types of probes for new burn
    inputs:       none
    outputs:      none
    actions:      next
    redirections: none 

********************************************************************************
-->
<html>
    <head>
        <?php include 'head.html' ?>
        <script src = "navigation.js"></script>
    </head>
    <body>
        <?php include 'navigationmanageburns.html' ?>
        <div id="content">
            <h3>NEW BURN PROBES</h3>
        <?php
            include 'functions.php';
            $conn = connectToDB();
/*
            ... Clear current probe settings
*/
/*
            for ($i = 1; $i <= 8; $i++) {
                $sql = "UPDATE currentprobes SET ProbeTemp = null, targetTemp = null, highTemp = null, lowTemp = null WHERE probePosition = $i";
                if (!mysqli_query($conn, $sql)) {
                    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                }
            }
*/
/*
            ... get current probes
*/
            $currentProbeIds = array("");
            $sql = "SELECT probeId FROM currentprobes";
            $result = mysqli_query($conn, $sql);            
            for ($i = 1; $i <= 8; $i++) {
                $row = mysqli_fetch_assoc($result);
                $currentProbeId = $row[probeId];
                array_push($currentProbeIds, $currentProbeId);            
            }
/*
            ... Get number of probes
*/
            $sql = "SELECT burnId, burnState, numberProbes, blowerSpeed, ventPosition FROM currentburn WHERE currentBurnId=1";
            $result = mysqli_query($conn, $sql);            
            $row = mysqli_fetch_assoc($result);
            $burnId= $row[burnId];
            $burnState= $row[burnState];
            $numberProbes= $row[numberProbes];
            $blowerSpeed= $row[blowerSpeed];
            $ventPosition= $row[ventPosition];
/*
            if ($numberProbes == null) {
                $numberProbes = 1;
            }
*/
/*
            ... Get probes
*/
            $sql = "SELECT probeId, manufacturer, model FROM probes";
            $probesresult = mysqli_query($conn, $sql);
            $probeIds = array("");
            $probeManufacturers = array("");
            $probeModels = array("");
            if (mysqli_num_rows($probesresult) > 0) {
                while($row = mysqli_fetch_assoc($probesresult)) {
                    array_push($probeIds, $row["probeId"]);
                    array_push($probeManufacturers, $row["manufacturer"]);
                    array_push($probeModels, $row["model"]);
                }
            } else {
                echo "No probes defined<br>";
            }

            $probesRequired = "*";
            if ($_GET['probesRequired'] == 'true') {
                $probesRequired = '* Required';
            }
/*
            ... generate input form
*/
            echo '<form action="newburnprobesprocess.php" method="post">';
                echo "<table>";
                    for ($i = 1; $i <= $numberProbes; $i++) {
                        echo '<tr>';
                            echo '<th>Probe ' . $i . '</th>';
                            echo '<th>';
                                echo '<select name="probe' . $i . 'Id">';
                                for($j = 0; $j < count($probeIds); $j++) {
                                    if ($probeIds[$j] == $currentProbeIds[$i]) {
                                        echo '<option value="' . $probeIds[$j] . '" selected>' . $probeManufacturers[$j] . ' ' . $probeModels[$j] . '</option>';
                                    } else {
                                        echo '<option value="' . $probeIds[$j] . '">' . $probeManufacturers[$j] . ' ' . $probeModels[$j] . '</option>';
                                    }
                                }
                                echo '</select>';
                                echo '<span class="error">' . $probesRequired . '</span>'; 
                            echo "</th>";
                    }
                echo "</table>";
                echo '<input type="submit" name="next" value="Next"><br>';
            echo '</form>';
/*
            ... close database connection
*/
            mysqli_close($conn);
        ?>
    </div>
    </body>
</html>