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

    filename:    newburnprobesprocess.php
    description: initialize current probes for new burn
    inputs:       probeIds
    outputs:      set
    actions:      none
    redirections: currentburnadjust.php 

********************************************************************************
*/
/*
    ... connect to database
*/
    include 'functions.php';
    $conn = connectToDB();
/*
    ... Get number of probes
*/
    $sql = "SELECT numberProbes FROM currentburn WHERE currentBurnId=1";
    $result = mysqli_query($conn, $sql);            
    $row = mysqli_fetch_assoc($result);
    $numberProbes= $row[numberProbes];
/*
    ... Get probe ids
*/
    $probes = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
    $probes[1] = $_POST["probe1Id"];
    $probes[2] = $_POST["probe2Id"];
    $probes[3] = $_POST["probe3Id"];
    $probes[4] = $_POST["probe4Id"];
    $probes[5] = $_POST["probe5Id"];
    $probes[6] = $_POST["probe6Id"];
    $probes[7] = $_POST["probe7Id"];
    $probes[8] = $_POST["probe8Id"];
/*
    ... redirect to probe selection if no probes selected
*/
    $probesRequired = "";
    for ($i = 1; $i <= $numberProbes; $i++) {
        if ($probes[$i] == "") {
            $probesRequired = "probesRequired=true";   
            header('Location: newburnprobes.php?' . $probesRequired);
            exit;
        }
    }
/*
    ... set current probe parameters
*/
    for ($i = 1; $i <= $numberProbes; $i++) {
        $probeId = $probes[$i];
        if ($probeId != null) {
            $sql = "SELECT manufacturer, model, a, b, c FROM probes WHERE probeId=$probeId";
            $result = mysqli_query($conn, $sql);
            if (mysqli_num_rows($result) > 0) {
                $row = mysqli_fetch_assoc($result);
                $manufacturer = $row["manufacturer"];
                $model = $row["model"];
                $a = $row["a"];
                $b = $row["b"];
                $c = $row["c"];
            }
            $sql = "UPDATE currentprobes SET probeId=$probeId, manufacturer='$manufacturer', model='$model', a=$a, b=$b, c=$c WHERE probePosition=$i";  
            if (!mysqli_query($conn, $sql)) {
                echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            }
        }
    }
/*
    ... close database connection
*/
    mysqli_close($conn);
/*
    ... redirect to current burn adjust
*/
    header('Location: currentburnadjust.php?action=set');
?>