<?php
require_once __DIR__ . '/common.php';

$dbconn = getDBConnection();

//no gets, curent point outage info
if(empty(array_diff_key($_GET, array('service' => '')))) {
    $result = pg_query($dbconn,
    "SELECT jsonb_build_object('type', 'FeatureCollection','features', jsonb_agg(features.feature)) FROM (SELECT jsonb_build_object('type', 'Feature','geometry', ST_AsGeoJSON(ST_Transform(geom, 4326))::jsonb,'properties', to_jsonb(properties) - 'geom') AS feature FROM (SELECT *FROM pzone where cwa ='RLX') AS properties) AS features") or die('Query failed: ' . pg_last_error());
    $resultArray = pg_fetch_all($result);
    header('Content-Type: application/json; charset=utf-8');
    echo($resultArray[0]['jsonb_build_object']);
    pg_free_result($result);
}

if (isset($_GET['lsrslist'])) {
    $result = pg_query($dbconn,"SELECT * from simplever") or die('Query failed: ' . pg_last_error());
    while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
        $array[] = $line;
    }
    echo json_encode($array);
    pg_free_result($result);
}

if (isset($_GET['reset'])) {
    $result = pg_query($dbconn,"truncate simplever") or die('Query failed: ' . pg_last_error());
    $resultArray = pg_fetch_all($result);
    echo($resultArray);
}

if (isset($_GET['lsrs'])) {
    if (isset($_GET['zone'])) {
        $zone = $_GET['zone'];
        if (isset($_GET['lsr'])) {
            $lsr = (int) $_GET['lsr'];
        } else {
            $lsr = 1;
        }
        if (isset($_GET['lsrs'])) {
            $dir = $_GET['dir'];
        }

        if ($dir == 1) {
            $result = pg_query_params($dbconn,"INSERT into simplever (zone,lsr) values ($1,$2) on conflict (zone) do update set lsr = (simplever.lsr + 1) where simplever.zone = $1", array($zone,$lsr)) or die('Query failed: ' . pg_last_error());
            $resultArray = pg_fetch_all($result);
        } else {
            $result = pg_query_params($dbconn,"INSERT into simplever (zone,lsr) values ($1,$2) on conflict (zone) do update set lsr = 0 where simplever.zone = $1", array($zone,$lsr)) or die('Query failed: ' . pg_last_error());
            $resultArray = pg_fetch_all($result);
        }
        pg_free_result($result);
    }
}

if (isset($_GET['inc'])) {
    if ($_GET['inc'] == 'true') {
        $hideflag = 'true';
    } else {
        $hideflag = 'false';
    }
    $id = (int) $_GET['id'];
    $query = "UPDATE reports SET hide = $1 WHERE id = $2";
    $result = pg_query_params($dbconn, $query, array($hideflag, $id)) or die('Query failed: ' . pg_last_error());
    pg_free_result($result);
}

if (isset($_GET['hide'])) {
    if ($_GET['hide'] == 'true') {
        $hideflag = 'true';
    } else {
        $hideflag = 'false';
    }
    $id = (int) $_GET['id'];
    $query = "UPDATE reports SET hide = $1 WHERE id = $2";
    $result = pg_query_params($dbconn, $query, array($hideflag, $id)) or die('Query failed: ' . pg_last_error());
    pg_free_result($result);
}

// Close database connection when needed
if (isset($dbconn)) {
    pg_close($dbconn);
}
?>