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

    filename:    burnimportprocess.php
    description: import burn as new burn in burns table, statuses in statuses table
    inputs:      import/cancel, existingBurnFile
    outputs:     none
    actions:     none
    redirection: burnlist.php

********************************************************************************
*/
/*
    ... get action
*/
    if ($_POST["cancel"]) {
     header('Location: burnlist.php');
     exit;       
    }
/*
    ... Get parameters passed in POST
*/
    $burnFile = $_POST["burnFile"];
/*
    ... open existing burn file
*/
    $burnsString= file_get_contents("burn_2017-12-25_1357.json");
    if ($burnsString == false) {
        header('Location: burnimport.php?existingBurnFileRequired=true');
        exit;
   }

/*
    ... connect to database
*/
    include 'functions.php';
    $conn = connectToDB();
    $burnList = json_decode($burnsString, true);
    foreach ($burnList as $burns) {
        foreach ($burns as $burn) {
//            $burnId = $burn["burnId"];
            $startDateTime = $burn["startDateTime"];
            $endDateTime = $burn["endDateTime"];
            $foodId = $burn["foodId"];
            $food = $burn["food"];
            $doneness = $burn["doneness"];
            $comments = $burn["comments"];
            $pitId = $burn["pitId"];
            $pitManufacturer = $burn["pitManufacturer"];
            $pitModel = $burn["pitModel"];
            $blowerP = $burn["blowerP"];
            $blowerI = $burn["blowerI"];
            $blowerD = $burn["blowerD"];
            $ventP = $burn["ventP"];
            $ventI = $burn["ventI"];
            $ventD = $burn["ventD"];
/*
            ... add burn to burns and retreive ID assigned to burn
*/
            $sql = "INSERT INTO burns (startDateTime, endDateTime, foodId, food, doneness, comments, pitId, pitManufacturer, pitModel, blowerP, blowerI, blowerD, ventP, ventI, ventD) VALUES ('$startDateTime', '$endDateTime', $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 burn statuses to statuses
*/
            $statuses = $burn["statuses"];
            foreach ($statuses as $status) {
//                $statusId = $status["statusId"];
//                $burnId = $status["burnId"];
                $burnState = $status["burnState"];
                $probeId = $status["probeId"];
                $probePosition = $status["probePosition"];
                $statusDateTime = $status["statusDateTime"];
                $probeTemp = $status["probeTemp"];
                $targetTemp = $status["targetTemp"];
                $highTemp = $status["highTemp"];
                $lowTemp = $status["lowTemp"];
                $blowerSpeed = $status["blowerSpeed"];
                $ventPosition = $status["ventPosition"];
                $sql = "INSERT INTO statuses (burnId, burnState, probeId, probePosition, statusDateTime, probeTemp, targetTemp, highTemp, lowTemp, blowerSpeed, ventPosition) VALUES ($burnId, '$burnState', $probeId, $probePosition, '$statusDateTime', $probeTemp, $targetTemp, $highTemp, $lowTemp, $blowerSpeed, $ventPosition)";
                if (!mysqli_query($conn, $sql)) {
                    exit("Error: " . $sql . " " . mysqli_error($conn) . "<br>");
                }
                $statusId= mysqli_insert_id($conn);
            }
        }
    }
/*
    ... close database connection
*/
    mysqli_close($conn);
/*
    .... redirect to burn list
*/
    header('Location: burnlist.php');
?>