<?php

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json; charset=UTF-8");

include_once("../database.php");

if($_SERVER['REQUEST_METHOD'] === '$_GET'){
    $sql1 = "SELECT Now()";
    $currtime = $mysqli->query($sql1);
    // $sql2 = "SELECT * FROM questions WHERE stime <= $currtime and etime >= $currtime";
    $sql2 = "SELECT * FROM questions";

    $data = [];
    if($result = $mysqli->query($sql2)){

        $i = 0;
        while($row = mysql_fetch_assoc($result)){

            $data[$i]['title'] = $row['title'];
            $data[$i]['username'] = $row['username'];
            $data[$i]['statement'] = $row['statement'];
            $data[$i]['tc1'] = $row['tc1'];
            $data[$i]['out1'] = $row['out1'];
            $data[$i]['tc2'] = $row['tc2'];
            $data[$i]['out2'] = $row['out2'];
            $data[$i]['stime'] = $row['stime'];
            $data[$i]['etime'] = $row['etime'];
            $i++;
        }
        echo json_encode($data);
    }
    else{
        http_response_code(404);
    }
}

// if($_SERVER['REQUEST_METHOD'] === '$_GET'){
//     $sql1 = "SELECT Now()";
//     $currtime = $mysqli->query($sql1);
//     $sql2 = "SELECT * FROM questions WHERE stime <= $currtime and etime >= $currtime";

//     $data = [];
//     if($result = $mysqli->query($sql2)){
//         while($row = mysql_fetch_assoc($request)){
//             array_push($data,$row);
//         }
//         echo json_encode($data);
//     }
//     else{
//         http_response_code(404);
//     }
// }

?>
