<?php

include('config.php');
include('function.php');
include('countrycodes.php');

date_default_timezone_set('UTC');

define('GX_PATH', realpath(__DIR__.'/'));
define('GX_LIB', GX_PATH.'/lib/');

require 'autoload.php';
new Db();

$q = Db::result("SELECT `id` FROM `conversions` WHERE `id` > '{$_GET['last_id']}' ");
$n = Db::$num_rows;

$lastq = Db::result("SELECT `id` FROM `conversions` ORDER BY `id` DESC LIMIT 1  ");
$last_id = $lastq[0]->id;
$topEarning = ""; //topEarning();
header("Content-type:application/json");

if( $n > 0 ) {
    $data = Db::result("SELECT * FROM `conversions` WHERE `id` > '{$_GET['last_id']}' ORDER BY `id` DESC");
    $result = array(
        'last_id' => $last_id,
        'result' => true,
        'data' => $data,
        'countrycodes' => $countrycodes,
        'topEarning' => $topEarning
    );

    echo json_encode($result);
} else {
    $result = array(
        'last_id' => $_GET['last_id'],
        'result' => false
    );

    echo json_encode($result);
}