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

    filename:     newburnprocess.php
    description:  add new burn to burns, initialize current burn, initialize current probes
    inputs:       pitId, numberProbes, foodId, comments
    outputs:      none
    actions:      none
    redirections: newburnprobes.php

********************************************************************************
*/
/*
    ... get action
*/
    if ($_POST["cancel"]) {
     header('Location: manageburns.php');
     exit;       
    }
/*
    ... Get parameters passed in POST
*/
    $pitId = $_POST["pitId"];
    $numberProbes = $_POST["numberProbes"];
    $foodId = $_POST["foodId"];
    $comments = $_POST["comments"];
/*
    ... If no pit specified, return to newburn, passing error back to newburn
*/
    if ($pitId == 0) {
        header('Location: newburn.php?pitRequired=true');
        exit;
    }
/*
    ... connect to database
*/
    include 'functions.php';
    $conn = connectToDB();
/*
    ... get mode
*/
    $mode = getMode();
/*
    ... get pit parameters
*/
    $sql = "SELECT pitId, manufacturer, model, blowerP, blowerI, blowerD, ventP, ventI, ventD FROM pits WHERE pitId=$pitId";
    $result = mysqli_query($conn, $sql);
    if (!mysqli_query($conn, $sql)) {
        error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
    }
    $row = mysqli_fetch_assoc($result);
    $pitId = $row["pitId"];
    $pitManufacturer = $row["manufacturer"];
    $pitModel = $row["model"];
    $blowerP = $row["blowerP"];
    $blowerI = $row["blowerI"];
    $blowerD = $row["blowerD"];
    $ventP = $row["ventP"];
    $ventI = $row["ventI"];
    $ventD = $row["ventD"];

/*
    ... get food parameters
*/
    if ($foodId != null) {
        $sql = "SELECT foodId, food, doneness, externalTemp, internalTemp FROM foods WHERE foodId=$foodId";
        $result = mysqli_query($conn, $sql);
        if (!mysqli_query($conn, $sql)) {
            error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
        }
        $row = mysqli_fetch_assoc($result);
        $foodId = $row["foodId"];
        $food = $row["food"];
        $doneness = $row["doneness"];
        $externalTemp = (int)$row["externalTemp"];
        $internalTemp = (int)$row["internalTemp"];
    } else {
        $foodId = 0;
        $food = "";
        $doneness = "";
        $externalTemp = 0;
        $internalTemp = 0;
    }
/*
    ... add new burn to burns and retreive ID assigned to new burn
*/
    $sql = "INSERT INTO burns (foodId, food, doneness, comments, pitId, pitManufacturer, pitModel, blowerP, blowerI, blowerD, ventP, ventI, ventD) VALUES ($foodId, '$food', '$doneness', '$comments', $pitId, '$pitManufacturer', '$pitModel', $blowerP, $blowerI, $blowerD, $ventP, $ventI, $ventD)";
    if (!mysqli_query($conn, $sql)) {
        error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
    }
    $burnId = mysqli_insert_id($conn);


/*
    ... add new burn to current burn
*/
    $burnState = "Ready";
    $sql = "DELETE FROM currentburn WHERE currentBurnId=1";
    if (!mysqli_query($conn, $sql)) {
        error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
    }
    $cookState = "Off";
    $foodState = "Raw";
    $sql = "INSERT INTO currentburn (currentBurnId, burnId, mode, burnState, cookState, foodState, numberProbes, foodId, food, doneness, comments, blowerSpeed, ventPosition, pitId, pitManufacturer, pitModel, blowerP, blowerI, blowerD, ventP, ventI, ventD) VALUES (1, $burnId, '$mode', '$burnState', '$cookState', '$foodState', $numberProbes, $foodId, '$food', '$doneness', '$comments', 0, 0, $pitId, '$pitManufacturer', '$pitModel', $blowerP, $blowerI, $blowerD, $ventP, $ventI, $ventD)";
    if (!mysqli_query($conn, $sql)) {
        error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
    }

/*
    ... Clear current probe settings
*/
    for ($i = 1; $i <= 8; $i++) {
        $sql = "UPDATE currentprobes SET targetTemp=0, highTemp=0, lowTemp=0 WHERE probePosition=$i";
        if (!mysqli_query($conn, $sql)) {
            error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
        }
    }
    if ($foodId != null) {
        if ($externalTemp > 20) {
            $externalHighTemp = $externalTemp + 20;
            $externalLowTemp = $externalTemp - 20;
        } else {
            $externalHighTemp = 0;
            $externalLowTemp = 0;
        }
        if ($internalTemp > 10) {
            $internalHighTemp = $internalTemp;
            $internalLowTemp = $internalTemp - 10;
        } else {
            $internalHighTemp = 0;
            $internalLowTemp = 0;
        }
        for ($i = 1; $i <= $numberProbes; $i++) {
            if ($i == 1) {
                $sql = "UPDATE currentprobes SET targetTemp=$externalTemp, highTemp=$externalHighTemp, lowTemp=$externalLowTemp WHERE probePosition=$i";
            } else {
                $sql = "UPDATE currentprobes SET targetTemp=$internalTemp, highTemp=$internalHighTemp, lowTemp=$internalLowTemp WHERE probePosition=$i";
           }
            if (!mysqli_query($conn, $sql)) {
                error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
            }
        }
    }

    /*
        go to new burn probes selection
    */
    header('Location: newburnprobes.php');
    mysqli_close($conn);
?>