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

    filename:     burnexportprocess.php
    description:  export burn  from selected burn in burns table, statuses table
    inputs:       burnId
    outputs:      none
    actions:      none
    redirections: burnlist.php

********************************************************************************
*/
/*
    ... connect to database
*/
    define("PHP_EOL", "\n");
    include 'functions.php';
    $conn = connectToDB();
/*
    ... get burn id
*/
    $burnId = $_GET["burnId"];
/*
    ... get burn and write to file
*/
    $file = "burn" . $burnId . ".json";
    !unlink($file);
    $fref = fopen($file, "w");
    $sql = "SELECT burnId, startDateTime, endDateTime, foodId, food, doneness, comments, pitId, pitManufacturer, pitModel, blowerP, blowerI, blowerD, ventP, ventI, ventD FROM burns WHERE burnId=$burnId";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        $burnId = $row["burnId"];
        $startDateTime = $row["startDateTime"];
        $endDateTime = $row["endDateTime"];
        $foodId = $row["foodId"];
        $food = $row["food"];
        $doneness = $row["doneness"];
        $comments = $row["comments"];
        $pitId = $row["pitId"];
        $pitManufacturer = $row["pitManufacturer"];
        $pitModel = $row["pitModel"];
        $blowerP = $row["blowerP"];
        $blowerI = $row["blowerI"];
        $blowerD = $row["blowerD"];
        $ventP = $row["ventP"];
        $ventI = $row["ventI"];
        $ventD = $row["ventD"];
        $json = '{"burns": [{' .
               '"burnId": ' . $burnId . ', ' .
               '"startDateTime": ' . '"' . $startDateTime . '"' .  ', ' .
               '"endDateTime": ' . '"' . $endDateTime . '"' .  ', ' .
               '"foodId": ' . $foodId . ', ' .
               '"food": ' . '"' . $food . '"' .  ', ' .
               '"doneness": ' . '"' . $doneness . '"' .  ', ' .
               '"comments": ' . '"' . $comments . '"' .  ', ' .
               '"pitId": ' . $pitId . ', ' .
               '"pitManufacturer": ' . '"' . $pitManufacturer . '"' .  ', ' .
               '"pitModel": ' . '"' . $pitModel . '"' .  ', ' .
               '"blowerP": ' . $blowerP . ', ' .
               '"blowerI": ' . $blowerI . ', ' .
               '"blowerD": ' . $blowerD . ', ' .
               '"ventP": ' . $ventP . ', ' .
               '"ventI": ' . $ventI . ', ' .
               '"ventD": ' . $ventD;
        fwrite($fref, $json);
    }      
/*
    ... get statuses for burn and write to file
*/
        $sql = "SELECT statusId, burnId, burnState, probeId, probePosition, statusDateTime, probeTemp, targetTemp, highTemp, lowTemp, blowerSpeed, ventPosition FROM statuses WHERE burnId = $burnId";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        $json = ', ' . '"statuses": [' . PHP_EOL;
        fwrite($fref, $json);
        $i = 0;
        while($row = mysqli_fetch_assoc($result)) {
            $i+=1;
            $statusId = $row["statusId"];
            $burnId = $row["burnId"];
            $burnState = $row["burnState"];
            $probeId = $row["probeId"];
            $probePosition = $row["probePosition"];
            $statusDateTime = $row["statusDateTime"];
            $probeTemp = $row["probeTemp"];
            $targetTemp = $row["targetTemp"];
            $highTemp = $row["highTemp"];
            $lowTemp = $row["lowTemp"];
            $blowerSpeed = (int)$row["blowerSpeed"];
            $ventPosition = (int)$row["ventPosition"];
            $json = '{' .
               '"statusId": ' . $statusId . ', ' .
               '"burnId": ' . $burnId . ', ' .
               '"burnState": ' . '"' . $burnState . '"' .  ', ' .
               '"probeId": ' . $probeId . ', ' .
               '"probePosition": ' . $probePosition . ', ' .
               '"statusDateTime": ' . '"' . $statusDateTime . '"' .  ', ' .
               '"probeTemp": ' . $blowerP . ', ' .
               '"targetTemp": ' . $targetTemp . ', ' .
               '"highTemp": ' . $highTemp . ', ' .
               '"lowTemp": ' . $lowTemp . ', ' .
               '"blowerSpeed": ' . $blowerSpeed . ', ' .
               '"ventPosition": ' . $ventPosition .
               '}';
            if ($i > 1) {
                $json = ', ' . PHP_EOL . $json;
            }
            fwrite($fref, $json);
        }
    $json = ']';
    fwrite($fref, $json);
    }
    $json = '}]}';
    fwrite($fref, $json);
    fclose($fref);
/*
    ... close database connection
*/
    mysqli_close($conn);
/*
    .... redirect to burn list
*/
    header('Location: burnlist.php');
?>