<!DOCTYPE html>
<!--
********************************************************************************

    filename:     probechart.php
    description:  plot probe temp vs resistance
    inputs:       probeId
    outputs:      none
    actions:      none
    redirections: none

********************************************************************************
-->
<html>
    <head>
        <?php include 'head.html' ?>
        <script src = "navigation.js"></script>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript">
            google.charts.load('current', {'packages':['corechart']});
            google.charts.setOnLoadCallback(drawChart);
            function drawChart() {
                var data = google.visualization.arrayToDataTable([
                   ['Resistance', 'Temp(F)']
            <?php
/*
                ... connect to database
*/
                include 'functions.php';
                $conn = connectToDB();
/*
                ... get probe parameters
*/
                $probeId = $_GET["probeId"];
                $sql = "SELECT manufacturer, model, a, b, c FROM probes WHERE probeId=$probeId";
                $results = mysqli_query($conn, $sql);
                if (mysqli_num_rows($results) > 0) {
                    $row = mysqli_fetch_assoc($results);
                    $manufacturer = $row[manufacturer];
                    $model = $row[model];
                    $a = $row[a];
                    $b = $row[b];
                    $c = $row[c];
                 
                    for ($r = 100; $r <= 300000; $r += 100) {
                        $rf = (float)$r;
                        $tK = 1 / ($a + $b * log($rf) + $c * pow(log($rf),3));
                        $tF = tF($tK);
                        echo ",[" . $r . ", " . $tF . "]";
                    }
               }
               mysqli_close($conn);
        ?>
        ]);

        var options = {
          title: 'Probe Temperature (F)',
          curveType: 'function',
          legend: 'none',
          hAxis: {
              title: 'Resistance (ohms)'
          }
       };

        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

        chart.draw(data, options);
      }
      </script>
    </head>
    <body>
        <?php include 'navigationmanagefoodspitsprobes.html' ?>
        <div id="content">
        <h3>PROBE CHART</h3>
        <?php
            echo "<table>";
                echo "<tr>";
                    echo "<th>Probe ID</th>";
                    echo "<th>$probeId</th>";
                echo "</tr>";
                echo "<tr>";
                    echo "<th>Manufacturer</th>";
                    echo "<th>" . $manufacturer . "</th>";
                echo "</tr>";
                echo"<tr>";
                    echo "<th>Model</th>";
                    echo "<th>$model</th>";
                echo "</tr>";
            echo "</table>";
        ?>
        </div>
        <div id="curve_chart" style="width: 900px; height: 500px"></div>
    </body>
</html>