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

    filename:     currentburn.php
    description:  Show status of current burn and current probes
                  Present available actions based on state of burn
    inputs:      
    outputs:     
    actions:      preheat, cook, check, done, switch, adjustdamper, currentburndetail, read, adjusttemps, chart, probedetail
    redirections: currentburnprocess.php

********************************************************************************
-->
    <html>
        <head>
        <style>
            table {
                border-collapse: collapse;
            }

            table, td, th {
                border: 1px solid black;
            }
            #red {
                color: red;
            }
        </style>
        <?php include 'head.html' ?>
        <script src = "navigation.js"></script>
<!--
        ... refresh every 10 seconds
-->
<!--
        <meta http-equiv="refresh" content="10">
-->
        </head>
    <body>
        <?php include 'navigationmanageburns.html' ?>
        <h3>CURRENT BURN</h3>
        <div id="content">
            <?php
/*
                ... connect to database
*/
                include 'functions.php';
                $conn = connectToDB();
/*
                ... get mode
*/
                $sql = "SELECT mode FROM system
                    WHERE systemId=1";
                $result = mysqli_query($conn, $sql);            
                $row = mysqli_fetch_assoc($result);
                $mode= $row[mode];
/*
                ... get current burn
*/
                $currentBurn = getCurrentBurn();
                $burnId = $currentBurn[burnId];
                $burnState = $currentBurn[burnState];
                $numberProbes = $currentBurn[numberProbes];
                $startDateTime = $currentBurn[startDateTime];
                $endDateTime = $currentBurn[endDateTime];
                $blowerSpeed = $currentBurn[blowerSpeed];
                $ventPosition= $currentBurn[ventPosition];
                if ($burnId == null) {
//                  exit("no current burn<br>");
                    echo "no current burn<br>";
                } else {
/*
                    ... set new state
*/
                    $action = $_GET['action'];
                    if ($action == "ready") {
                        $newBurnState = "Ready";
                    } elseif ($action == "preheat") {
                        $newBurnState = "Preheating";
                    } elseif ($action == "cook") {
                        $newBurnState = "Cooking";
                    } elseif ($action == "check") {
                        $newBurnState = "Checking";
                    } elseif ($action == "done") {
                        $newBurnState = "Done";
                    } else {
                        $newBurnState = $burnState; // none
                    }
/*
                    ... set start and end date/times
*/
                    $currentDateTime = date('Y-m-d H:i:s');
                    if (($burnState == "Preheating" || $burnState == "Ready") &&
                        $newBurnState == "Cooking") {
                        $startDateTime = $currentDateTime;
                    }
                    if (($burnState == "Cooking" || $burnState == "Checking") &&
                        $newBurnState == "Done") {
                        $endDateTime = $currentDateTime;
                    }
/*
                    ... update current burn
*/
                    $sql = "UPDATE currentburn SET burnState='$newBurnState',
                        startDateTime='$startDateTime', endDateTime='$endDateTime'
                        WHERE currentBurnId=1";
                    if (!mysqli_query($conn, $sql)) {
                        echo "Error updating table: " . mysqli_error($conn);
                    }

                    $sql = "UPDATE burns SET startDateTime='$startDateTime',
                        endDateTime='$endDateTime'
                        WHERE burnId=$burnId";
                    if (!mysqli_query($conn, $sql)) {
                        echo "Error updating table: " . mysqli_error($conn);
                    }
/*
                    ... initialize warnings array
*/
                    $warnings = array("", "", "", "", "", "", "", "", "");
/*
                    ... alert and warn if pit to temp, over temp, under temp
*/
                    $sql = "SELECT probeTemp, targetTemp, highTemp, lowTemp FROM currentprobes
                        WHERE probePosition=1";
                    $results = mysqli_query($conn, $sql);
                    $row = mysqli_fetch_assoc($results);
                    $probeTemp = $row["probeTemp"];
                    $targetTemp = $row["targetTemp"];
                    $highTemp = $row["highTemp"];
                    $lowTemp = $row["lowTemp"];
                    if ($probeTemp >= $targetTemp && $newBurnState == "Preheating") {
                        $warnings[1] =  "Burn Ready";
                        echo "<script>";
                            echo "alert('burn ready');";
                        echo "</script>";
/*
                    $newBurnState = "Ready";
                    $sql = "UPDATE currentburn SET burnState='Ready' WHERE currentBurnId=1";
                    if (!mysqli_query($conn, $sql)) {
                        echo "Error updating table: " . mysqli_error($conn);
                        }
*/
                    } elseif ($probeTemp >= ($targetTemp - 10) && $newBurnState == "Preheating") {
                        $warnings[1] =  "Burn almost ready";
                        echo "<script>";
                            echo "alert('Burn almost ready');";
                        echo "</script>";
                    } elseif ($probeTemp >= $highTemp && $newBurnState == "Cooking") {
                        $warnings[1] =  "Burn too high";
                        echo "<script>";
                            echo "alert('Burn too high');";
                        echo "</script>";
                    } elseif ($probeTemp <= $lowTemp && $newBurnState == "Cooking") {
                        $warnings[1] =  "Burn too low";
                        echo "<script>";
                            echo "alert('burn too low');";
                        echo "</script>";
                    }
/*
                ... alert and warn if food to temp
*/
                $sql = "SELECT probePosition, probeTemp, targetTemp FROM currentprobes
                    WHERE probePosition>1 AND probePosition <= $numberProbes";
                $results = mysqli_query($conn, $sql);
                if (mysqli_num_rows($results) > 0) {
                    while($row = mysqli_fetch_assoc($results)) {
                        $probePosition = $row["probePosition"];
                        $probeTemp = $row["probeTemp"];
                        $targetTemp = $row["targetTemp"];
                        if ($probeTemp >= $targetTemp && $newBurnState == "Cooking") {
                            $warnings[$probePosition] =  "Food done";
                           echo "<script>";
                               echo "alert('Food on probe $probePosition is done')";
                           echo "</script>";
/*
                           $newBurnState = "Done";
                           $sql = "UPDATE currentburn SET burnState='Done' WHERE currentBurnId=1";
                           if (!mysqli_query($conn, $sql)) {
                               echo "Error updating table: " . mysqli_error($conn);
*/
                       } elseif ($probeTemp >= ($targetTemp - 10) && $newBurnState == "Cooking") {
                            $warnings[$probePosition] =  "Food almost done";
                            echo "<script>";
                               echo "alert('Food on Probe $probePosition is Almost Done')";
                            echo "</script>";
                       }
                  }
             } 
/*
            ... show current burn
*/
            echo '<form action="currentburnprocess.php?burnId=' . $burnId . '" method="post">';
                echo "<table>";
                    echo"<tr>";
                        echo "<th>PARAMETER</th>";
                        echo "<th>VALUE</th>";
                    echo "</tr>";
                    echo"<tr>";
                        echo "<th>Burn ID</th>";
                        echo "<th>$burnId</th>";
                    echo "</tr>";
                    echo"<tr>";
                        echo "<th>Burn State</th>";
                        echo "<th>$newBurnState</th>";
                    echo "</tr>";
                    if ($newBurnState != "Preheating" && $newBurnState != "Ready") {
                        echo"<tr>";
                            echo "<th>Burn Start</th>";
                            echo "<th>" . date_format(date_create($startDateTime), "H:i a") . "</th>";
                        echo "</tr>";
                    }
                    if ($newBurnState == "Done") {
                        echo"<tr>";
                            echo "<th>Burn End</th>";
                            echo "<th>" . date_format(date_create($endDateTime), "H:i a") . "</th>";
                        echo "</tr>";
                    }
                    if ($newBurnState == "Cooking" || $newBurnState == "Checking") {                  
                        $burnTime = strtotime($currentDateTime) - strtotime($startDateTime);
                        $burnMinutes = round($burnTime / 60); 
                        $burnHours = floor($burnMinutes / 60);
                        $burnMinutes = $burnMinutes - $burnHours * 60;
                        echo"<tr>";
                            echo "<th>Burn Time</th>";
                            echo "<th>" . $burnHours . " hr " . $burnMinutes . " min</th>";
                        echo "</tr>";
                    } else if ($newBurnState == "Done") {
                        $burnTime = strtotime($endDateTime) - strtotime($startDateTime);
                        $burnMinutes = round($burnTime / 60); 
                        $burnHours = floor($burnMinutes / 60);
                        $burnMinutes = $burnMinutes - $burnHours * 60;
                        echo"<tr>";
                            echo "<th>Burn Time</th>";
                            echo "<th>" . $burnHours . " hr " . $burnMinutes . " min</th>";
                        echo "</tr>";
                    }
                    echo "<tr>";
                        echo "<th>Mode</th>";
                        echo "<th>$mode</th>";
                    echo "</tr>";
                    echo "<tr>";
                        echo"<th>Blower Speed</th>";
                        if ($mode == "Auto") {
                            echo "<th>$blowerSpeed</th>";
                        } else {
                            echo "<th>";
                                echo '<select name="blowerSpeed">';
                                    for($i = 0; $i <= 100; $i+=10) {
                                        if ($i == $blowerSpeed) {
                                            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 Position</th>";
                        if ($mode == "Auto") {
                            echo "<th>$ventPosition</th>";
                        } else {
                            echo "<th>";
                                echo '<select name="ventPosition">';
                                    for($i = 0; $i <= 100; $i+=10) {
                                        if ($i == $ventPosition) {
                                            echo '<option value=' . $i . ' selected>' . $i . '</option>';
                                        } else {
                                            echo '<option value=' . $i . '>' . $i . '</option>';
                                        }
                                    }
                               echo '</select>';   
                           echo "</th>";
                        }
                    echo "</tr>";
                echo "</table>";                    

                    if ($newBurnState == "Ready") {
                        echo '<input type="submit" name="preheat" value="Preheat">';
                    } else if ($newBurnState == "Preheating") {
                        echo '<input type="submit" name="cook" value="Cook">';
                    } elseif ($newBurnState == "Cooking") {
                        echo '<input type="submit" name="check" value="Check"> ';
                        echo '<input type="submit" name="done" value="Done">';
                    } elseif ($newBurnState == "Checking") {
                        echo '<input type="submit" name="cook" value="Cook"> ';
                        echo '<input type="submit" name="done" value="Done">';
                    } elseif ($newBurnState == "Done") {
                    }
               if ($mode == "Auto") {
                   echo '<input type="submit" name="manualMode" value="Switch">';
               } else {
                   echo '<input type="submit" name="adjustDamping" value="Adjust">';
                   echo '<input type="submit" name="autoMode" value="Switch">';
               }
               echo '<input type="submit" name="burndetail" value="Detail">';
               echo "<br><br>";
/*
               ... show current probes
*/
//               $sql = "SELECT probePosition, probeTemp, targetTemp FROM currentprobes
//                   WHERE probePosition<=$numberProbes";
//               $results = mysqli_query($conn, $sql);
               $currentProbes = getCurrentProbes();
               echo "<table>";
                    echo"<tr>";
                        echo "<th>PROBE</th>";
                        echo "<th>TEMP</th>";
                        echo "<th>TARGET</th>";
                        echo "<th>DIFF</th>";
                        echo "<th>ALERT</th>";
                    echo "</tr>";
                    $i = 0;        
                    foreach($currentProbes as $row) {
                        $i++;
                        if ($i <= $numberProbes) {
                            echo"<tr>";
                            $probePosition = $row["probePosition"];
                            if ($probePosition == 1) {
                                $probeLocation = "Dome";
                            } else {
                                $probeLocation = "Food";
                            }
                            $probeTemp = $row["probeTemp"];
                            $targetTemp = $row["targetTemp"];
                            $diffTemp = $probeTemp - $targetTemp;
                            echo "<th>" . $probePosition . " (" . $probeLocation . ")" . "</th>";
                            echo "<th>" . $probeTemp . "</th>";
                            echo "<th>" . $targetTemp . "</th>";
                            echo "<th>" . $diffTemp . "</th>";
                            echo "<th>";
                                echo '<div id="red">';
                                    echo "$warnings[$probePosition]";
                                echo "</div>";
                            echo"</th>";
                            echo '</tr>';
                        }
                       }
                echo "</table>";
                echo '<input type="submit" name="read" value="Read">';
                echo '<input type="submit" name="adjustTemps" value="Adjust">';
                echo '<input type="submit" name="chart" value="Chart">';
                echo '<input type="submit" name="probedetail" value="Detail">';
            echo '</form>';
        }
/*
        ... close database connection
*/
        mysqli_close($conn);
            ?>
        </div>
    </body>
</html>