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

    filename:    systemupdateprocess.php
    description: update system settings
    inputs:      update, sample interval, time zone, temperature scale, mode
    outputs:     none
    actions:     none
    redirection: systemupdate.php

********************************************************************************
*/
/*
    ... connect to database
*/
    include 'functions.php';
    $conn = connectToDB();

    if ($_POST["update"]) {
        $sampleInterval = $_POST["sampleInterval"];
        $timeZone = $_POST["timeZone"];
        $tempScale = $_POST["tempScale"];
        $mode = $_POST["mode"];
        $sql = "UPDATE system SET sampleInterval=$sampleInterval, timeZone=$timeZone, tempScale='$tempScale' WHERE systemId=1";
        if (!mysqli_query($conn, $sql)) {
            error_log("Error updating table: " . mysqli_error($conn));
            header('Location: systemupdate.php');
            exit();
        }
        if ($mode == MODEAUTO) {
            setAutoMode();
        } elseif($mode == MODEMANUAL) {
            setManualMode();
        }
    }
    header('Location: systemupdate.php');
/*
    ... close database connection
*/
    mysqli_close($conn);
?>