<?php

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

date_default_timezone_set('UTC');

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

require 'autoload.php';
new Db();
// $login = login();

$day = isset($_GET['date']) ? $_GET['date']: date("Y-m-d"); 

foreach($api as $k => $v) {
    // var_dump($login);
    $auth = authToken($v['client_id'], $v['api_key']);
    echo $auth['access_token'];
    $data = array(
        'start_date' => urlencode($day),
        'end_date' => urlencode($day),
        'timezone' => "+00:00",
        "date_segment" => "day",
        "include_archived" => 0,
        "limit" => 500
    );
    $res = APIFetch('/statistics/sm', $auth['access_token'], http_build_query($data), "GET");
    $d = $res['data'];
    $d = array_values($d);
    print_r($d);

    Db::delete(array(
        'table' => 'link_report',
        'where' => array(
            'date' => $day,
            'network' => $k
        )
    ));
    $c = count($d);
    echo $c;
    if($c > 1) {
        for ( $i = 0; $i < $c; $i++) {
            addPerf($d[$i], $k);
        }
    }
}