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

    filename:     probesinit.php
    description:  Initialize probes database table and populate with default probes
    inputs:       none
    outputs:      none
    actions:      none
    redirections: none 

********************************************************************************
-->
<html>
    <head>
        <?php include 'head.html' ?>
        <script src = "navigation.js"></script>
    </head>
    <body>
        <?php include 'navigationmanagedatabase.html' ?>
        <h3>INITIALIZE PROBES LIST TABLE</h3>
        <div id="content">
        <?php
/*
            ... connect to database
*/
            include 'functions.php';
            $conn = connectToDB();
/*
            ... drop probes table
*/
            $sql = "DROP TABLE IF EXISTS probes";
            if (!mysqli_query($conn, $sql)) {
                echo "Error dropping table: " . mysqli_error($conn);
            }
/*
            ... create probes table
*/
            $sql = "CREATE TABLE probes ("
                . "probeId INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,"
                . "manufacturer VARCHAR(256) NULL,"
                . "model VARCHAR(256) NULL,"
                . "a FLOAT NULL,"
                . "b FLOAT NULL,"
                . "c FLOAT NULL,"
                . "lowTemp INT NULL," //0-999 degC
                . "lowResistance INT NULL," //ohms
                . "mediumTemp INT NULL," //0-999 degC
                . "mediumResistance INT NULL," //ohms
                . "highTemp INT NULL," //0-999 degC
                . "highResistance INT NULL)"; //ohms
            if (!mysqli_query($conn, $sql)) {
                error_log("Error creating table: " . mysqli_error($conn));
                echo '"probes" table NOT initialized';
                exit();
            }
/*
            ... populate probes table
*/
            $sql = "INSERT INTO probes (manufacturer, model, a, b, c, lowTemp, lowResistance, mediumTemp, mediumResistance, highTemp, highResistance) 
                VALUES ('ThermoPro', 'TP20',  0.000788385, 0.00020433, 0.000000137698, 35, 311000, 74, 106500, 210, 6900)";
            if (!mysqli_query($conn, $sql)) {
                error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
                echo '"probes" table NOT initialized';
                exit();
            }
            $sql = "INSERT INTO probes (manufacturer, model, a, b, c, lowTemp, lowResistance, mediumTemp, mediumResistance, highTemp, highResistance) 
                VALUES ('Maverick', 'ET-72/73',  0.000788385, 0.00020433, 0.000000137698, 35, 311000, 74, 106500, 210, 6900)";
            if (!mysqli_query($conn, $sql)) {
                error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
                echo '"probes" table NOT initialized';
                exit();
            }
            $sql = "INSERT INTO probes (manufacturer, model, a, b, c, lowTemp, lowResistance, mediumTemp, mediumResistance, highTemp, highResistance) 
                VALUES ('Thermoworks', 'Pro Series High Temp Air Probe',  0.000788385, 0.00020433, 0.000000137698, 35, 311000, 74, 106500, 210, 6900)";
            if (!mysqli_query($conn, $sql)) {
                error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
                echo '"probes" table NOT initialized';
                exit();
            }

            $sql = "INSERT INTO probes (manufacturer, model, a, b, c, lowTemp, lowResistance, mediumTemp, mediumResistance, highTemp, highResistance) 
                VALUES ('Thermoworks', 'Pro Series High Temp Cooking Probe',  0.000788385, 0.00020433, 0.000000137698, 35, 311000, 74, 106500, 210, 6900)";
            if (!mysqli_query($conn, $sql)) {
                error_log("Error: " . $sql . "<br>" . mysqli_error($conn));
                echo '"probes" table NOT initialized';
                exit();
            }
            echo '"probes" table initialized';
/*
            ... close database connection
*/
            mysqli_close($conn);
        ?>
    </div>
    </body>
</html>