<?php
    session_start();
    require_once './config/database.php';
    require_once './config/defines.php';

    $data = $_GET['data'];
//    error_reporting(0);
    //prijimana data se indexuji zleva od 0
    
    $U = $data[START_U]*10000 + $data[START_U+1]*1000 + $data[START_U+2]*100 + $data[START_U+3]*10 + $data[START_U+4]*1;
    
    $I = $data[START_I]*10000 + $data[START_I+1]*1000 + $data[START_I+2]*100 + $data[START_I+3]*10 + $data[START_I+4]*1;
    
    $P = $data[START_P]*10000 + $data[START_P+1]*1000 + $data[START_P+2]*100 + $data[START_P+3]*10 + $data[START_P+4]*1;
    
    $Q = $data[START_Q]*10000 + $data[START_Q+1]*1000 + $data[START_Q+2]*100 + $data[START_Q+3]*10 + $data[START_Q+4]*1;
    
    $S = $data[START_S]*10000 + $data[START_S+1]*1000 + $data[START_S+2]*100 + $data[START_S+3]*10 + $data[START_S+4]*1;
    
    $n = $data[START_n]*10000 + $data[START_n+1]*1000 + $data[START_n+2]*100 + $data[START_n+3]*10 + $data[START_n+4]*1;
    
    $time = $data[START_time]*100000 + $data[START_time+1]*10000 + $data[START_time+2]*1000 + $data[START_time+3]*100 + $data[START_time+4]*10 + $data[START_time+5]*1;
    
    $date = $data[START_date+4]*100000 + $data[START_date+5]*10000 + $data[START_date+2]*1000 + $data[START_date+3]*100 + $data[START_date]*10 + $data[START_date+1]*1;
    
    $R = $data[START_R];
    $output = $R;
    
    $result = mysql_query('SELECT VALUE FROM variables WHERE NAME = \'STATUS\' AND CHANGED = 1');
    if (mysql_num_rows($result))
    {
        $status = mysql_fetch_array($result);
        $output = $status[0];
        mysql_query('UPDATE variables SET CHANGED=0 WHERE NAME=\'STATUS\'');
    }
    
    $vysledek = mysql_query("INSERT INTO power (U, I, P, Q, S, n, time, date, R) VALUES ('".$U."', '".$I."','".$P."','".$Q."','".$S."','".$n."','".$time."','".$date."','".$output."')");
    
    
    if ($vysledek) {
        date_default_timezone_set("Europe/Prague");
        echo "MESSAGE" . date("Hisdmyw") . $output ."SUCCESS";
        //echo 'MESSAGE12554526041631SUCCESS';
    }
    else
    {
        echo 'ERROR';
    }

?>