<?php
    header('Content-Type: application/json');
    header("Access-Control-Allow-Origin: *");

    require '../php/database.php';

    $query = sprintf("SELECT `outletSortID` FROM `systemSettings` WHERE `userID` = 1");

    //execute query
    $result = $mysqli->query($query);
    $row = mysqli_fetch_assoc($result);    
    $sortID = $row['outletSortID'];
    // echo $sortID;
    // echo " - ";
    //free memory associated with result
    $result->close();

    if ($sortID == 1) {$s = "name";}
    else if ($sortID == 2) {$s = "iconID";}
    else if ($sortID == 3) {$s = "gpio";}

    //query to get data from the table
    $query = sprintf("SELECT `pinID`, `gpio`, `assigned`, `name`, `iconID`, `invert`, `manual` FROM `pinsAvailable` WHERE `pinTypeID` = 1 AND `assigned` = 1 ORDER BY `{$s}` ASC");

    //execute query
    $result = $mysqli->query($query);

    //loop through the returned data
    $dataWindow = array();
    foreach ($result as $row) {
        $dataWindow[] = $row;
    }

    //free memory associated with result
    $result->close();

    //close connection
    $mysqli->close();
    // print_r(json_encode($dataWindow));
    //now print the data
    print json_encode($dataWindow);