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

    $col = $_GET["c"];  // get sort type
    $asc = $_GET["a"];  // get sort type
    $days = $_GET["d"];  // get number of days
    $sec = $days * 86400;

    $timestamp = strtotime('tomorrow midnight');
    
    require '../php/database.php';

    if ($asc == 1) {
        $query = "SELECT log.logID, log.logTime, log.note, logType.logType, pinsAvailable.name, logSource.logSource, logAction.logAction
        FROM log
        LEFT JOIN logType
        ON log.logTypeID = logType.logTypeID
        LEFT JOIN pinsAvailable
        ON log.logPinID = pinsAvailable.pinID
        LEFT JOIN logSource
        ON log.logSourceID = logSource.logSourceID
        LEFT JOIN logAction
        ON log.logActionID = logAction.logActionID
        WHERE logTime >= $timestamp - $sec
        ORDER BY $col ASC, logTime ASC";
    }
    else {
        $query = "SELECT log.logID, log.logTime, log.note, logType.logType, pinsAvailable.name, logSource.logSource, logAction.logAction
        FROM log
        LEFT JOIN logType
        ON log.logTypeID = logType.logTypeID
        LEFT JOIN pinsAvailable
        ON log.logPinID = pinsAvailable.pinID
        LEFT JOIN logSource
        ON log.logSourceID = logSource.logSourceID
        LEFT JOIN logAction
        ON log.logActionID = logAction.logActionID
        WHERE logTime >= $timestamp - $sec
        ORDER BY $col DESC, logTime 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);