<?php
/*
********************************************************************************

    filename:     currentburnadjustprocess.php
    description:  store burn adjustments in currentprobes database table
    inputs:       probes 1-8 target/high/low temperatures
    outputs:      ready, none
    actions:      none
    redirections: currentburn.php

********************************************************************************
*/
    include 'functions.php';
    $conn = connectToDB();
    if (!$_POST["cancel"]) {
    if ($_POST["set"]) {
        $action = "set";
    } else {
        $action = "adjust";
    }

    $probeTargetTemp[1]= $_POST["probe1TargetTemp"];
    $probeHighTemp[1] = $_POST["probe1HighTemp"];
    $probeLowTemp[1] = $_POST["probe1LowTemp"];
    $probeTargetTemp[2]= $_POST["probe2TargetTemp"];
    $probeHighTemp[2] = $_POST["probe2HighTemp"];
    $probeLowTemp[2] = $_POST["probe2LowTemp"];
    $probeTargetTemp[3]= $_POST["probe3TargetTemp"];
    $probeHighTemp[3] = $_POST["probe3HighTemp"];
    $probeLowTemp[3] = $_POST["probe3LowTemp"];
    $probeTargetTemp[4]= $_POST["probe4TargetTemp"];
    $probeHighTemp[4] = $_POST["probe4HighTemp"];
    $probeLowTemp[4] = $_POST["probe4LowTemp"];
    $probeTargetTemp[5]= $_POST["probe5TargetTemp"];
    $probeHighTemp[5] = $_POST["probe5HighTemp"];
    $probeLowTemp[5] = $_POST["probe5LowTemp"];
    $probeTargetTemp[6]= $_POST["probe6TargetTemp"];
    $probeHighTemp[6] = $_POST["probe6HighTemp"];
    $probeLowTemp[6] = $_POST["probe6LowTemp"];
    $probeTargetTemp[7]= $_POST["probe7TargetTemp"];
    $probeHighTemp[7] = $_POST["probe7HighTemp"];
    $probeLowTemp[7] = $_POST["probe7LowTemp"];
    $probeTargetTemp[8]= $_POST["probe8TargetTemp"];
    $probeHighTemp[8] = $_POST["probe8HighTemp"];
    $probeLowTemp[8] = $_POST["probe8LowTemp"];

    for ($i = 1; $i <=8; $i++) {
        $sql = "";
        if ($probeTargetTemp[$i] != null) {
            $sql = $sql . " targetTemp = $probeTargetTemp[$i]";
        }
        if ($probeHighTemp[$i] != null) {
            if ($sql != "") {
                $sql = $sql . ", ";
            }
            $sql = $sql . " highTemp = $probeHighTemp[$i]";
        }
        if ($probeLowTemp[$i] != null) {
            if ($sql != "") {
                $sql = $sql . ", ";
            }
            $sql = $sql . " lowTemp = $probeLowTemp[$i]";
        } 
        if ($sql != "") {
            $sql = "UPDATE currentprobes SET" . $sql . " WHERE probePosition = $i";
            if (!mysqli_query($conn, $sql)) {
                echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            }
        }
    }
    }
    if ($action == "set") {
        header('Location: currentburn.php?action=ready');
    } else {
        header('Location: currentburn.php?action=none');
    }
?>